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 if(length > total_length) total_length = length;
253 length = from_units(automation->get_length());
254 if(length > total_length) total_length = length;
260 int Track::has_speed()
262 FloatAutos *autos = (FloatAutos*)automation->autos[AUTOMATION_SPEED];
267 for(FloatAuto *current = (FloatAuto*)autos->first;
269 current = (FloatAuto*)current->next)
271 if(!EQUIV(current->get_value(), 1.0) ||
272 !EQUIV(current->get_control_in_value(), 0.0) ||
273 !EQUIV(current->get_control_out_value(), 0.0))
286 void Track::get_source_dimensions(double position, int &w, int &h)
288 int64_t native_position = to_units(position, 0);
289 for(Edit *current = edits->first; current; current = NEXT)
291 if(current->startproject <= native_position &&
292 current->startproject + current->length > native_position &&
295 w = current->asset->width;
296 h = current->asset->height;
303 int64_t Track::horizontal_span()
305 return (int64_t)(get_length() *
306 edl->session->sample_rate /
307 edl->local_session->zoom_sample +
312 int Track::load(FileXML *file, int track_offset, uint32_t load_flags)
315 int current_plugin = 0;
318 record = file->tag.get_property("RECORD", record);
319 play = file->tag.get_property("PLAY", play);
320 gang = file->tag.get_property("GANG", gang);
321 draw = file->tag.get_property("DRAW", draw);
322 nudge = file->tag.get_property("NUDGE", nudge);
323 expand_view = file->tag.get_property("EXPAND", expand_view);
324 track_w = file->tag.get_property("TRACK_W", track_w);
325 track_h = file->tag.get_property("TRACK_H", track_h);
327 load_header(file, load_flags);
330 result = file->read_tag();
334 if(file->tag.title_is("/TRACK"))
339 if(file->tag.title_is("TITLE"))
341 file->read_text_until("/TITLE", title, BCTEXTLEN);
344 if(load_flags && automation->load(file)
345 /* strstr(file->tag.get_title(), "AUTOS") */)
350 if(file->tag.title_is("EDITS"))
352 if(load_flags & LOAD_EDITS)
353 edits->load(file, track_offset);
356 if(file->tag.title_is("PLUGINSET"))
358 if(load_flags & LOAD_EDITS)
360 PluginSet *plugin_set = new PluginSet(edl, this);
361 this->plugin_set.append(plugin_set);
362 plugin_set->load(file, load_flags);
365 if(load_flags & LOAD_AUTOMATION)
367 if(current_plugin < this->plugin_set.total)
369 PluginSet *plugin_set = this->plugin_set.values[current_plugin];
370 plugin_set->load(file, load_flags);
376 load_derived(file, load_flags);
385 void Track::insert_asset(Asset *asset,
391 edits->insert_asset(asset,
394 to_units(position, 0),
400 // Default keyframes: We don't replace default keyframes in pasting but
401 // when inserting the first EDL of a load operation we need to replace
402 // the default keyframes.
404 // Plugins: This is an arbitrary behavior
406 // 1) No plugin in source track: Paste silence into destination
408 // 2) Plugin in source track: plugin in source track is inserted into
409 // existing destination track plugin sets, new sets being added when
412 void Track::insert_track(Track *track,
419 // Calculate minimum length of data to pad.
420 int64_t min_length = to_units(
421 MAX(edl_length, track->get_length()),
423 //printf("Track::insert_track %d %s %jd\n", __LINE__, title, min_length);
425 // Decide whether to copy settings based on load_mode
426 if(replace_default) copy_settings(track);
428 edits->insert_edits(track->edits,
429 to_units(position, 0),
434 insert_plugin_set(track,
435 to_units(position, 0),
440 automation->insert_track(track->automation,
441 to_units(position, 0),
449 // Called by insert_track
450 void Track::insert_plugin_set(Track *track,
455 // Extend plugins if no incoming plugins
456 if(!track->plugin_set.total)
458 shift_effects(position,
463 for(int i = 0; i < track->plugin_set.total; i++)
465 if(i >= plugin_set.total)
466 plugin_set.append(new PluginSet(edl, this));
468 plugin_set.values[i]->insert_edits(track->plugin_set.values[i],
476 Plugin* Track::insert_effect(const char *title,
477 SharedLocation *shared_location,
478 KeyFrame *default_keyframe,
479 PluginSet *plugin_set,
486 plugin_set = new PluginSet(edl, this);
487 this->plugin_set.append(plugin_set);
492 // Position is identical to source plugin
493 if(plugin_type == PLUGIN_SHAREDPLUGIN)
495 Track *source_track = tracks->get_item_number(shared_location->module);
498 Plugin *source_plugin = source_track->get_current_plugin(
499 edl->local_session->get_selectionstart(),
500 shared_location->plugin,
505 // From an attach operation
508 plugin = plugin_set->insert_plugin(title,
509 source_plugin->startproject,
510 source_plugin->length,
517 // From a drag operation
519 plugin = plugin_set->insert_plugin(title,
531 // This should be done in the caller
534 if(edl->local_session->get_selectionend() >
535 edl->local_session->get_selectionstart())
537 start = edl->local_session->get_selectionstart();
538 length = edl->local_session->get_selectionend() - start;
543 length = get_length();
546 //printf("Track::insert_effect %f %f %d %d\n", start, length, to_units(start, 0),
547 // to_units(length, 0));
549 plugin = plugin_set->insert_plugin(title,
557 //printf("Track::insert_effect 2 %f %f\n", start, length);
563 void Track::move_plugins_up(PluginSet *plugin_set)
565 for(int i = 0; i < this->plugin_set.total; i++)
567 if(this->plugin_set.values[i] == plugin_set)
571 PluginSet *temp = this->plugin_set.values[i - 1];
572 this->plugin_set.values[i - 1] = this->plugin_set.values[i];
573 this->plugin_set.values[i] = temp;
575 SharedLocation old_location, new_location;
576 new_location.module = old_location.module = tracks->number_of(this);
577 old_location.plugin = i;
578 new_location.plugin = i - 1;
579 tracks->change_plugins(old_location, new_location, 1);
585 void Track::move_plugins_down(PluginSet *plugin_set)
587 for(int i = 0; i < this->plugin_set.total; i++)
589 if(this->plugin_set.values[i] == plugin_set)
591 if(i == this->plugin_set.total - 1) break;
593 PluginSet *temp = this->plugin_set.values[i + 1];
594 this->plugin_set.values[i + 1] = this->plugin_set.values[i];
595 this->plugin_set.values[i] = temp;
597 SharedLocation old_location, new_location;
598 new_location.module = old_location.module = tracks->number_of(this);
599 old_location.plugin = i;
600 new_location.plugin = i + 1;
601 tracks->change_plugins(old_location, new_location, 1);
608 void Track::remove_asset(Indexable *asset)
610 for(Edit *edit = edits->first; edit; edit = edit->next)
612 if(asset->is_asset &&
614 edit->asset == (Asset*)asset)
619 if(!asset->is_asset &&
621 edit->nested_edl == (EDL*)asset)
623 edit->nested_edl = 0;
629 void Track::remove_pluginset(PluginSet *plugin_set)
632 for(i = 0; i < this->plugin_set.total; i++)
633 if(plugin_set == this->plugin_set.values[i]) break;
635 this->plugin_set.remove_object(plugin_set);
636 for(i++ ; i < this->plugin_set.total; i++)
638 SharedLocation old_location, new_location;
639 new_location.module = old_location.module = tracks->number_of(this);
640 old_location.plugin = i;
641 new_location.plugin = i - 1;
642 tracks->change_plugins(old_location, new_location, 0);
646 void Track::shift_keyframes(int64_t position, int64_t length)
648 automation->paste_silence(position, position + length);
649 // Effect keyframes are shifted in shift_effects
652 void Track::shift_effects(int64_t position, int64_t length, int edit_autos)
654 for(int i = 0; i < plugin_set.total; i++)
656 plugin_set.values[i]->shift_effects(position, length, edit_autos);
660 void Track::detach_effect(Plugin *plugin)
662 //printf("Track::detach_effect 1\n");
663 for(int i = 0; i < plugin_set.total; i++)
665 PluginSet *plugin_set = this->plugin_set.values[i];
666 for(Plugin *dest = (Plugin*)plugin_set->first;
668 dest = (Plugin*)dest->next)
672 int64_t start = plugin->startproject;
673 int64_t end = plugin->startproject + plugin->length;
675 plugin_set->clear(start, end, 1);
677 //printf("Track::detach_effect 2 %d\n", plugin_set->length());
678 // Delete 0 length pluginsets
685 void Track::resample(double old_rate, double new_rate)
687 edits->resample(old_rate, new_rate);
688 automation->resample(old_rate, new_rate);
689 for(int i = 0; i < plugin_set.total; i++)
690 plugin_set.values[i]->resample(old_rate, new_rate);
691 nudge = (int64_t)(nudge * new_rate / old_rate);
694 void Track::detach_shared_effects(int module)
696 for(int i = 0; i < plugin_set.size(); i++) {
697 PluginSet *plugin_set = this->plugin_set.get(i);
698 for(Plugin *dest = (Plugin*)plugin_set->first; dest; ) {
699 if( (dest->plugin_type == PLUGIN_SHAREDPLUGIN ||
700 dest->plugin_type == PLUGIN_SHAREDMODULE) &&
701 dest->shared_location.module == module ) {
702 int64_t start = dest->startproject;
703 int64_t end = dest->startproject + dest->length;
704 plugin_set->clear(start, end, 1);
707 if(dest) dest = (Plugin*)dest->next;
714 void Track::optimize()
717 for(int i = 0; i < plugin_set.total; ) {
718 PluginSet *plugin_set = this->plugin_set.values[i];
719 plugin_set->optimize();
720 //printf("Track::optimize %d\n", plugin_set.values[i]->total());
721 // new definition of empty track...
722 if( !plugin_set->last ||
723 (plugin_set->last == plugin_set->first &&
724 plugin_set->last->silence()) ) {
725 remove_pluginset(plugin_set);
732 Plugin* Track::get_current_plugin(double position,
739 if(convert_units) position = to_units(position, 0);
740 if(use_nudge) position += nudge;
742 if(plugin_set >= this->plugin_set.total || plugin_set < 0) return 0;
744 //printf("Track::get_current_plugin 1 %d %d %d\n", position, this->plugin_set.total, direction);
745 if(direction == PLAY_FORWARD)
747 for(current = (Plugin*)this->plugin_set.values[plugin_set]->last;
749 current = (Plugin*)PREVIOUS)
751 // printf("Track::get_current_plugin 2 %d %ld %ld\n",
752 // current->startproject,
753 // current->startproject + current->length,
755 if(current->startproject <= position &&
756 current->startproject + current->length > position)
763 if(direction == PLAY_REVERSE)
765 for(current = (Plugin*)this->plugin_set.values[plugin_set]->first;
767 current = (Plugin*)NEXT)
769 if(current->startproject < position &&
770 current->startproject + current->length >= position)
780 Plugin* Track::get_current_transition(double position,
787 if(convert_units) position = to_units(position, 0);
788 if(use_nudge) position += nudge;
790 if(direction == PLAY_FORWARD)
792 for(current = edits->last; current; current = PREVIOUS)
794 if(current->startproject <= position && current->startproject + current->length > position)
796 //printf("Track::get_current_transition %p\n", current->transition);
797 if(current->transition && position < current->startproject + current->transition->length)
799 result = current->transition;
806 if(direction == PLAY_REVERSE)
808 for(current = edits->first; current; current = NEXT)
810 if(current->startproject < position && current->startproject + current->length >= position)
812 if(current->transition && position <= current->startproject + current->transition->length)
814 result = current->transition;
824 void Track::synchronize_params(Track *track)
826 for(Edit *this_edit = edits->first, *that_edit = track->edits->first;
827 this_edit && that_edit;
828 this_edit = this_edit->next, that_edit = that_edit->next)
830 this_edit->synchronize_params(that_edit);
833 for(int i = 0; i < plugin_set.total && i < track->plugin_set.total; i++)
834 plugin_set.values[i]->synchronize_params(track->plugin_set.values[i]);
836 automation->copy_from(track->automation);
837 this->nudge = track->nudge;
844 int Track::dump(FILE *fp)
846 fprintf(fp," Data type %d\n", data_type);
847 fprintf(fp," Title %s\n", title);
848 fprintf(fp," Edits:\n");
849 for(Edit* current = edits->first; current; current = NEXT)
853 automation->dump(fp);
854 fprintf(fp," Plugin Sets: %d\n", plugin_set.total);
856 for(int i = 0; i < plugin_set.total; i++)
857 plugin_set.values[i]->dump(fp);
858 //printf("Track::dump 2\n");
882 Track::Track() : ListItem<Track>()
887 // ======================================== accounting
889 int Track::number_of()
891 return tracks->number_of(this);
906 // ================================================= editing
908 int Track::select_auto(AutoConf *auto_conf, int cursor_x, int cursor_y)
913 int Track::move_auto(AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
918 int Track::release_auto()
923 // used for copying automation alone
924 int Track::copy_automation(double selectionstart,
930 int64_t start = to_units(selectionstart, 0);
931 int64_t end = to_units(selectionend, 1);
933 file->tag.set_title("TRACK");
937 file->append_newline();
939 automation->copy(start, end, file, default_only, active_only);
941 if(edl->session->auto_conf->plugins)
943 for(int i = 0; i < plugin_set.total; i++)
946 plugin_set.values[i]->copy_keyframes(start,
954 file->tag.set_title("/TRACK");
956 file->append_newline();
957 file->append_newline();
958 file->append_newline();
959 file->append_newline();
964 int Track::paste_automation(double selectionstart,
972 // Only used for pasting automation alone.
977 int current_pluginset;
979 if(data_type == TRACK_AUDIO)
980 scale = edl->session->sample_rate / sample_rate;
982 scale = edl->session->frame_rate / frame_rate;
984 total_length *= scale;
985 start = to_units(selectionstart, 0);
986 length = to_units(total_length, 1);
988 current_pluginset = 0;
989 //printf("Track::paste_automation 1\n");
993 result = file->read_tag();
997 if(file->tag.title_is("/TRACK"))
1000 if(automation->paste(start, length, scale, file,
1001 default_only, active_only, 0)) {}
1002 else if(file->tag.title_is("PLUGINSET")) {
1003 if(current_pluginset < plugin_set.total) {
1004 plugin_set.values[current_pluginset]->
1005 paste_keyframes(start, length, file,
1006 default_only, active_only);
1007 current_pluginset++;
1017 void Track::clear_automation(double selectionstart,
1018 double selectionend,
1022 int64_t start = to_units(selectionstart, 0);
1023 int64_t end = to_units(selectionend, 1);
1025 automation->clear(start, end, edl->session->auto_conf, 0);
1027 if(edl->session->auto_conf->plugins)
1029 for(int i = 0; i < plugin_set.total; i++)
1031 plugin_set.values[i]->clear_keyframes(start, end);
1037 void Track::set_automation_mode(double selectionstart,
1038 double selectionend,
1041 int64_t start = to_units(selectionstart, 0);
1042 int64_t end = to_units(selectionend, 1);
1044 automation->set_automation_mode(start, end, mode, edl->session->auto_conf);
1050 int Track::copy(double start,
1053 const char *output_path)
1055 // Use a copy of the selection in converted units
1056 // So copy_automation doesn't reconvert.
1057 int64_t start_unit = to_units(start, 0);
1058 int64_t end_unit = to_units(end, 1);
1063 file->tag.set_title("TRACK");
1064 file->tag.set_property("RECORD", record);
1065 file->tag.set_property("NUDGE", nudge);
1066 file->tag.set_property("PLAY", play);
1067 file->tag.set_property("GANG", gang);
1068 file->tag.set_property("DRAW", draw);
1069 file->tag.set_property("EXPAND", expand_view);
1070 file->tag.set_property("TRACK_W", track_w);
1071 file->tag.set_property("TRACK_H", track_h);
1074 file->append_newline();
1077 file->tag.set_title("TITLE");
1079 file->append_text(title);
1080 file->tag.set_title("/TITLE");
1082 file->append_newline();
1084 // if(data_type == TRACK_AUDIO)
1085 // file->tag.set_property("TYPE", "AUDIO");
1087 // file->tag.set_property("TYPE", "VIDEO");
1089 // file->append_tag();
1090 // file->append_newline();
1092 edits->copy(start_unit, end_unit, file, output_path);
1095 auto_conf.set_all(1);
1096 automation->copy(start_unit, end_unit, file, 0, 0);
1099 for(int i = 0; i < plugin_set.total; i++)
1101 plugin_set.values[i]->copy(start_unit, end_unit, file);
1104 copy_derived(start_unit, end_unit, file);
1106 file->tag.set_title("/TRACK");
1108 file->append_newline();
1109 file->append_newline();
1110 file->append_newline();
1111 file->append_newline();
1116 int Track::copy_assets(double start,
1118 ArrayList<Asset*> *asset_list)
1122 start = to_units(start, 0);
1123 end = to_units(end, 1);
1125 Edit *current = edits->editof((int64_t)start, PLAY_FORWARD, 0);
1128 while(current && current->startproject < end)
1130 // Check for duplicate assets
1133 for(i = 0, result = 0; i < asset_list->total; i++)
1135 if(asset_list->values[i] == current->asset) result = 1;
1137 // append pointer to new asset
1138 if(!result) asset_list->append(current->asset);
1151 int Track::clear(double start,
1160 // Edits::move_auto calls this routine after the units are converted to the track
1162 //printf("Track::clear 1 %d %d %d\n", edit_edits, edit_labels, edit_plugins);
1165 start = to_units(start, 0);
1166 end = to_units(end, 1);
1171 automation->clear((int64_t)start, (int64_t)end, 0, 1);
1175 for(int i = 0; i < plugin_set.total; i++)
1177 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1178 plugin_set.values[i]->clear((int64_t)start, (int64_t)end, edit_autos);
1183 edits->clear((int64_t)start, (int64_t)end);
1187 int Track::clear_handle(double start,
1194 edits->clear_handle(start, end, clear_plugins, edit_autos, distance);
1198 int Track::popup_transition(int cursor_x, int cursor_y)
1205 int Track::modify_edithandles(double oldposition,
1213 edits->modify_handles(oldposition,
1227 int Track::modify_pluginhandles(double oldposition,
1235 for(int i = 0; i < plugin_set.total; i++)
1237 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1238 plugin_set.values[i]->modify_handles(oldposition,
1242 // Don't allow plugin tweeks to affect edits.
1253 int Track::paste_silence(double start, double end, int edit_plugins, int edit_autos)
1255 int64_t start_i = to_units(start, 0);
1256 int64_t end_i = to_units(end, 1);
1258 edits->paste_silence(start_i, end_i);
1259 if(edit_autos) shift_keyframes(start_i, end_i - start_i);
1260 if(edit_plugins) shift_effects(start_i, end_i - start_i, edit_autos);
1266 int Track::select_edit(int cursor_x,
1274 int Track::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
1279 void Track::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
1281 for(int i = 0; i < plugin_set.total; i++)
1283 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1285 plugin = (Plugin*)plugin->next)
1287 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN)
1289 if(plugin->shared_location == old_location)
1290 plugin->shared_location = new_location;
1292 if(do_swap && plugin->shared_location == new_location)
1293 plugin->shared_location = old_location;
1299 void Track::change_modules(int old_location, int new_location, int do_swap)
1301 for(int i = 0; i < plugin_set.total; i++)
1303 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1305 plugin = (Plugin*)plugin->next)
1307 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN ||
1308 plugin->plugin_type == PLUGIN_SHAREDMODULE)
1310 if(plugin->shared_location.module == old_location)
1311 plugin->shared_location.module = new_location;
1313 if(do_swap && plugin->shared_location.module == new_location)
1314 plugin->shared_location.module = old_location;
1321 int Track::playable_edit(int64_t position, int direction)
1324 if(direction == PLAY_REVERSE) position--;
1325 for(Edit *current = edits->first; current && !result; current = NEXT)
1327 if(current->startproject <= position &&
1328 current->startproject + current->length > position)
1330 //printf("Track::playable_edit %p %p\n", current->transition, current->asset);
1331 if(current->transition ||
1333 current->nested_edl) result = 1;
1340 int Track::need_edit(Edit *current, int test_transitions)
1342 return ((test_transitions && current->transition) ||
1343 (!test_transitions && current->asset));
1346 int64_t Track::plugin_change_duration(int64_t input_position,
1347 int64_t input_length,
1351 if(use_nudge) input_position += nudge;
1352 for(int i = 0; i < plugin_set.total; i++)
1354 int64_t new_duration = plugin_set.values[i]->plugin_change_duration(
1358 if(new_duration < input_length) input_length = new_duration;
1360 return input_length;
1363 int64_t Track::edit_change_duration(int64_t input_position,
1364 int64_t input_length,
1366 int test_transitions,
1370 int64_t edit_length = input_length;
1371 if(use_nudge) input_position += nudge;
1375 // ================================= Reverse playback
1376 // Get first edit on or after position
1377 for(current = edits->first;
1378 current && current->startproject + current->length <= input_position;
1384 if(current->startproject > input_position)
1386 // Before first edit
1390 if(need_edit(current, test_transitions))
1392 // Over an edit of interest.
1393 if(input_position - current->startproject < input_length)
1394 edit_length = input_position - current->startproject + 1;
1398 // Over an edit that isn't of interest.
1399 // Search for next edit of interest.
1400 for(current = PREVIOUS ;
1402 current->startproject + current->length > input_position - input_length &&
1403 !need_edit(current, test_transitions);
1408 need_edit(current, test_transitions) &&
1409 current->startproject + current->length > input_position - input_length)
1410 edit_length = input_position - current->startproject - current->length + 1;
1415 // Not over an edit. Try the last edit.
1416 current = edits->last;
1418 ((test_transitions && current->transition) ||
1419 (!test_transitions && current->asset)))
1420 edit_length = input_position - edits->length() + 1;
1425 // =================================== forward playback
1426 // Get first edit on or before position
1427 for(current = edits->last;
1428 current && current->startproject > input_position;
1434 if(current->startproject + current->length <= input_position)
1436 // Beyond last edit.
1440 if(need_edit(current, test_transitions))
1442 // Over an edit of interest.
1443 // Next edit is going to require a change.
1444 if(current->length + current->startproject - input_position < input_length)
1445 edit_length = current->startproject + current->length - input_position;
1449 // Over an edit that isn't of interest.
1450 // Search for next edit of interest.
1451 for(current = NEXT ;
1453 current->startproject < input_position + input_length &&
1454 !need_edit(current, test_transitions);
1459 need_edit(current, test_transitions) &&
1460 current->startproject < input_position + input_length)
1461 edit_length = current->startproject - input_position;
1466 // Not over an edit. Try the first edit.
1467 current = edits->first;
1469 ((test_transitions && current->transition) ||
1470 (!test_transitions && current->asset)))
1471 edit_length = edits->first->startproject - input_position;
1475 if(edit_length < input_length)
1478 return input_length;
1481 void Track::shuffle_edits(double start, double end, int first_track)
1483 ArrayList<Edit*> new_edits;
1484 ArrayList<Label*> new_labels;
1485 int64_t start_units = to_units(start, 0);
1486 int64_t end_units = to_units(end, 0);
1487 // Sample range of all edits selected
1488 //int64_t total_start_units = 0;
1489 //int64_t total_end_units = 0;
1490 // Edit before range
1491 Edit *start_edit = 0;
1492 int have_start_edit = 0;
1494 // Move all edit pointers to list
1495 for(Edit *current = edits->first;
1498 if(current->startproject >= start_units &&
1499 current->startproject + current->length <= end_units)
1501 if(!have_start_edit) start_edit = current->previous;
1502 have_start_edit = 1;
1503 //total_start_units = current->startproject;
1504 //total_end_units = current->startproject + current->length;
1505 new_edits.append(current);
1507 // Move label pointers
1508 if(first_track && edl->session->labels_follow_edits)
1510 double start_seconds = from_units(current->startproject);
1511 double end_seconds = from_units(current->startproject +
1513 for(Label *label = edl->labels->first;
1515 label = label->next)
1517 if(label->position >= start_seconds &&
1518 label->position < end_seconds)
1520 new_labels.append(label);
1521 edl->labels->remove_pointer(label);
1526 // Remove edit pointer
1527 Edit *previous = current;
1529 edits->remove_pointer(previous);
1537 // Insert pointers in random order
1538 while(new_edits.size())
1540 int index = rand() % new_edits.size();
1541 Edit *edit = new_edits.get(index);
1542 new_edits.remove_number(index);
1543 edits->insert_after(start_edit, edit);
1546 // Recalculate start position
1547 // Save old position for moving labels
1548 int64_t startproject1 = edit->startproject;
1549 int64_t startproject2 = 0;
1552 edit->startproject =
1554 edit->previous->startproject + edit->previous->length;
1558 edit->startproject = startproject2 = 0;
1562 // Insert label pointers
1563 if(first_track && edl->session->labels_follow_edits)
1565 double start_seconds1 = from_units(startproject1);
1566 double start_seconds2 = from_units(startproject2);
1567 //double end_seconds1 = from_units(edit->startproject +
1569 for(int i = new_labels.size() - 1; i >= 0; i--)
1571 Label *label = new_labels.get(i);
1572 // Was in old edit position
1573 if(label->position >= start_seconds1 &&
1574 label->position < start_seconds2)
1576 // Move to new edit position
1577 double position = label->position -
1580 edl->labels->insert_label(position);
1581 new_labels.remove_object_number(i);
1591 if(first_track && edl->session->labels_follow_edits)
1593 edl->labels->optimize();
1597 // exactly the same as shuffle_edits except for 1 line
1598 void Track::reverse_edits(double start, double end, int first_track)
1600 ArrayList<Edit*> new_edits;
1601 ArrayList<Label*> new_labels;
1602 int64_t start_units = to_units(start, 0);
1603 int64_t end_units = to_units(end, 0);
1604 // Sample range of all edits selected
1605 //int64_t total_start_units = 0;
1606 //int64_t total_end_units = 0;
1607 // Edit before range
1608 Edit *start_edit = 0;
1609 int have_start_edit = 0;
1611 // Move all edit pointers to list
1612 for(Edit *current = edits->first; current; )
1614 if(current->startproject >= start_units &&
1615 current->startproject + current->length <= end_units)
1617 if(!have_start_edit) start_edit = current->previous;
1618 have_start_edit = 1;
1619 //total_start_units = current->startproject;
1620 //total_end_units = current->startproject + current->length;
1621 new_edits.append(current);
1623 // Move label pointers
1624 if(first_track && edl->session->labels_follow_edits)
1626 double start_seconds = from_units(current->startproject);
1627 double end_seconds = from_units(current->startproject +
1629 for(Label *label = edl->labels->first;
1631 label = label->next)
1633 if(label->position >= start_seconds &&
1634 label->position < end_seconds)
1636 new_labels.append(label);
1637 edl->labels->remove_pointer(label);
1642 // Remove edit pointer
1643 Edit *previous = current;
1645 edits->remove_pointer(previous);
1653 // Insert pointers in reverse order
1654 while(new_edits.size())
1656 int index = new_edits.size() - 1;
1657 Edit *edit = new_edits.get(index);
1658 new_edits.remove_number(index);
1659 edits->insert_after(start_edit, edit);
1662 // Recalculate start position
1663 // Save old position for moving labels
1664 int64_t startproject1 = edit->startproject;
1665 int64_t startproject2 = 0;
1668 edit->startproject =
1670 edit->previous->startproject + edit->previous->length;
1674 edit->startproject = startproject2 = 0;
1678 // Insert label pointers
1679 if(first_track && edl->session->labels_follow_edits)
1681 double start_seconds1 = from_units(startproject1);
1682 double start_seconds2 = from_units(startproject2);
1683 //double end_seconds1 = from_units(edit->startproject + edit->length);
1684 for(int i = new_labels.size() - 1; i >= 0; i--)
1686 Label *label = new_labels.get(i);
1687 // Was in old edit position
1688 if(label->position >= start_seconds1 &&
1689 label->position < start_seconds2)
1691 // Move to new edit position
1692 double position = label->position -
1695 edl->labels->insert_label(position);
1696 new_labels.remove_object_number(i);
1706 if(first_track && edl->session->labels_follow_edits)
1708 edl->labels->optimize();
1712 void Track::align_edits(double start,
1714 ArrayList<double> *times)
1716 int64_t start_units = to_units(start, 0);
1717 int64_t end_units = to_units(end, 0);
1719 // If 1st track with data, times is empty & we need to collect the edit times.
1722 for(Edit *current = edits->first; current; current = NEXT)
1724 if(current->startproject >= start_units &&
1725 current->startproject + current->length <= end_units)
1727 times->append(from_units(current->startproject));
1732 // All other tracks get silence or cut to align the edits on the times.
1734 int current_time = 0;
1735 for(Edit *current = edits->first;
1736 current && current_time < times->size(); )
1738 if(current->startproject >= start_units &&
1739 current->startproject + current->length <= end_units)
1741 int64_t desired_startunits = to_units(times->get(current_time), 0);
1742 int64_t current_startunits = current->startproject;
1746 if(current_startunits < desired_startunits)
1748 //printf("Track::align_edits %d\n", __LINE__);
1749 edits->paste_silence(current_startunits,
1750 desired_startunits);
1751 shift_keyframes(current_startunits,
1752 desired_startunits - current_startunits);
1755 if(current_startunits > desired_startunits)
1757 edits->clear(desired_startunits,
1758 current_startunits);
1759 if(edl->session->autos_follow_edits)
1760 shift_keyframes(desired_startunits,
1761 current_startunits - desired_startunits);
1776 int Track::purge_asset(Asset *asset)
1781 int Track::asset_used(Asset *asset)
1786 for(current_edit = edits->first; current_edit; current_edit = current_edit->next)
1788 if(current_edit->asset == asset)
1796 int Track::is_playable(int64_t position, int direction)
1802 int Track::plugin_used(int64_t position, int64_t direction)
1804 //printf("Track::plugin_used 1 %d\n", this->plugin_set.total);
1805 for(int i = 0; i < this->plugin_set.total; i++)
1807 Plugin *current_plugin = get_current_plugin(position,
1813 //printf("Track::plugin_used 2 %p %d %d\n", current_plugin, current_plugin->on, current_plugin->plugin_type);
1814 if(current_plugin &&
1815 current_plugin->on &&
1816 current_plugin->plugin_type != PLUGIN_NONE)
1821 //printf("Track::plugin_used 3 %p\n", current_plugin);
1825 // Audio is always rendered through VConsole
1826 int Track::direct_copy_possible(int64_t start, int direction, int use_nudge)
1831 int64_t Track::to_units(double position, int round)
1833 return (int64_t)position;
1836 double Track::to_doubleunits(double position)
1841 double Track::from_units(int64_t position)
1843 return (double)position;
1846 int Track::plugin_exists(Plugin *plugin)
1848 for(int number = 0; number < plugin_set.size(); number++)
1850 PluginSet *ptr = plugin_set.get(number);
1851 for(Plugin *current_plugin = (Plugin*)ptr->first;
1853 current_plugin = (Plugin*)current_plugin->next)
1855 if(current_plugin == plugin) return 1;
1859 for(Edit *current = edits->first; current; current = NEXT)
1861 if(current->transition &&
1862 (Plugin*)current->transition == plugin) return 1;