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;
76 plugin_set.remove_all_objects();
79 void Track::create_objects()
84 int Track::copy_settings(Track *track)
86 this->expand_view = track->expand_view;
87 this->draw = track->draw;
88 this->gang = track->gang;
89 this->record = track->record;
90 this->nudge = track->nudge;
91 this->mixer_id = track->mixer_id;
92 this->play = track->play;
93 this->track_w = track->track_w;
94 this->track_h = track->track_h;
95 strcpy(this->title, track->title);
105 int Track::load_defaults(BC_Hash *defaults)
110 void Track::equivalent_output(Track *track, double *result)
112 if(data_type != track->data_type ||
113 track_w != track->track_w ||
114 track_h != track->track_h ||
115 play != track->play ||
116 nudge != track->nudge)
119 // Convert result to track units
120 int64_t result2 = -1;
121 automation->equivalent_output(track->automation, &result2);
122 edits->equivalent_output(track->edits, &result2);
124 int plugin_sets = MIN(plugin_set.total, track->plugin_set.total);
125 // Test existing plugin sets
126 for(int i = 0; i < plugin_sets; i++)
128 plugin_set.values[i]->equivalent_output(
129 track->plugin_set.values[i],
133 // New EDL has more plugin sets. Get starting plugin in new plugin sets
134 for(int i = plugin_sets; i < plugin_set.total; i++)
136 Plugin *current = plugin_set.values[i]->get_first_plugin();
139 if(result2 < 0 || current->startproject < result2)
140 result2 = current->startproject;
144 // New EDL has fewer plugin sets. Get starting plugin in old plugin set
145 for(int i = plugin_sets; i < track->plugin_set.total; i++)
147 Plugin *current = track->plugin_set.values[i]->get_first_plugin();
150 if(result2 < 0 || current->startproject < result2)
151 result2 = current->startproject;
155 // Number of plugin sets differs but somehow we didn't find the start of the
157 if(track->plugin_set.total != plugin_set.total && result2 < 0)
161 (*result < 0 || from_units(result2) < *result))
162 *result = from_units(result2);
166 int Track::is_synthesis(int64_t position,
169 int is_synthesis = 0;
170 for(int i = 0; i < plugin_set.total; i++)
172 Plugin *plugin = get_current_plugin(position,
179 // Assume data from a shared track is synthesized
180 if(plugin->plugin_type == PLUGIN_SHAREDMODULE)
183 is_synthesis = plugin->is_synthesis(position,
186 //printf("Track::is_synthesis %d %d\n", __LINE__, is_synthesis);
187 if(is_synthesis) break;
193 void Track::copy_from(Track *track)
195 copy_settings(track);
196 edits->copy_from(track->edits);
197 for(int i = 0; i < this->plugin_set.total; i++)
198 delete this->plugin_set.values[i];
199 this->plugin_set.remove_all_objects();
201 for(int i = 0; i < track->plugin_set.total; i++)
203 PluginSet *new_plugin_set = plugin_set.append(new PluginSet(edl, this));
204 new_plugin_set->copy_from(track->plugin_set.values[i]);
206 automation->copy_from(track->automation);
207 this->track_w = track->track_w;
208 this->track_h = track->track_h;
211 Track& Track::operator=(Track& track)
213 printf("Track::operator= 1\n");
218 int Track::vertical_span(Theme *theme)
222 result += theme->get_image("title_bg_data")->get_h();
224 result += edl->local_session->zoom_track;
226 result += plugin_set.total * theme->get_image("plugin_bg_data")->get_h();
227 result = MAX(result, theme->title_h);
231 double Track::get_length()
233 double total_length = 0;
239 length = from_units(edits->last->startproject + edits->last->length);
240 if(length > total_length) total_length = length;
244 for(int i = 0; i < plugin_set.total; i++)
246 if( !plugin_set.values[i]->last ) continue;
247 length = from_units(plugin_set.values[i]->last->startproject +
248 plugin_set.values[i]->last->length);
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))
284 int Track::show_assets()
286 return expand_view || edl->session->show_assets ? 1 : 0;
289 int Track::show_titles()
291 return expand_view || edl->session->show_titles ? 1 : 0;
294 int Track::show_transitions()
296 return expand_view || edl->session->auto_conf->transitions ? 1 : 0;
299 void Track::get_source_dimensions(double position, int &w, int &h)
301 int64_t native_position = to_units(position, 0);
302 for(Edit *current = edits->first; current; current = NEXT)
304 if(current->startproject <= native_position &&
305 current->startproject + current->length > native_position &&
308 w = current->asset->width;
309 h = current->asset->height;
316 int64_t Track::horizontal_span()
318 return (int64_t)(get_length() *
319 edl->session->sample_rate /
320 edl->local_session->zoom_sample +
325 int Track::load(FileXML *file, int track_offset, uint32_t load_flags)
328 int current_plugin = 0;
331 record = file->tag.get_property("RECORD", record);
332 play = file->tag.get_property("PLAY", play);
333 gang = file->tag.get_property("GANG", gang);
334 draw = file->tag.get_property("DRAW", draw);
335 nudge = file->tag.get_property("NUDGE", nudge);
336 mixer_id = file->tag.get_property("MIXER_ID", mixer_id);
337 expand_view = file->tag.get_property("EXPAND", expand_view);
338 track_w = file->tag.get_property("TRACK_W", track_w);
339 track_h = file->tag.get_property("TRACK_H", track_h);
341 load_header(file, load_flags);
344 result = file->read_tag();
348 if(file->tag.title_is("/TRACK"))
353 if(file->tag.title_is("TITLE"))
356 file->read_text_until("/TITLE", &data);
357 memset(title, 0, sizeof(title));
358 strncpy(title, data.cstr(), sizeof(title)-1);
361 if(load_flags && automation->load(file)
362 /* strstr(file->tag.get_title(), "AUTOS") */)
367 if(file->tag.title_is("EDITS"))
369 if(load_flags & LOAD_EDITS)
370 edits->load(file, track_offset);
372 result = file->skip_tag();
375 if(file->tag.title_is("PLUGINSET"))
377 if(load_flags & LOAD_EDITS)
379 PluginSet *plugin_set = new PluginSet(edl, this);
380 this->plugin_set.append(plugin_set);
381 plugin_set->load(file, load_flags);
384 if(load_flags & LOAD_AUTOMATION)
386 if(current_plugin < this->plugin_set.total)
388 PluginSet *plugin_set = this->plugin_set.values[current_plugin];
389 plugin_set->load(file, load_flags);
394 result = file->skip_tag();
397 load_derived(file, load_flags);
406 void Track::insert_asset(Asset *asset,
412 edits->insert_asset(asset,
415 to_units(position, 0),
421 // Default keyframes: We don't replace default keyframes in pasting but
422 // when inserting the first EDL of a load operation we need to replace
423 // the default keyframes.
425 // Plugins: This is an arbitrary behavior
427 // 1) No plugin in source track: Paste silence into destination
429 // 2) Plugin in source track: plugin in source track is inserted into
430 // existing destination track plugin sets, new sets being added when
433 void Track::insert_track(Track *track,
440 // Calculate minimum length of data to pad.
441 int64_t min_length = to_units(
442 MAX(edl_length, track->get_length()),
444 //printf("Track::insert_track %d %s %jd\n", __LINE__, title, min_length);
446 // Decide whether to copy settings based on load_mode
447 if(replace_default) copy_settings(track);
449 edits->insert_edits(track->edits,
450 to_units(position, 0),
455 insert_plugin_set(track,
456 to_units(position, 0),
461 automation->insert_track(track->automation,
462 to_units(position, 0),
470 // Called by insert_track
471 void Track::insert_plugin_set(Track *track,
476 // Extend plugins if no incoming plugins
477 if( track->plugin_set.total ) {
478 for(int i = 0; i < track->plugin_set.total; i++) {
479 if(i >= plugin_set.total)
480 plugin_set.append(new PluginSet(edl, this));
482 plugin_set.values[i]->insert_edits(track->plugin_set.values[i],
483 position, min_length, edit_autos);
487 shift_effects(position, min_length, edit_autos, 0);
491 Plugin* Track::insert_effect(const char *title,
492 SharedLocation *shared_location,
493 KeyFrame *default_keyframe,
494 PluginSet *plugin_set,
501 plugin_set = new PluginSet(edl, this);
502 this->plugin_set.append(plugin_set);
507 // Position is identical to source plugin
508 if(plugin_type == PLUGIN_SHAREDPLUGIN)
510 Track *source_track = tracks->get_item_number(shared_location->module);
513 Plugin *source_plugin = source_track->get_current_plugin(
514 edl->local_session->get_selectionstart(1),
515 shared_location->plugin,
520 // From an attach operation
523 plugin = plugin_set->insert_plugin(title,
524 source_plugin->startproject,
525 source_plugin->length,
532 // From a drag operation
534 plugin = plugin_set->insert_plugin(title,
546 // This should be done in the caller
549 if(edl->local_session->get_selectionend() >
550 edl->local_session->get_selectionstart())
552 start = edl->local_session->get_selectionstart();
553 length = edl->local_session->get_selectionend() - start;
558 length = get_length();
561 //printf("Track::insert_effect %f %f %d %d\n", start, length, to_units(start, 0),
562 // to_units(length, 0));
564 plugin = plugin_set->insert_plugin(title,
572 //printf("Track::insert_effect 2 %f %f\n", start, length);
578 void Track::move_plugins_up(PluginSet *plugin_set)
580 for(int i = 0; i < this->plugin_set.total; i++)
582 if(this->plugin_set.values[i] == plugin_set)
586 PluginSet *temp = this->plugin_set.values[i - 1];
587 this->plugin_set.values[i - 1] = this->plugin_set.values[i];
588 this->plugin_set.values[i] = temp;
590 SharedLocation old_location, new_location;
591 new_location.module = old_location.module = tracks->number_of(this);
592 old_location.plugin = i;
593 new_location.plugin = i - 1;
594 tracks->change_plugins(old_location, new_location, 1);
600 void Track::move_plugins_down(PluginSet *plugin_set)
602 for(int i = 0; i < this->plugin_set.total; i++)
604 if(this->plugin_set.values[i] == plugin_set)
606 if(i == this->plugin_set.total - 1) break;
608 PluginSet *temp = this->plugin_set.values[i + 1];
609 this->plugin_set.values[i + 1] = this->plugin_set.values[i];
610 this->plugin_set.values[i] = temp;
612 SharedLocation old_location, new_location;
613 new_location.module = old_location.module = tracks->number_of(this);
614 old_location.plugin = i;
615 new_location.plugin = i + 1;
616 tracks->change_plugins(old_location, new_location, 1);
623 void Track::remove_asset(Indexable *asset)
625 for(Edit *edit = edits->first; edit; edit = edit->next)
627 if(asset->is_asset &&
629 edit->asset == (Asset*)asset)
634 if(!asset->is_asset &&
636 edit->nested_edl == (EDL*)asset)
638 edit->nested_edl = 0;
644 void Track::remove_pluginset(PluginSet *plugin_set)
647 for(i = 0; i < this->plugin_set.total; i++)
648 if(plugin_set == this->plugin_set.values[i]) break;
650 this->plugin_set.remove_object(plugin_set);
651 for(i++ ; i < this->plugin_set.total; i++)
653 SharedLocation old_location, new_location;
654 new_location.module = old_location.module = tracks->number_of(this);
655 old_location.plugin = i;
656 new_location.plugin = i - 1;
657 tracks->change_plugins(old_location, new_location, 0);
661 void Track::shift_keyframes(int64_t position, int64_t length)
663 automation->paste_silence(position, position + length);
664 // Effect keyframes are shifted in shift_effects
667 void Track::shift_effects(int64_t position, int64_t length, int edit_autos, Edits *trim_edits)
669 for( int i=0; i<plugin_set.total; ++i ) {
670 if( !trim_edits || trim_edits == (Edits*)plugin_set.values[i] )
671 plugin_set.values[i]->shift_effects(position, length, edit_autos);
675 void Track::detach_effect(Plugin *plugin)
677 //printf("Track::detach_effect 1\n");
678 for(int i = 0; i < plugin_set.total; i++)
680 PluginSet *plugin_set = this->plugin_set.values[i];
681 for(Plugin *dest = (Plugin*)plugin_set->first;
683 dest = (Plugin*)dest->next)
687 int64_t start = plugin->startproject;
688 int64_t end = plugin->startproject + plugin->length;
690 plugin_set->clear(start, end, 1);
692 //printf("Track::detach_effect 2 %d\n", plugin_set->length());
693 // Delete 0 length pluginsets
700 void Track::resample(double old_rate, double new_rate)
702 edits->resample(old_rate, new_rate);
703 automation->resample(old_rate, new_rate);
704 for(int i = 0; i < plugin_set.total; i++)
705 plugin_set.values[i]->resample(old_rate, new_rate);
706 nudge = (int64_t)(nudge * new_rate / old_rate);
709 void Track::detach_shared_effects(int module)
711 for(int i = 0; i < plugin_set.size(); i++) {
712 PluginSet *plugin_set = this->plugin_set.get(i);
713 for(Plugin *dest = (Plugin*)plugin_set->first; dest; ) {
714 if( (dest->plugin_type == PLUGIN_SHAREDPLUGIN ||
715 dest->plugin_type == PLUGIN_SHAREDMODULE) &&
716 dest->shared_location.module == module ) {
717 int64_t start = dest->startproject;
718 int64_t end = dest->startproject + dest->length;
719 plugin_set->clear(start, end, 1);
722 if(dest) dest = (Plugin*)dest->next;
729 void Track::optimize()
732 for(int i = 0; i < plugin_set.total; ) {
733 PluginSet *plugin_set = this->plugin_set.values[i];
734 plugin_set->optimize();
735 //printf("Track::optimize %d\n", plugin_set.values[i]->total());
736 // new definition of empty track...
737 if( !plugin_set->last ||
738 (plugin_set->last == plugin_set->first &&
739 plugin_set->last->silence()) ) {
740 remove_pluginset(plugin_set);
747 Plugin* Track::get_current_plugin(double position,
754 if(convert_units) position = to_units(position, 0);
755 if(use_nudge) position += nudge;
757 if(plugin_set >= this->plugin_set.total || plugin_set < 0) return 0;
759 //printf("Track::get_current_plugin 1 %d %d %d\n", position, this->plugin_set.total, direction);
760 if(direction == PLAY_FORWARD)
762 for(current = (Plugin*)this->plugin_set.values[plugin_set]->last;
764 current = (Plugin*)PREVIOUS)
766 // printf("Track::get_current_plugin 2 %d %ld %ld\n",
767 // current->startproject,
768 // current->startproject + current->length,
770 if(current->startproject <= position &&
771 current->startproject + current->length > position)
778 if(direction == PLAY_REVERSE)
780 for(current = (Plugin*)this->plugin_set.values[plugin_set]->first;
782 current = (Plugin*)NEXT)
784 if(current->startproject < position &&
785 current->startproject + current->length >= position)
795 Plugin* Track::get_current_transition(double position,
802 if(convert_units) position = to_units(position, 0);
803 if(use_nudge) position += nudge;
805 if(direction == PLAY_FORWARD)
807 for(current = edits->last; current; current = PREVIOUS)
809 if(current->startproject <= position && current->startproject + current->length > position)
811 //printf("Track::get_current_transition %p\n", current->transition);
812 if(current->transition && position < current->startproject + current->transition->length)
814 result = current->transition;
821 if(direction == PLAY_REVERSE)
823 for(current = edits->first; current; current = NEXT)
825 if(current->startproject < position && current->startproject + current->length >= position)
827 if(current->transition && position <= current->startproject + current->transition->length)
829 result = current->transition;
839 void Track::synchronize_params(Track *track)
841 for(Edit *this_edit = edits->first, *that_edit = track->edits->first;
842 this_edit && that_edit;
843 this_edit = this_edit->next, that_edit = that_edit->next)
845 this_edit->synchronize_params(that_edit);
848 for(int i = 0; i < plugin_set.total && i < track->plugin_set.total; i++)
849 plugin_set.values[i]->synchronize_params(track->plugin_set.values[i]);
851 automation->copy_from(track->automation);
852 this->nudge = track->nudge;
859 int Track::dump(FILE *fp)
861 fprintf(fp," Data type %d\n", data_type);
862 fprintf(fp," Title %s\n", title);
863 fprintf(fp," Edits:\n");
864 for(Edit* current = edits->first; current; current = NEXT)
868 automation->dump(fp);
869 fprintf(fp," Plugin Sets: %d\n", plugin_set.total);
871 for(int i = 0; i < plugin_set.total; i++)
872 plugin_set.values[i]->dump(fp);
873 //printf("Track::dump 2\n");
897 Track::Track() : ListItem<Track>()
902 // ======================================== accounting
904 int Track::number_of()
906 return tracks->number_of(this);
921 // ================================================= editing
923 int Track::select_auto(AutoConf *auto_conf, int cursor_x, int cursor_y)
928 int Track::move_auto(AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
933 int Track::release_auto()
938 // used for copying automation alone
939 int Track::copy_automation(double selectionstart,
945 int64_t start = to_units(selectionstart, 0);
946 int64_t end = to_units(selectionend, 1);
948 file->tag.set_title("TRACK");
952 file->append_newline();
954 automation->copy(start, end, file, default_only, active_only);
956 if(edl->session->auto_conf->plugins)
958 for(int i = 0; i < plugin_set.total; i++)
961 plugin_set.values[i]->copy_keyframes(start,
969 file->tag.set_title("/TRACK");
971 file->append_newline();
972 file->append_newline();
973 file->append_newline();
974 file->append_newline();
979 int Track::paste_automation(double selectionstart,
987 // Only used for pasting automation alone.
992 int current_pluginset;
994 if(data_type == TRACK_AUDIO)
995 scale = edl->session->sample_rate / sample_rate;
997 scale = edl->session->frame_rate / frame_rate;
999 total_length *= scale;
1000 start = to_units(selectionstart, 0);
1001 length = to_units(total_length, 1);
1003 current_pluginset = 0;
1004 //printf("Track::paste_automation 1\n");
1008 result = file->read_tag();
1012 if(file->tag.title_is("/TRACK"))
1015 if(automation->paste(start, length, scale, file,
1016 default_only, active_only, 0)) {}
1017 else if(file->tag.title_is("PLUGINSET")) {
1018 if(current_pluginset < plugin_set.total) {
1019 plugin_set.values[current_pluginset]->
1020 paste_keyframes(start, length, file,
1021 default_only, active_only);
1022 current_pluginset++;
1032 void Track::clear_automation(double selectionstart,
1033 double selectionend,
1037 int64_t start = to_units(selectionstart, 0);
1038 int64_t end = to_units(selectionend, 1);
1040 automation->clear(start, end, edl->session->auto_conf, 0);
1042 if(edl->session->auto_conf->plugins)
1044 for(int i = 0; i < plugin_set.total; i++)
1046 plugin_set.values[i]->clear_keyframes(start, end);
1052 void Track::set_automation_mode(double selectionstart,
1053 double selectionend,
1056 int64_t start = to_units(selectionstart, 0);
1057 int64_t end = to_units(selectionend, 1);
1059 automation->set_automation_mode(start, end, mode, edl->session->auto_conf);
1065 int Track::copy(double start,
1068 const char *output_path)
1070 // Use a copy of the selection in converted units
1071 // So copy_automation doesn't reconvert.
1072 int64_t start_unit = to_units(start, 0);
1073 int64_t end_unit = to_units(end, 1);
1078 file->tag.set_title("TRACK");
1079 file->tag.set_property("RECORD", record);
1080 file->tag.set_property("NUDGE", nudge);
1081 file->tag.set_property("MIXER_ID", mixer_id);
1082 file->tag.set_property("PLAY", play);
1083 file->tag.set_property("GANG", gang);
1084 file->tag.set_property("DRAW", draw);
1085 file->tag.set_property("EXPAND", expand_view);
1086 file->tag.set_property("TRACK_W", track_w);
1087 file->tag.set_property("TRACK_H", track_h);
1090 file->append_newline();
1093 file->tag.set_title("TITLE");
1095 file->append_text(title);
1096 file->tag.set_title("/TITLE");
1098 file->append_newline();
1100 // if(data_type == TRACK_AUDIO)
1101 // file->tag.set_property("TYPE", "AUDIO");
1103 // file->tag.set_property("TYPE", "VIDEO");
1105 // file->append_tag();
1106 // file->append_newline();
1108 edits->copy(start_unit, end_unit, file, output_path);
1111 auto_conf.set_all(1);
1112 automation->copy(start_unit, end_unit, file, 0, 0);
1115 for(int i = 0; i < plugin_set.total; i++)
1117 plugin_set.values[i]->copy(start_unit, end_unit, file);
1120 copy_derived(start_unit, end_unit, file);
1122 file->tag.set_title("/TRACK");
1124 file->append_newline();
1125 file->append_newline();
1126 file->append_newline();
1127 file->append_newline();
1132 int Track::copy_assets(double start,
1134 ArrayList<Asset*> *asset_list)
1138 start = to_units(start, 0);
1139 end = to_units(end, 1);
1141 Edit *current = edits->editof((int64_t)start, PLAY_FORWARD, 0);
1144 while(current && current->startproject < end)
1146 // Check for duplicate assets
1149 for(i = 0, result = 0; i < asset_list->total; i++)
1151 if(asset_list->values[i] == current->asset) result = 1;
1153 // append pointer to new asset
1154 if(!result) asset_list->append(current->asset);
1163 int Track::blade(double position)
1165 int64_t start = to_units(position, 0);
1166 Edit *edit = edits->split_edit(start);
1167 if( !edit ) return 1;
1168 edit->hard_left = 1;
1169 if( edit->previous ) edit->previous->hard_right = 1;
1173 int Track::clear(double start, double end,
1174 int edit_edits, int edit_labels, int edit_plugins,
1175 int edit_autos, Edits *trim_edits)
1177 return clear(to_units(start, 0), to_units(end, 1),
1178 edit_edits, edit_labels, edit_plugins, edit_autos, trim_edits);
1181 int Track::clear(int64_t start, int64_t end,
1182 int edit_edits, int edit_labels, int edit_plugins,
1183 int edit_autos, Edits *trim_edits)
1185 //printf("Track::clear 1 %d %d %d\n", edit_edits, edit_labels, edit_plugins);
1187 automation->clear(start, end, 0, 1);
1188 if( edit_plugins ) {
1189 for(int i = 0; i < plugin_set.total; i++) {
1190 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1191 plugin_set.values[i]->clear(start, end, edit_autos);
1195 edits->clear(start, end);
1199 int Track::clear_handle(double start,
1206 edits->clear_handle(start, end, clear_plugins, edit_autos, distance);
1210 int Track::popup_transition(int cursor_x, int cursor_y)
1217 int Track::modify_edithandles(double oldposition, double newposition,
1218 int currentend, int handle_mode, int edit_labels,
1219 int edit_plugins, int edit_autos, int group_id)
1221 edits->modify_handles(oldposition, newposition,
1222 currentend, handle_mode, 1, edit_labels, edit_plugins,
1223 edit_autos, 0, group_id);
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, newposition,
1239 // Don't allow plugin tweeks to affect edits.
1240 currentend, handle_mode, 0, 0, 0, 0, 0, 0);
1246 int Track::paste_silence(double start, double end, int edit_plugins, int edit_autos)
1248 return paste_silence(to_units(start, 0), to_units(end, 1),
1249 edit_plugins, edit_autos);
1252 int Track::paste_silence(int64_t start, int64_t end, int edit_plugins, int edit_autos)
1254 edits->paste_silence(start, end);
1256 shift_keyframes(start, end - start);
1258 shift_effects(start, end - start, edit_autos, 0);
1263 int Track::select_edit(int cursor_x,
1271 int Track::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
1276 void Track::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
1278 for(int i = 0; i < plugin_set.total; i++)
1280 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1282 plugin = (Plugin*)plugin->next)
1284 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN)
1286 if(plugin->shared_location == old_location)
1287 plugin->shared_location = new_location;
1289 if(do_swap && plugin->shared_location == new_location)
1290 plugin->shared_location = old_location;
1296 void Track::change_modules(int old_location, int new_location, int do_swap)
1298 for(int i = 0; i < plugin_set.total; i++)
1300 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1302 plugin = (Plugin*)plugin->next)
1304 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN ||
1305 plugin->plugin_type == PLUGIN_SHAREDMODULE)
1307 if(plugin->shared_location.module == old_location)
1308 plugin->shared_location.module = new_location;
1310 if(do_swap && plugin->shared_location.module == new_location)
1311 plugin->shared_location.module = old_location;
1318 int Track::playable_edit(int64_t position, int direction)
1321 if(direction == PLAY_REVERSE) position--;
1322 for(Edit *current = edits->first; current && !result; current = NEXT)
1324 if(current->startproject <= position &&
1325 current->startproject + current->length > position)
1327 //printf("Track::playable_edit %p %p\n", current->transition, current->asset);
1328 if(current->transition ||
1330 current->nested_edl) result = 1;
1337 int Track::need_edit(Edit *current, int test_transitions)
1339 return ((test_transitions && current->transition) ||
1340 (!test_transitions && current->asset));
1343 int64_t Track::plugin_change_duration(int64_t input_position,
1344 int64_t input_length,
1348 if(use_nudge) input_position += nudge;
1349 for(int i = 0; i < plugin_set.total; i++)
1351 int64_t new_duration = plugin_set.values[i]->plugin_change_duration(
1355 if(new_duration < input_length) input_length = new_duration;
1357 return input_length;
1360 int64_t Track::edit_change_duration(int64_t input_position,
1361 int64_t input_length,
1363 int test_transitions,
1367 int64_t edit_length = input_length;
1368 if(use_nudge) input_position += nudge;
1372 // ================================= Reverse playback
1373 // Get first edit on or after position
1374 for(current = edits->first;
1375 current && current->startproject + current->length <= input_position;
1381 if(current->startproject > input_position)
1383 // Before first edit
1387 if(need_edit(current, test_transitions))
1389 // Over an edit of interest.
1390 if(input_position - current->startproject < input_length)
1391 edit_length = input_position - current->startproject + 1;
1395 // Over an edit that isn't of interest.
1396 // Search for next edit of interest.
1397 for(current = PREVIOUS ;
1399 current->startproject + current->length > input_position - input_length &&
1400 !need_edit(current, test_transitions);
1405 need_edit(current, test_transitions) &&
1406 current->startproject + current->length > input_position - input_length)
1407 edit_length = input_position - current->startproject - current->length + 1;
1412 // Not over an edit. Try the last edit.
1413 current = edits->last;
1415 ((test_transitions && current->transition) ||
1416 (!test_transitions && current->asset)))
1417 edit_length = input_position - edits->length() + 1;
1422 // =================================== forward playback
1423 // Get first edit on or before position
1424 for(current = edits->last;
1425 current && current->startproject > input_position;
1431 if(current->startproject + current->length <= input_position)
1433 // Beyond last edit.
1437 if(need_edit(current, test_transitions))
1439 // Over an edit of interest.
1440 // Next edit is going to require a change.
1441 if(current->length + current->startproject - input_position < input_length)
1442 edit_length = current->startproject + current->length - input_position;
1446 // Over an edit that isn't of interest.
1447 // Search for next edit of interest.
1448 for(current = NEXT ;
1450 current->startproject < input_position + input_length &&
1451 !need_edit(current, test_transitions);
1456 need_edit(current, test_transitions) &&
1457 current->startproject < input_position + input_length)
1458 edit_length = current->startproject - input_position;
1463 // Not over an edit. Try the first edit.
1464 current = edits->first;
1466 ((test_transitions && current->transition) ||
1467 (!test_transitions && current->asset)))
1468 edit_length = edits->first->startproject - input_position;
1472 if(edit_length < input_length)
1475 return input_length;
1478 void Track::shuffle_edits(double start, double end, int first_track)
1480 ArrayList<Edit*> new_edits;
1481 ArrayList<Label*> new_labels;
1482 int64_t start_units = to_units(start, 0);
1483 int64_t end_units = to_units(end, 0);
1484 // Sample range of all edits selected
1485 //int64_t total_start_units = 0;
1486 //int64_t total_end_units = 0;
1487 // Edit before range
1488 Edit *start_edit = 0;
1489 int have_start_edit = 0;
1491 // Move all edit pointers to list
1492 for(Edit *current = edits->first;
1495 if(current->startproject >= start_units &&
1496 current->startproject + current->length <= end_units)
1498 if(!have_start_edit) start_edit = current->previous;
1499 have_start_edit = 1;
1500 //total_start_units = current->startproject;
1501 //total_end_units = current->startproject + current->length;
1502 new_edits.append(current);
1504 // Move label pointers
1505 if(first_track && edl->session->labels_follow_edits)
1507 double start_seconds = from_units(current->startproject);
1508 double end_seconds = from_units(current->startproject +
1510 for(Label *label = edl->labels->first;
1512 label = label->next)
1514 if(label->position >= start_seconds &&
1515 label->position < end_seconds)
1517 new_labels.append(label);
1518 edl->labels->remove_pointer(label);
1523 // Remove edit pointer
1524 Edit *previous = current;
1526 edits->remove_pointer(previous);
1534 // Insert pointers in random order
1535 while(new_edits.size())
1537 int index = rand() % new_edits.size();
1538 Edit *edit = new_edits.get(index);
1539 new_edits.remove_number(index);
1541 edits->insert_before(edits->first, edit);
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 end_seconds1 = from_units(startproject1 + edit->length);
1567 double start_seconds2 = from_units(startproject2);
1568 for(int i = new_labels.size() - 1; i >= 0; i--)
1570 Label *label = new_labels.get(i);
1571 // Was in old edit position
1572 if(label->position >= start_seconds1 &&
1573 label->position < end_seconds1)
1575 // Move to new edit position
1576 double position = label->position -
1577 start_seconds1 + start_seconds2;
1578 edl->labels->insert_label(position);
1579 new_labels.remove_object_number(i);
1589 if(first_track && edl->session->labels_follow_edits)
1591 edl->labels->optimize();
1595 // exactly the same as shuffle_edits except for 1 line
1596 void Track::reverse_edits(double start, double end, int first_track)
1598 ArrayList<Edit*> new_edits;
1599 ArrayList<Label*> new_labels;
1600 int64_t start_units = to_units(start, 0);
1601 int64_t end_units = to_units(end, 0);
1602 // Sample range of all edits selected
1603 //int64_t total_start_units = 0;
1604 //int64_t total_end_units = 0;
1605 // Edit before range
1606 Edit *start_edit = 0;
1607 int have_start_edit = 0;
1609 // Move all edit pointers to list
1610 for(Edit *current = edits->first; current; )
1612 if(current->startproject >= start_units &&
1613 current->startproject + current->length <= end_units)
1615 if(!have_start_edit) start_edit = current->previous;
1616 have_start_edit = 1;
1617 //total_start_units = current->startproject;
1618 //total_end_units = current->startproject + current->length;
1619 new_edits.append(current);
1621 // Move label pointers
1622 if(first_track && edl->session->labels_follow_edits)
1624 double start_seconds = from_units(current->startproject);
1625 double end_seconds = from_units(current->startproject +
1627 for(Label *label = edl->labels->first;
1629 label = label->next)
1631 if(label->position >= start_seconds &&
1632 label->position < end_seconds)
1634 new_labels.append(label);
1635 edl->labels->remove_pointer(label);
1640 // Remove edit pointer
1641 Edit *previous = current;
1643 edits->remove_pointer(previous);
1651 // Insert pointers in reverse order
1652 while(new_edits.size())
1654 int index = new_edits.size() - 1;
1655 Edit *edit = new_edits.get(index);
1656 new_edits.remove_number(index);
1658 edits->insert_before(edits->first, edit);
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 end_seconds1 = from_units(startproject1 + edit->length);
1684 double start_seconds2 = from_units(startproject2);
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 < end_seconds1)
1692 // Move to new edit position
1693 double position = label->position -
1694 start_seconds1 + start_seconds2;
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 int64_t Track::frame_align(int64_t position, int round)
1848 if( data_type != TRACK_VIDEO && edl->session->cursor_on_frames )
1849 position = to_units(edl->align_to_frame(from_units(position), round), round);
1853 int Track::plugin_exists(Plugin *plugin)
1855 for(int number = 0; number < plugin_set.size(); number++)
1857 PluginSet *ptr = plugin_set.get(number);
1858 for(Plugin *current_plugin = (Plugin*)ptr->first;
1860 current_plugin = (Plugin*)current_plugin->next)
1862 if(current_plugin == plugin) return 1;
1866 for(Edit *current = edits->first; current; current = NEXT)
1868 if(current->transition &&
1869 (Plugin*)current->transition == plugin) return 1;
1876 int Track::get_mixer_id()
1878 if( mixer_id < 0 ) {
1880 for( Track *track=tracks->first; track!=0; track=track->next )
1881 if( track->mixer_id > v ) v = track->mixer_id;