3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "automation.h"
24 #include "bcsignals.h"
29 #include "edlsession.h"
31 #include "floatauto.h"
32 #include "floatautos.h"
35 #include "localsession.h"
40 #include "pluginset.h"
41 #include "mainsession.h"
45 #include "trackcanvas.h"
47 #include "transition.h"
48 #include "transportque.inc"
54 Track::Track(EDL *edl, Tracks *tracks) : ListItem<Track>()
57 this->tracks = tracks;
66 track_w = edl->session->output_w;
67 track_h = edl->session->output_h;
75 plugin_set.remove_all_objects();
78 void Track::create_objects()
83 int Track::copy_settings(Track *track)
85 this->expand_view = track->expand_view;
86 this->draw = track->draw;
87 this->gang = track->gang;
88 this->record = track->record;
89 this->nudge = track->nudge;
90 this->play = track->play;
91 this->track_w = track->track_w;
92 this->track_h = track->track_h;
93 strcpy(this->title, track->title);
103 int Track::load_defaults(BC_Hash *defaults)
108 void Track::equivalent_output(Track *track, double *result)
110 if(data_type != track->data_type ||
111 track_w != track->track_w ||
112 track_h != track->track_h ||
113 play != track->play ||
114 nudge != track->nudge)
117 // Convert result to track units
118 int64_t result2 = -1;
119 automation->equivalent_output(track->automation, &result2);
120 edits->equivalent_output(track->edits, &result2);
122 int plugin_sets = MIN(plugin_set.total, track->plugin_set.total);
123 // Test existing plugin sets
124 for(int i = 0; i < plugin_sets; i++)
126 plugin_set.values[i]->equivalent_output(
127 track->plugin_set.values[i],
131 // New EDL has more plugin sets. Get starting plugin in new plugin sets
132 for(int i = plugin_sets; i < plugin_set.total; i++)
134 Plugin *current = plugin_set.values[i]->get_first_plugin();
137 if(result2 < 0 || current->startproject < result2)
138 result2 = current->startproject;
142 // New EDL has fewer plugin sets. Get starting plugin in old plugin set
143 for(int i = plugin_sets; i < track->plugin_set.total; i++)
145 Plugin *current = track->plugin_set.values[i]->get_first_plugin();
148 if(result2 < 0 || current->startproject < result2)
149 result2 = current->startproject;
153 // Number of plugin sets differs but somehow we didn't find the start of the
155 if(track->plugin_set.total != plugin_set.total && result2 < 0)
159 (*result < 0 || from_units(result2) < *result))
160 *result = from_units(result2);
164 int Track::is_synthesis(int64_t position,
167 int is_synthesis = 0;
168 for(int i = 0; i < plugin_set.total; i++)
170 Plugin *plugin = get_current_plugin(position,
177 // Assume data from a shared track is synthesized
178 if(plugin->plugin_type == PLUGIN_SHAREDMODULE)
181 is_synthesis = plugin->is_synthesis(position,
184 //printf("Track::is_synthesis %d %d\n", __LINE__, is_synthesis);
185 if(is_synthesis) break;
191 void Track::copy_from(Track *track)
193 copy_settings(track);
194 edits->copy_from(track->edits);
195 for(int i = 0; i < this->plugin_set.total; i++)
196 delete this->plugin_set.values[i];
197 this->plugin_set.remove_all_objects();
199 for(int i = 0; i < track->plugin_set.total; i++)
201 PluginSet *new_plugin_set = plugin_set.append(new PluginSet(edl, this));
202 new_plugin_set->copy_from(track->plugin_set.values[i]);
204 automation->copy_from(track->automation);
205 this->track_w = track->track_w;
206 this->track_h = track->track_h;
209 Track& Track::operator=(Track& track)
211 printf("Track::operator= 1\n");
216 int Track::vertical_span(Theme *theme)
220 result = edl->local_session->zoom_track +
222 theme->get_image("plugin_bg_data")->get_h();
224 result = edl->local_session->zoom_track;
226 if(edl->session->show_titles)
227 result += theme->get_image("title_bg_data")->get_h();
232 double Track::get_length()
234 double total_length = 0;
240 length = from_units(edits->last->startproject + edits->last->length);
241 if(length > total_length) total_length = length;
245 for(int i = 0; i < plugin_set.total; i++)
247 if( !plugin_set.values[i]->last ) continue;
248 length = from_units(plugin_set.values[i]->last->startproject +
249 plugin_set.values[i]->last->length);
250 if(length > total_length) total_length = length;
254 length = from_units(automation->get_length());
255 if(length > total_length) total_length = length;
261 int Track::has_speed()
263 FloatAutos *autos = (FloatAutos*)automation->autos[AUTOMATION_SPEED];
268 for(FloatAuto *current = (FloatAuto*)autos->first;
270 current = (FloatAuto*)current->next)
272 if(!EQUIV(current->get_value(), 1.0) ||
273 !EQUIV(current->get_control_in_value(), 0.0) ||
274 !EQUIV(current->get_control_out_value(), 0.0))
287 void Track::get_source_dimensions(double position, int &w, int &h)
289 int64_t native_position = to_units(position, 0);
290 for(Edit *current = edits->first; current; current = NEXT)
292 if(current->startproject <= native_position &&
293 current->startproject + current->length > native_position &&
296 w = current->asset->width;
297 h = current->asset->height;
304 int64_t Track::horizontal_span()
306 return (int64_t)(get_length() *
307 edl->session->sample_rate /
308 edl->local_session->zoom_sample +
313 int Track::load(FileXML *file, int track_offset, uint32_t load_flags)
316 int current_plugin = 0;
319 record = file->tag.get_property("RECORD", record);
320 play = file->tag.get_property("PLAY", play);
321 gang = file->tag.get_property("GANG", gang);
322 draw = file->tag.get_property("DRAW", draw);
323 nudge = file->tag.get_property("NUDGE", nudge);
324 expand_view = file->tag.get_property("EXPAND", expand_view);
325 track_w = file->tag.get_property("TRACK_W", track_w);
326 track_h = file->tag.get_property("TRACK_H", track_h);
328 load_header(file, load_flags);
331 result = file->read_tag();
335 if(file->tag.title_is("/TRACK"))
340 if(file->tag.title_is("TITLE"))
342 file->read_text_until("/TITLE", title, BCTEXTLEN);
345 if(load_flags && automation->load(file)
346 /* strstr(file->tag.get_title(), "AUTOS") */)
351 if(file->tag.title_is("EDITS"))
353 if(load_flags & LOAD_EDITS)
354 edits->load(file, track_offset);
357 if(file->tag.title_is("PLUGINSET"))
359 if(load_flags & LOAD_EDITS)
361 PluginSet *plugin_set = new PluginSet(edl, this);
362 this->plugin_set.append(plugin_set);
363 plugin_set->load(file, load_flags);
366 if(load_flags & LOAD_AUTOMATION)
368 if(current_plugin < this->plugin_set.total)
370 PluginSet *plugin_set = this->plugin_set.values[current_plugin];
371 plugin_set->load(file, load_flags);
377 load_derived(file, load_flags);
386 void Track::insert_asset(Asset *asset,
392 edits->insert_asset(asset,
395 to_units(position, 0),
401 // Default keyframes: We don't replace default keyframes in pasting but
402 // when inserting the first EDL of a load operation we need to replace
403 // the default keyframes.
405 // Plugins: This is an arbitrary behavior
407 // 1) No plugin in source track: Paste silence into destination
409 // 2) Plugin in source track: plugin in source track is inserted into
410 // existing destination track plugin sets, new sets being added when
413 void Track::insert_track(Track *track,
420 // Calculate minimum length of data to pad.
421 int64_t min_length = to_units(
422 MAX(edl_length, track->get_length()),
424 //printf("Track::insert_track %d %s %jd\n", __LINE__, title, min_length);
426 // Decide whether to copy settings based on load_mode
427 if(replace_default) copy_settings(track);
429 edits->insert_edits(track->edits,
430 to_units(position, 0),
435 insert_plugin_set(track,
436 to_units(position, 0),
441 automation->insert_track(track->automation,
442 to_units(position, 0),
450 // Called by insert_track
451 void Track::insert_plugin_set(Track *track,
456 // Extend plugins if no incoming plugins
457 if(!track->plugin_set.total)
459 shift_effects(position,
464 for(int i = 0; i < track->plugin_set.total; i++)
466 if(i >= plugin_set.total)
467 plugin_set.append(new PluginSet(edl, this));
469 plugin_set.values[i]->insert_edits(track->plugin_set.values[i],
477 Plugin* Track::insert_effect(const char *title,
478 SharedLocation *shared_location,
479 KeyFrame *default_keyframe,
480 PluginSet *plugin_set,
487 plugin_set = new PluginSet(edl, this);
488 this->plugin_set.append(plugin_set);
493 // Position is identical to source plugin
494 if(plugin_type == PLUGIN_SHAREDPLUGIN)
496 Track *source_track = tracks->get_item_number(shared_location->module);
499 Plugin *source_plugin = source_track->get_current_plugin(
500 edl->local_session->get_selectionstart(),
501 shared_location->plugin,
506 // From an attach operation
509 plugin = plugin_set->insert_plugin(title,
510 source_plugin->startproject,
511 source_plugin->length,
518 // From a drag operation
520 plugin = plugin_set->insert_plugin(title,
532 // This should be done in the caller
535 if(edl->local_session->get_selectionend() >
536 edl->local_session->get_selectionstart())
538 start = edl->local_session->get_selectionstart();
539 length = edl->local_session->get_selectionend() - start;
544 length = get_length();
547 //printf("Track::insert_effect %f %f %d %d\n", start, length, to_units(start, 0),
548 // to_units(length, 0));
550 plugin = plugin_set->insert_plugin(title,
558 //printf("Track::insert_effect 2 %f %f\n", start, length);
564 void Track::move_plugins_up(PluginSet *plugin_set)
566 for(int i = 0; i < this->plugin_set.total; i++)
568 if(this->plugin_set.values[i] == plugin_set)
572 PluginSet *temp = this->plugin_set.values[i - 1];
573 this->plugin_set.values[i - 1] = this->plugin_set.values[i];
574 this->plugin_set.values[i] = temp;
576 SharedLocation old_location, new_location;
577 new_location.module = old_location.module = tracks->number_of(this);
578 old_location.plugin = i;
579 new_location.plugin = i - 1;
580 tracks->change_plugins(old_location, new_location, 1);
586 void Track::move_plugins_down(PluginSet *plugin_set)
588 for(int i = 0; i < this->plugin_set.total; i++)
590 if(this->plugin_set.values[i] == plugin_set)
592 if(i == this->plugin_set.total - 1) break;
594 PluginSet *temp = this->plugin_set.values[i + 1];
595 this->plugin_set.values[i + 1] = this->plugin_set.values[i];
596 this->plugin_set.values[i] = temp;
598 SharedLocation old_location, new_location;
599 new_location.module = old_location.module = tracks->number_of(this);
600 old_location.plugin = i;
601 new_location.plugin = i + 1;
602 tracks->change_plugins(old_location, new_location, 1);
609 void Track::remove_asset(Indexable *asset)
611 for(Edit *edit = edits->first; edit; edit = edit->next)
613 if(asset->is_asset &&
615 edit->asset == (Asset*)asset)
620 if(!asset->is_asset &&
622 edit->nested_edl == (EDL*)asset)
624 edit->nested_edl = 0;
630 void Track::remove_pluginset(PluginSet *plugin_set)
633 for(i = 0; i < this->plugin_set.total; i++)
634 if(plugin_set == this->plugin_set.values[i]) break;
636 this->plugin_set.remove_object(plugin_set);
637 for(i++ ; i < this->plugin_set.total; i++)
639 SharedLocation old_location, new_location;
640 new_location.module = old_location.module = tracks->number_of(this);
641 old_location.plugin = i;
642 new_location.plugin = i - 1;
643 tracks->change_plugins(old_location, new_location, 0);
647 void Track::shift_keyframes(int64_t position, int64_t length)
649 automation->paste_silence(position, position + length);
650 // Effect keyframes are shifted in shift_effects
653 void Track::shift_effects(int64_t position, int64_t length, int edit_autos)
655 for(int i = 0; i < plugin_set.total; i++)
657 plugin_set.values[i]->shift_effects(position, length, edit_autos);
661 void Track::detach_effect(Plugin *plugin)
663 //printf("Track::detach_effect 1\n");
664 for(int i = 0; i < plugin_set.total; i++)
666 PluginSet *plugin_set = this->plugin_set.values[i];
667 for(Plugin *dest = (Plugin*)plugin_set->first;
669 dest = (Plugin*)dest->next)
673 int64_t start = plugin->startproject;
674 int64_t end = plugin->startproject + plugin->length;
676 plugin_set->clear(start, end, 1);
678 //printf("Track::detach_effect 2 %d\n", plugin_set->length());
679 // Delete 0 length pluginsets
686 void Track::resample(double old_rate, double new_rate)
688 edits->resample(old_rate, new_rate);
689 automation->resample(old_rate, new_rate);
690 for(int i = 0; i < plugin_set.total; i++)
691 plugin_set.values[i]->resample(old_rate, new_rate);
692 nudge = (int64_t)(nudge * new_rate / old_rate);
695 void Track::detach_shared_effects(int module)
697 for(int i = 0; i < plugin_set.size(); i++) {
698 PluginSet *plugin_set = this->plugin_set.get(i);
699 for(Plugin *dest = (Plugin*)plugin_set->first; dest; ) {
700 if( (dest->plugin_type == PLUGIN_SHAREDPLUGIN ||
701 dest->plugin_type == PLUGIN_SHAREDMODULE) &&
702 dest->shared_location.module == module ) {
703 int64_t start = dest->startproject;
704 int64_t end = dest->startproject + dest->length;
705 plugin_set->clear(start, end, 1);
708 if(dest) dest = (Plugin*)dest->next;
715 void Track::optimize()
718 for(int i = 0; i < plugin_set.total; ) {
719 PluginSet *plugin_set = this->plugin_set.values[i];
720 plugin_set->optimize();
721 //printf("Track::optimize %d\n", plugin_set.values[i]->total());
722 // new definition of empty track...
723 if( !plugin_set->last ||
724 (plugin_set->last == plugin_set->first &&
725 plugin_set->last->silence()) ) {
726 remove_pluginset(plugin_set);
733 Plugin* Track::get_current_plugin(double position,
740 if(convert_units) position = to_units(position, 0);
741 if(use_nudge) position += nudge;
743 if(plugin_set >= this->plugin_set.total || plugin_set < 0) return 0;
745 //printf("Track::get_current_plugin 1 %d %d %d\n", position, this->plugin_set.total, direction);
746 if(direction == PLAY_FORWARD)
748 for(current = (Plugin*)this->plugin_set.values[plugin_set]->last;
750 current = (Plugin*)PREVIOUS)
752 // printf("Track::get_current_plugin 2 %d %ld %ld\n",
753 // current->startproject,
754 // current->startproject + current->length,
756 if(current->startproject <= position &&
757 current->startproject + current->length > position)
764 if(direction == PLAY_REVERSE)
766 for(current = (Plugin*)this->plugin_set.values[plugin_set]->first;
768 current = (Plugin*)NEXT)
770 if(current->startproject < position &&
771 current->startproject + current->length >= position)
781 Plugin* Track::get_current_transition(double position,
788 if(convert_units) position = to_units(position, 0);
789 if(use_nudge) position += nudge;
791 if(direction == PLAY_FORWARD)
793 for(current = edits->last; current; current = PREVIOUS)
795 if(current->startproject <= position && current->startproject + current->length > position)
797 //printf("Track::get_current_transition %p\n", current->transition);
798 if(current->transition && position < current->startproject + current->transition->length)
800 result = current->transition;
807 if(direction == PLAY_REVERSE)
809 for(current = edits->first; current; current = NEXT)
811 if(current->startproject < position && current->startproject + current->length >= position)
813 if(current->transition && position <= current->startproject + current->transition->length)
815 result = current->transition;
825 void Track::synchronize_params(Track *track)
827 for(Edit *this_edit = edits->first, *that_edit = track->edits->first;
828 this_edit && that_edit;
829 this_edit = this_edit->next, that_edit = that_edit->next)
831 this_edit->synchronize_params(that_edit);
834 for(int i = 0; i < plugin_set.total && i < track->plugin_set.total; i++)
835 plugin_set.values[i]->synchronize_params(track->plugin_set.values[i]);
837 automation->copy_from(track->automation);
838 this->nudge = track->nudge;
845 int Track::dump(FILE *fp)
847 fprintf(fp," Data type %d\n", data_type);
848 fprintf(fp," Title %s\n", title);
849 fprintf(fp," Edits:\n");
850 for(Edit* current = edits->first; current; current = NEXT)
854 automation->dump(fp);
855 fprintf(fp," Plugin Sets: %d\n", plugin_set.total);
857 for(int i = 0; i < plugin_set.total; i++)
858 plugin_set.values[i]->dump(fp);
859 //printf("Track::dump 2\n");
883 Track::Track() : ListItem<Track>()
888 // ======================================== accounting
890 int Track::number_of()
892 return tracks->number_of(this);
907 // ================================================= editing
909 int Track::select_auto(AutoConf *auto_conf, int cursor_x, int cursor_y)
914 int Track::move_auto(AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
919 int Track::release_auto()
924 // used for copying automation alone
925 int Track::copy_automation(double selectionstart,
931 int64_t start = to_units(selectionstart, 0);
932 int64_t end = to_units(selectionend, 1);
934 file->tag.set_title("TRACK");
938 file->append_newline();
940 automation->copy(start, end, file, default_only, active_only);
942 if(edl->session->auto_conf->plugins)
944 for(int i = 0; i < plugin_set.total; i++)
947 plugin_set.values[i]->copy_keyframes(start,
955 file->tag.set_title("/TRACK");
957 file->append_newline();
958 file->append_newline();
959 file->append_newline();
960 file->append_newline();
965 int Track::paste_automation(double selectionstart,
973 // Only used for pasting automation alone.
978 int current_pluginset;
980 if(data_type == TRACK_AUDIO)
981 scale = edl->session->sample_rate / sample_rate;
983 scale = edl->session->frame_rate / frame_rate;
985 total_length *= scale;
986 start = to_units(selectionstart, 0);
987 length = to_units(total_length, 1);
989 current_pluginset = 0;
990 //printf("Track::paste_automation 1\n");
994 result = file->read_tag();
998 if(file->tag.title_is("/TRACK"))
1001 if(automation->paste(start, length, scale, file,
1002 default_only, active_only, 0)) {}
1003 else if(file->tag.title_is("PLUGINSET")) {
1004 if(current_pluginset < plugin_set.total) {
1005 plugin_set.values[current_pluginset]->
1006 paste_keyframes(start, length, file,
1007 default_only, active_only);
1008 current_pluginset++;
1018 void Track::clear_automation(double selectionstart,
1019 double selectionend,
1023 int64_t start = to_units(selectionstart, 0);
1024 int64_t end = to_units(selectionend, 1);
1026 automation->clear(start, end, edl->session->auto_conf, 0);
1028 if(edl->session->auto_conf->plugins)
1030 for(int i = 0; i < plugin_set.total; i++)
1032 plugin_set.values[i]->clear_keyframes(start, end);
1038 void Track::set_automation_mode(double selectionstart,
1039 double selectionend,
1042 int64_t start = to_units(selectionstart, 0);
1043 int64_t end = to_units(selectionend, 1);
1045 automation->set_automation_mode(start, end, mode, edl->session->auto_conf);
1051 int Track::copy(double start,
1054 const char *output_path)
1056 // Use a copy of the selection in converted units
1057 // So copy_automation doesn't reconvert.
1058 int64_t start_unit = to_units(start, 0);
1059 int64_t end_unit = to_units(end, 1);
1064 file->tag.set_title("TRACK");
1065 file->tag.set_property("RECORD", record);
1066 file->tag.set_property("NUDGE", nudge);
1067 file->tag.set_property("PLAY", play);
1068 file->tag.set_property("GANG", gang);
1069 file->tag.set_property("DRAW", draw);
1070 file->tag.set_property("EXPAND", expand_view);
1071 file->tag.set_property("TRACK_W", track_w);
1072 file->tag.set_property("TRACK_H", track_h);
1075 file->append_newline();
1078 file->tag.set_title("TITLE");
1080 file->append_text(title);
1081 file->tag.set_title("/TITLE");
1083 file->append_newline();
1085 // if(data_type == TRACK_AUDIO)
1086 // file->tag.set_property("TYPE", "AUDIO");
1088 // file->tag.set_property("TYPE", "VIDEO");
1090 // file->append_tag();
1091 // file->append_newline();
1093 edits->copy(start_unit, end_unit, file, output_path);
1096 auto_conf.set_all(1);
1097 automation->copy(start_unit, end_unit, file, 0, 0);
1100 for(int i = 0; i < plugin_set.total; i++)
1102 plugin_set.values[i]->copy(start_unit, end_unit, file);
1105 copy_derived(start_unit, end_unit, file);
1107 file->tag.set_title("/TRACK");
1109 file->append_newline();
1110 file->append_newline();
1111 file->append_newline();
1112 file->append_newline();
1117 int Track::copy_assets(double start,
1119 ArrayList<Asset*> *asset_list)
1123 start = to_units(start, 0);
1124 end = to_units(end, 1);
1126 Edit *current = edits->editof((int64_t)start, PLAY_FORWARD, 0);
1129 while(current && current->startproject < end)
1131 // Check for duplicate assets
1134 for(i = 0, result = 0; i < asset_list->total; i++)
1136 if(asset_list->values[i] == current->asset) result = 1;
1138 // append pointer to new asset
1139 if(!result) asset_list->append(current->asset);
1152 int Track::clear(double start,
1161 // Edits::move_auto calls this routine after the units are converted to the track
1163 //printf("Track::clear 1 %d %d %d\n", edit_edits, edit_labels, edit_plugins);
1166 start = to_units(start, 0);
1167 end = to_units(end, 1);
1172 automation->clear((int64_t)start, (int64_t)end, 0, 1);
1176 for(int i = 0; i < plugin_set.total; i++)
1178 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1179 plugin_set.values[i]->clear((int64_t)start, (int64_t)end, edit_autos);
1184 edits->clear((int64_t)start, (int64_t)end);
1188 int Track::clear_handle(double start,
1195 edits->clear_handle(start, end, clear_plugins, edit_autos, distance);
1199 int Track::popup_transition(int cursor_x, int cursor_y)
1206 int Track::modify_edithandles(double oldposition,
1214 edits->modify_handles(oldposition,
1228 int Track::modify_pluginhandles(double oldposition,
1236 for(int i = 0; i < plugin_set.total; i++)
1238 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1239 plugin_set.values[i]->modify_handles(oldposition,
1243 // Don't allow plugin tweeks to affect edits.
1254 int Track::paste_silence(double start, double end, int edit_plugins, int edit_autos)
1256 int64_t start_i = to_units(start, 0);
1257 int64_t end_i = to_units(end, 1);
1259 edits->paste_silence(start_i, end_i);
1260 if(edit_autos) shift_keyframes(start_i, end_i - start_i);
1261 if(edit_plugins) shift_effects(start_i, end_i - start_i, edit_autos);
1267 int Track::select_edit(int cursor_x,
1275 int Track::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
1280 void Track::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
1282 for(int i = 0; i < plugin_set.total; i++)
1284 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1286 plugin = (Plugin*)plugin->next)
1288 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN)
1290 if(plugin->shared_location == old_location)
1291 plugin->shared_location = new_location;
1293 if(do_swap && plugin->shared_location == new_location)
1294 plugin->shared_location = old_location;
1300 void Track::change_modules(int old_location, int new_location, int do_swap)
1302 for(int i = 0; i < plugin_set.total; i++)
1304 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1306 plugin = (Plugin*)plugin->next)
1308 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN ||
1309 plugin->plugin_type == PLUGIN_SHAREDMODULE)
1311 if(plugin->shared_location.module == old_location)
1312 plugin->shared_location.module = new_location;
1314 if(do_swap && plugin->shared_location.module == new_location)
1315 plugin->shared_location.module = old_location;
1322 int Track::playable_edit(int64_t position, int direction)
1325 if(direction == PLAY_REVERSE) position--;
1326 for(Edit *current = edits->first; current && !result; current = NEXT)
1328 if(current->startproject <= position &&
1329 current->startproject + current->length > position)
1331 //printf("Track::playable_edit %p %p\n", current->transition, current->asset);
1332 if(current->transition ||
1334 current->nested_edl) result = 1;
1341 int Track::need_edit(Edit *current, int test_transitions)
1343 return ((test_transitions && current->transition) ||
1344 (!test_transitions && current->asset));
1347 int64_t Track::plugin_change_duration(int64_t input_position,
1348 int64_t input_length,
1352 if(use_nudge) input_position += nudge;
1353 for(int i = 0; i < plugin_set.total; i++)
1355 int64_t new_duration = plugin_set.values[i]->plugin_change_duration(
1359 if(new_duration < input_length) input_length = new_duration;
1361 return input_length;
1364 int64_t Track::edit_change_duration(int64_t input_position,
1365 int64_t input_length,
1367 int test_transitions,
1371 int64_t edit_length = input_length;
1372 if(use_nudge) input_position += nudge;
1376 // ================================= Reverse playback
1377 // Get first edit on or after position
1378 for(current = edits->first;
1379 current && current->startproject + current->length <= input_position;
1385 if(current->startproject > input_position)
1387 // Before first edit
1391 if(need_edit(current, test_transitions))
1393 // Over an edit of interest.
1394 if(input_position - current->startproject < input_length)
1395 edit_length = input_position - current->startproject + 1;
1399 // Over an edit that isn't of interest.
1400 // Search for next edit of interest.
1401 for(current = PREVIOUS ;
1403 current->startproject + current->length > input_position - input_length &&
1404 !need_edit(current, test_transitions);
1409 need_edit(current, test_transitions) &&
1410 current->startproject + current->length > input_position - input_length)
1411 edit_length = input_position - current->startproject - current->length + 1;
1416 // Not over an edit. Try the last edit.
1417 current = edits->last;
1419 ((test_transitions && current->transition) ||
1420 (!test_transitions && current->asset)))
1421 edit_length = input_position - edits->length() + 1;
1426 // =================================== forward playback
1427 // Get first edit on or before position
1428 for(current = edits->last;
1429 current && current->startproject > input_position;
1435 if(current->startproject + current->length <= input_position)
1437 // Beyond last edit.
1441 if(need_edit(current, test_transitions))
1443 // Over an edit of interest.
1444 // Next edit is going to require a change.
1445 if(current->length + current->startproject - input_position < input_length)
1446 edit_length = current->startproject + current->length - input_position;
1450 // Over an edit that isn't of interest.
1451 // Search for next edit of interest.
1452 for(current = NEXT ;
1454 current->startproject < input_position + input_length &&
1455 !need_edit(current, test_transitions);
1460 need_edit(current, test_transitions) &&
1461 current->startproject < input_position + input_length)
1462 edit_length = current->startproject - input_position;
1467 // Not over an edit. Try the first edit.
1468 current = edits->first;
1470 ((test_transitions && current->transition) ||
1471 (!test_transitions && current->asset)))
1472 edit_length = edits->first->startproject - input_position;
1476 if(edit_length < input_length)
1479 return input_length;
1482 void Track::shuffle_edits(double start, double end, int first_track)
1484 ArrayList<Edit*> new_edits;
1485 ArrayList<Label*> new_labels;
1486 int64_t start_units = to_units(start, 0);
1487 int64_t end_units = to_units(end, 0);
1488 // Sample range of all edits selected
1489 //int64_t total_start_units = 0;
1490 //int64_t total_end_units = 0;
1491 // Edit before range
1492 Edit *start_edit = 0;
1493 int have_start_edit = 0;
1495 // Move all edit pointers to list
1496 for(Edit *current = edits->first;
1499 if(current->startproject >= start_units &&
1500 current->startproject + current->length <= end_units)
1502 if(!have_start_edit) start_edit = current->previous;
1503 have_start_edit = 1;
1504 //total_start_units = current->startproject;
1505 //total_end_units = current->startproject + current->length;
1506 new_edits.append(current);
1508 // Move label pointers
1509 if(first_track && edl->session->labels_follow_edits)
1511 double start_seconds = from_units(current->startproject);
1512 double end_seconds = from_units(current->startproject +
1514 for(Label *label = edl->labels->first;
1516 label = label->next)
1518 if(label->position >= start_seconds &&
1519 label->position < end_seconds)
1521 new_labels.append(label);
1522 edl->labels->remove_pointer(label);
1527 // Remove edit pointer
1528 Edit *previous = current;
1530 edits->remove_pointer(previous);
1538 // Insert pointers in random order
1539 while(new_edits.size())
1541 int index = rand() % new_edits.size();
1542 Edit *edit = new_edits.get(index);
1543 new_edits.remove_number(index);
1544 edits->insert_after(start_edit, edit);
1547 // Recalculate start position
1548 // Save old position for moving labels
1549 int64_t startproject1 = edit->startproject;
1550 int64_t startproject2 = 0;
1553 edit->startproject =
1555 edit->previous->startproject + edit->previous->length;
1559 edit->startproject = startproject2 = 0;
1563 // Insert label pointers
1564 if(first_track && edl->session->labels_follow_edits)
1566 double start_seconds1 = from_units(startproject1);
1567 double start_seconds2 = from_units(startproject2);
1568 //double end_seconds1 = from_units(edit->startproject +
1570 for(int i = new_labels.size() - 1; i >= 0; i--)
1572 Label *label = new_labels.get(i);
1573 // Was in old edit position
1574 if(label->position >= start_seconds1 &&
1575 label->position < start_seconds2)
1577 // Move to new edit position
1578 double position = label->position -
1581 edl->labels->insert_label(position);
1582 new_labels.remove_object_number(i);
1592 if(first_track && edl->session->labels_follow_edits)
1594 edl->labels->optimize();
1598 // exactly the same as shuffle_edits except for 1 line
1599 void Track::reverse_edits(double start, double end, int first_track)
1601 ArrayList<Edit*> new_edits;
1602 ArrayList<Label*> new_labels;
1603 int64_t start_units = to_units(start, 0);
1604 int64_t end_units = to_units(end, 0);
1605 // Sample range of all edits selected
1606 //int64_t total_start_units = 0;
1607 //int64_t total_end_units = 0;
1608 // Edit before range
1609 Edit *start_edit = 0;
1610 int have_start_edit = 0;
1612 // Move all edit pointers to list
1613 for(Edit *current = edits->first; current; )
1615 if(current->startproject >= start_units &&
1616 current->startproject + current->length <= end_units)
1618 if(!have_start_edit) start_edit = current->previous;
1619 have_start_edit = 1;
1620 //total_start_units = current->startproject;
1621 //total_end_units = current->startproject + current->length;
1622 new_edits.append(current);
1624 // Move label pointers
1625 if(first_track && edl->session->labels_follow_edits)
1627 double start_seconds = from_units(current->startproject);
1628 double end_seconds = from_units(current->startproject +
1630 for(Label *label = edl->labels->first;
1632 label = label->next)
1634 if(label->position >= start_seconds &&
1635 label->position < end_seconds)
1637 new_labels.append(label);
1638 edl->labels->remove_pointer(label);
1643 // Remove edit pointer
1644 Edit *previous = current;
1646 edits->remove_pointer(previous);
1654 // Insert pointers in reverse order
1655 while(new_edits.size())
1657 int index = new_edits.size() - 1;
1658 Edit *edit = new_edits.get(index);
1659 new_edits.remove_number(index);
1660 edits->insert_after(start_edit, edit);
1663 // Recalculate start position
1664 // Save old position for moving labels
1665 int64_t startproject1 = edit->startproject;
1666 int64_t startproject2 = 0;
1669 edit->startproject =
1671 edit->previous->startproject + edit->previous->length;
1675 edit->startproject = startproject2 = 0;
1679 // Insert label pointers
1680 if(first_track && edl->session->labels_follow_edits)
1682 double start_seconds1 = from_units(startproject1);
1683 double start_seconds2 = from_units(startproject2);
1684 //double end_seconds1 = from_units(edit->startproject + edit->length);
1685 for(int i = new_labels.size() - 1; i >= 0; i--)
1687 Label *label = new_labels.get(i);
1688 // Was in old edit position
1689 if(label->position >= start_seconds1 &&
1690 label->position < start_seconds2)
1692 // Move to new edit position
1693 double position = label->position -
1696 edl->labels->insert_label(position);
1697 new_labels.remove_object_number(i);
1707 if(first_track && edl->session->labels_follow_edits)
1709 edl->labels->optimize();
1713 void Track::align_edits(double start,
1715 ArrayList<double> *times)
1717 int64_t start_units = to_units(start, 0);
1718 int64_t end_units = to_units(end, 0);
1720 // If 1st track with data, times is empty & we need to collect the edit times.
1723 for(Edit *current = edits->first; current; current = NEXT)
1725 if(current->startproject >= start_units &&
1726 current->startproject + current->length <= end_units)
1728 times->append(from_units(current->startproject));
1733 // All other tracks get silence or cut to align the edits on the times.
1735 int current_time = 0;
1736 for(Edit *current = edits->first;
1737 current && current_time < times->size(); )
1739 if(current->startproject >= start_units &&
1740 current->startproject + current->length <= end_units)
1742 int64_t desired_startunits = to_units(times->get(current_time), 0);
1743 int64_t current_startunits = current->startproject;
1747 if(current_startunits < desired_startunits)
1749 //printf("Track::align_edits %d\n", __LINE__);
1750 edits->paste_silence(current_startunits,
1751 desired_startunits);
1752 shift_keyframes(current_startunits,
1753 desired_startunits - current_startunits);
1756 if(current_startunits > desired_startunits)
1758 edits->clear(desired_startunits,
1759 current_startunits);
1760 if(edl->session->autos_follow_edits)
1761 shift_keyframes(desired_startunits,
1762 current_startunits - desired_startunits);
1777 int Track::purge_asset(Asset *asset)
1782 int Track::asset_used(Asset *asset)
1787 for(current_edit = edits->first; current_edit; current_edit = current_edit->next)
1789 if(current_edit->asset == asset)
1797 int Track::is_playable(int64_t position, int direction)
1803 int Track::plugin_used(int64_t position, int64_t direction)
1805 //printf("Track::plugin_used 1 %d\n", this->plugin_set.total);
1806 for(int i = 0; i < this->plugin_set.total; i++)
1808 Plugin *current_plugin = get_current_plugin(position,
1814 //printf("Track::plugin_used 2 %p %d %d\n", current_plugin, current_plugin->on, current_plugin->plugin_type);
1815 if(current_plugin &&
1816 current_plugin->on &&
1817 current_plugin->plugin_type != PLUGIN_NONE)
1822 //printf("Track::plugin_used 3 %p\n", current_plugin);
1826 // Audio is always rendered through VConsole
1827 int Track::direct_copy_possible(int64_t start, int direction, int use_nudge)
1832 int64_t Track::to_units(double position, int round)
1834 return (int64_t)position;
1837 double Track::to_doubleunits(double position)
1842 double Track::from_units(int64_t position)
1844 return (double)position;
1847 int Track::plugin_exists(Plugin *plugin)
1849 for(int number = 0; number < plugin_set.size(); number++)
1851 PluginSet *ptr = plugin_set.get(number);
1852 for(Plugin *current_plugin = (Plugin*)ptr->first;
1854 current_plugin = (Plugin*)current_plugin->next)
1856 if(current_plugin == plugin) return 1;
1860 for(Edit *current = edits->first; current; current = NEXT)
1862 if(current->transition &&
1863 (Plugin*)current->transition == plugin) return 1;