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"
41 #include "pluginset.h"
42 #include "mainsession.h"
46 #include "trackcanvas.h"
48 #include "transition.h"
49 #include "transportque.inc"
55 Track::Track(EDL *edl, Tracks *tracks) : ListItem<Track>()
58 this->tracks = tracks;
67 track_w = edl->session->output_w;
68 track_h = edl->session->output_h;
71 masks = (1<<SUBMASKS)-1;
78 plugin_set.remove_all_objects();
81 void Track::create_objects()
86 int Track::copy_settings(Track *track)
88 this->expand_view = track->expand_view;
89 this->draw = track->draw;
90 this->gang = track->gang;
91 this->record = track->record;
92 this->nudge = track->nudge;
93 this->mixer_id = track->mixer_id;
94 this->play = track->play;
95 this->track_w = track->track_w;
96 this->track_h = track->track_h;
97 this->masks = track->masks;
98 strcpy(this->title, track->title);
108 int Track::load_defaults(BC_Hash *defaults)
113 void Track::equivalent_output(Track *track, double *result)
115 if(data_type != track->data_type ||
116 track_w != track->track_w ||
117 track_h != track->track_h ||
118 play != track->play ||
119 nudge != track->nudge)
122 // Convert result to track units
123 int64_t result2 = -1;
124 automation->equivalent_output(track->automation, &result2);
125 edits->equivalent_output(track->edits, &result2);
127 int plugin_sets = MIN(plugin_set.total, track->plugin_set.total);
128 // Test existing plugin sets
129 for(int i = 0; i < plugin_sets; i++)
131 plugin_set.values[i]->equivalent_output(
132 track->plugin_set.values[i],
136 // New EDL has more plugin sets. Get starting plugin in new plugin sets
137 for(int i = plugin_sets; i < plugin_set.total; i++)
139 Plugin *current = plugin_set.values[i]->get_first_plugin();
142 if(result2 < 0 || current->startproject < result2)
143 result2 = current->startproject;
147 // New EDL has fewer plugin sets. Get starting plugin in old plugin set
148 for(int i = plugin_sets; i < track->plugin_set.total; i++)
150 Plugin *current = track->plugin_set.values[i]->get_first_plugin();
153 if(result2 < 0 || current->startproject < result2)
154 result2 = current->startproject;
158 // Number of plugin sets differs but somehow we didn't find the start of the
160 if(track->plugin_set.total != plugin_set.total && result2 < 0)
164 (*result < 0 || from_units(result2) < *result))
165 *result = from_units(result2);
169 int Track::is_synthesis(int64_t position, int direction, int depth)
172 for( int i = 0; !result && i<plugin_set.total; ++i ) {
173 Plugin *plugin = get_current_plugin(position, i, direction, 0, 0);
174 if( !plugin ) continue;
175 // Assume data from a shared track is synthesized
176 result = plugin->plugin_type == PLUGIN_SHAREDMODULE ? 1 :
177 plugin->is_synthesis(position, direction, depth);
182 void Track::copy_from(Track *track)
184 copy_settings(track);
185 edits->copy_from(track->edits);
186 this->plugin_set.remove_all_objects();
188 for( int i=0; i<track->plugin_set.total; ++i ) {
189 PluginSet *new_plugin_set = plugin_set.append(new PluginSet(edl, this));
190 new_plugin_set->copy_from(track->plugin_set.values[i]);
192 automation->copy_from(track->automation);
193 this->track_w = track->track_w;
194 this->track_h = track->track_h;
197 Track& Track::operator=(Track& track)
199 printf("Track::operator= 1\n");
204 int Track::vertical_span(Theme *theme)
208 result += theme->get_image("title_bg_data")->get_h();
210 result += edl->local_session->zoom_track;
212 result += plugin_set.total * theme->get_image("plugin_bg_data")->get_h();
213 result = MAX(result, theme->title_h);
217 double Track::get_length()
219 double total_length = 0;
225 length = from_units(edits->last->startproject + edits->last->length);
226 if(length > total_length) total_length = length;
230 for(int i = 0; i < plugin_set.total; i++)
232 if( !plugin_set.values[i]->last ) continue;
233 length = from_units(plugin_set.values[i]->last->startproject +
234 plugin_set.values[i]->last->length);
235 if(length > total_length) total_length = length;
239 length = from_units(automation->get_length());
240 if(length > total_length) total_length = length;
246 int Track::has_speed()
248 FloatAutos *autos = (FloatAutos*)automation->autos[AUTOMATION_SPEED];
253 for(FloatAuto *current = (FloatAuto*)autos->first;
255 current = (FloatAuto*)current->next)
257 if(!EQUIV(current->get_value(), 1.0) ||
258 !EQUIV(current->get_control_in_value(), 0.0) ||
259 !EQUIV(current->get_control_out_value(), 0.0))
270 int Track::show_assets()
272 return expand_view || edl->session->show_assets ? 1 : 0;
275 int Track::show_titles()
277 return expand_view || edl->session->show_titles ? 1 : 0;
280 int Track::show_transitions()
282 return expand_view || edl->session->auto_conf->transitions ? 1 : 0;
285 void Track::get_source_dimensions(double position, int &w, int &h)
287 int64_t native_position = to_units(position, 0);
288 for(Edit *current = edits->first; current; current = NEXT)
290 if(current->startproject <= native_position &&
291 current->startproject + current->length > native_position &&
294 w = current->asset->width;
295 h = current->asset->height;
302 int64_t Track::horizontal_span()
304 return (int64_t)(get_length() *
305 edl->session->sample_rate /
306 edl->local_session->zoom_sample +
311 int Track::load(FileXML *file, int track_offset, uint32_t load_flags)
314 int current_plugin = 0;
317 record = file->tag.get_property("RECORD", record);
318 play = file->tag.get_property("PLAY", play);
319 gang = file->tag.get_property("GANG", gang);
320 draw = file->tag.get_property("DRAW", draw);
321 nudge = file->tag.get_property("NUDGE", nudge);
322 mixer_id = file->tag.get_property("MIXER_ID", mixer_id);
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);
326 masks = file->tag.get_property("MASKS", masks);
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"))
343 file->read_text_until("/TITLE", &data);
344 memset(title, 0, sizeof(title));
345 strncpy(title, data.cstr(), sizeof(title)-1);
348 if(load_flags && automation->load(file)
349 /* strstr(file->tag.get_title(), "AUTOS") */)
354 if(file->tag.title_is("EDITS"))
356 if(load_flags & LOAD_EDITS)
357 edits->load(file, track_offset);
359 result = file->skip_tag();
362 if(file->tag.title_is("PLUGINSET"))
364 if(load_flags & LOAD_EDITS)
366 PluginSet *plugin_set = new PluginSet(edl, this);
367 this->plugin_set.append(plugin_set);
368 plugin_set->load(file, load_flags);
371 if(load_flags & LOAD_AUTOMATION)
373 if(current_plugin < this->plugin_set.total)
375 PluginSet *plugin_set = this->plugin_set.values[current_plugin];
376 plugin_set->load(file, load_flags);
381 result = file->skip_tag();
384 load_derived(file, load_flags);
393 void Track::insert_asset(Asset *asset,
399 edits->insert_asset(asset,
402 to_units(position, 0),
408 // Default keyframes: We don't replace default keyframes in pasting but
409 // when inserting the first EDL of a load operation we need to replace
410 // the default keyframes.
412 // Plugins: This is an arbitrary behavior
414 // 1) No plugin in source track: Paste silence into destination
416 // 2) Plugin in source track: plugin in source track is inserted into
417 // existing destination track plugin sets, new sets being added when
420 void Track::insert_track(Track *track,
427 // Calculate minimum length of data to pad.
428 int64_t min_length = to_units(
429 MAX(edl_length, track->get_length()),
431 //printf("Track::insert_track %d %s %jd\n", __LINE__, title, min_length);
433 // Decide whether to copy settings based on load_mode
434 if(replace_default) copy_settings(track);
436 edits->insert_edits(track->edits,
437 to_units(position, 0),
442 insert_plugin_set(track,
443 to_units(position, 0),
448 automation->insert_track(track->automation,
449 to_units(position, 0),
457 // Called by insert_track
458 void Track::insert_plugin_set(Track *track,
463 // Extend plugins if no incoming plugins
464 if( track->plugin_set.total ) {
465 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],
470 position, min_length, edit_autos);
474 shift_effects(position, min_length, edit_autos, 0);
478 Plugin* Track::insert_effect(const char *title,
479 SharedLocation *shared_location,
480 KeyFrame *default_keyframe,
481 PluginSet *plugin_set,
488 plugin_set = new PluginSet(edl, this);
489 this->plugin_set.append(plugin_set);
494 // Position is identical to source plugin
495 if(plugin_type == PLUGIN_SHAREDPLUGIN)
497 Track *source_track = tracks->get_item_number(shared_location->module);
500 Plugin *source_plugin = source_track->get_current_plugin(
501 edl->local_session->get_selectionstart(1),
502 shared_location->plugin,
507 // From an attach operation
510 plugin = plugin_set->insert_plugin(title,
511 source_plugin->startproject,
512 source_plugin->length,
519 // From a drag operation
521 plugin = plugin_set->insert_plugin(title,
533 // This should be done in the caller
536 if(edl->local_session->get_selectionend() >
537 edl->local_session->get_selectionstart())
539 start = edl->local_session->get_selectionstart();
540 length = edl->local_session->get_selectionend() - start;
545 length = get_length();
548 //printf("Track::insert_effect %f %f %d %d\n", start, length, to_units(start, 0),
549 // to_units(length, 0));
551 plugin = plugin_set->insert_plugin(title,
559 //printf("Track::insert_effect 2 %f %f\n", start, length);
565 void Track::move_plugins_up(PluginSet *plugin_set)
567 for(int i = 0; i < this->plugin_set.total; i++)
569 if(this->plugin_set.values[i] == plugin_set)
573 PluginSet *temp = this->plugin_set.values[i - 1];
574 this->plugin_set.values[i - 1] = this->plugin_set.values[i];
575 this->plugin_set.values[i] = temp;
577 SharedLocation old_location, new_location;
578 new_location.module = old_location.module = tracks->number_of(this);
579 old_location.plugin = i;
580 new_location.plugin = i - 1;
581 tracks->change_plugins(old_location, new_location, 1);
587 void Track::move_plugins_down(PluginSet *plugin_set)
589 for(int i = 0; i < this->plugin_set.total; i++)
591 if(this->plugin_set.values[i] == plugin_set)
593 if(i == this->plugin_set.total - 1) break;
595 PluginSet *temp = this->plugin_set.values[i + 1];
596 this->plugin_set.values[i + 1] = this->plugin_set.values[i];
597 this->plugin_set.values[i] = temp;
599 SharedLocation old_location, new_location;
600 new_location.module = old_location.module = tracks->number_of(this);
601 old_location.plugin = i;
602 new_location.plugin = i + 1;
603 tracks->change_plugins(old_location, new_location, 1);
610 void Track::remove_asset(Indexable *asset)
612 for(Edit *edit = edits->first; edit; edit = edit->next)
614 if(asset->is_asset &&
616 edit->asset == (Asset*)asset)
621 if(!asset->is_asset &&
623 edit->nested_edl == (EDL*)asset)
625 edit->nested_edl = 0;
631 void Track::remove_pluginset(PluginSet *plugin_set)
634 for(i = 0; i < this->plugin_set.total; i++)
635 if(plugin_set == this->plugin_set.values[i]) break;
637 this->plugin_set.remove_object(plugin_set);
638 for(i++ ; i < this->plugin_set.total; i++)
640 SharedLocation old_location, new_location;
641 new_location.module = old_location.module = tracks->number_of(this);
642 old_location.plugin = i;
643 new_location.plugin = i - 1;
644 tracks->change_plugins(old_location, new_location, 0);
648 void Track::shift_keyframes(int64_t position, int64_t length)
650 automation->paste_silence(position, position + length);
651 // Effect keyframes are shifted in shift_effects
654 void Track::shift_effects(int64_t position, int64_t length, int edit_autos, Edits *trim_edits)
656 for( int i=0; i<plugin_set.total; ++i ) {
657 if( !trim_edits || trim_edits == (Edits*)plugin_set.values[i] )
658 plugin_set.values[i]->shift_effects(position, length, edit_autos);
662 void Track::detach_effect(Plugin *plugin)
664 //printf("Track::detach_effect 1\n");
665 for(int i = 0; i < plugin_set.total; i++)
667 PluginSet *plugin_set = this->plugin_set.values[i];
668 for(Plugin *dest = (Plugin*)plugin_set->first;
670 dest = (Plugin*)dest->next)
674 int64_t start = plugin->startproject;
675 int64_t end = plugin->startproject + plugin->length;
677 plugin_set->clear(start, end, 1);
679 //printf("Track::detach_effect 2 %d\n", plugin_set->length());
680 // Delete 0 length pluginsets
687 void Track::resample(double old_rate, double new_rate)
689 edits->resample(old_rate, new_rate);
690 automation->resample(old_rate, new_rate);
691 for(int i = 0; i < plugin_set.total; i++)
692 plugin_set.values[i]->resample(old_rate, new_rate);
693 nudge = (int64_t)(nudge * new_rate / old_rate);
696 void Track::detach_shared_effects(int module)
698 for(int i = 0; i < plugin_set.size(); i++) {
699 PluginSet *plugin_set = this->plugin_set.get(i);
700 for(Plugin *dest = (Plugin*)plugin_set->first; dest; ) {
701 if( (dest->plugin_type == PLUGIN_SHAREDPLUGIN ||
702 dest->plugin_type == PLUGIN_SHAREDMODULE) &&
703 dest->shared_location.module == module ) {
704 int64_t start = dest->startproject;
705 int64_t end = dest->startproject + dest->length;
706 plugin_set->clear(start, end, 1);
709 if(dest) dest = (Plugin*)dest->next;
716 void Track::optimize()
719 for(int i = 0; i < plugin_set.total; ) {
720 PluginSet *plugin_set = this->plugin_set.values[i];
721 plugin_set->optimize();
722 //printf("Track::optimize %d\n", plugin_set.values[i]->total());
723 // new definition of empty track...
724 if( !plugin_set->last ||
725 (plugin_set->last == plugin_set->first &&
726 plugin_set->last->silence()) ) {
727 remove_pluginset(plugin_set);
734 Plugin* Track::get_current_plugin(double position,
741 if(convert_units) position = to_units(position, 0);
742 if(use_nudge) position += nudge;
744 if(plugin_set >= this->plugin_set.total || plugin_set < 0) return 0;
746 //printf("Track::get_current_plugin 1 %d %d %d\n", position, this->plugin_set.total, direction);
747 if(direction == PLAY_FORWARD)
749 for(current = (Plugin*)this->plugin_set.values[plugin_set]->last;
751 current = (Plugin*)PREVIOUS)
753 // printf("Track::get_current_plugin 2 %d %ld %ld\n",
754 // current->startproject,
755 // current->startproject + current->length,
757 if(current->startproject <= position &&
758 current->startproject + current->length > position)
765 if(direction == PLAY_REVERSE)
767 for(current = (Plugin*)this->plugin_set.values[plugin_set]->first;
769 current = (Plugin*)NEXT)
771 if(current->startproject < position &&
772 current->startproject + current->length >= position)
782 Plugin* Track::get_current_transition(double position,
789 if(convert_units) position = to_units(position, 0);
790 if(use_nudge) position += nudge;
792 if(direction == PLAY_FORWARD)
794 for(current = edits->last; current; current = PREVIOUS)
796 if(current->startproject <= position && current->startproject + current->length > position)
798 //printf("Track::get_current_transition %p\n", current->transition);
799 if(current->transition && position < current->startproject + current->transition->length)
801 result = current->transition;
808 if(direction == PLAY_REVERSE)
810 for(current = edits->first; current; current = NEXT)
812 if(current->startproject < position && current->startproject + current->length >= position)
814 if(current->transition && position <= current->startproject + current->transition->length)
816 result = current->transition;
826 void Track::synchronize_params(Track *track)
828 for(Edit *this_edit = edits->first, *that_edit = track->edits->first;
829 this_edit && that_edit;
830 this_edit = this_edit->next, that_edit = that_edit->next)
832 this_edit->synchronize_params(that_edit);
835 for(int i = 0; i < plugin_set.total && i < track->plugin_set.total; i++)
836 plugin_set.values[i]->synchronize_params(track->plugin_set.values[i]);
838 automation->copy_from(track->automation);
839 this->nudge = track->nudge;
843 int Track::dump(FILE *fp)
845 fprintf(fp," Data type %d, draw %d, gang %d, play %d, record %d, nudge %jd, masks 0x%04x\n",
846 data_type, draw, gang, play, record, nudge, masks);
847 fprintf(fp," Title %s\n", title);
848 fprintf(fp," Edits:\n");
849 for(Edit* current = edits->first; current; current = NEXT)
851 automation->dump(fp);
852 fprintf(fp," Plugin Sets: %d\n", plugin_set.total);
854 for( int i=0; i<plugin_set.total; ++i )
855 plugin_set[i]->dump(fp);
860 Track::Track() : ListItem<Track>()
865 // ======================================== accounting
867 int Track::number_of()
869 return tracks->number_of(this);
884 // ================================================= editing
886 int Track::select_auto(AutoConf *auto_conf, int cursor_x, int cursor_y)
891 int Track::move_auto(AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
896 int Track::release_auto()
901 // used for copying automation alone
902 int Track::copy_automation(double selectionstart,
908 int64_t start = to_units(selectionstart, 0);
909 int64_t end = to_units(selectionend, 1);
911 file->tag.set_title("TRACK");
915 file->append_newline();
917 automation->copy(start, end, file, default_only, active_only);
919 if(edl->session->auto_conf->plugins)
921 for(int i = 0; i < plugin_set.total; i++)
924 plugin_set.values[i]->copy_keyframes(start,
932 file->tag.set_title("/TRACK");
934 file->append_newline();
935 file->append_newline();
936 file->append_newline();
937 file->append_newline();
942 int Track::paste_automation(double selectionstart, double total_length,
943 double frame_rate, int64_t sample_rate,
944 FileXML *file, int default_only, int active_only)
946 // Only used for pasting automation alone.
947 double scale = data_type == TRACK_AUDIO ?
948 edl->session->sample_rate / sample_rate :
949 edl->session->frame_rate / frame_rate ;
951 total_length *= scale;
952 int64_t start = to_units(selectionstart, 0);
953 int64_t length = to_units(total_length, 1);
955 int current_pluginset = 0;
956 //printf("Track::paste_automation 1\n");
958 while( !(result = file->read_tag()) ) {
959 if( file->tag.title_is("/TRACK") ) break;
960 if( automation->paste(start, length, scale, file,
961 default_only, active_only, 0) )
963 if( file->tag.title_is("PLUGINSET") ) {
964 if( current_pluginset < plugin_set.total ) {
965 plugin_set.values[current_pluginset]->
966 paste_keyframes(start, length, file,
967 default_only, active_only);
976 void Track::clear_automation(double selectionstart,
981 int64_t start = to_units(selectionstart, 0);
982 int64_t end = to_units(selectionend, 1);
984 automation->clear(start, end, edl->session->auto_conf, 0);
986 if(edl->session->auto_conf->plugins)
988 for(int i = 0; i < plugin_set.total; i++)
990 plugin_set.values[i]->clear_keyframes(start, end);
996 void Track::set_automation_mode(double selectionstart,
1000 int64_t start = to_units(selectionstart, 0);
1001 int64_t end = to_units(selectionend, 1);
1003 automation->set_automation_mode(start, end, mode, edl->session->auto_conf);
1009 int Track::copy(int copy_flags, double start, double end,
1010 FileXML *file, const char *output_path)
1012 // Use a copy of the selection in converted units
1013 // So copy_automation doesn't reconvert.
1014 int64_t start_unit = to_units(start, 0);
1015 int64_t end_unit = to_units(end, 1);
1020 file->tag.set_title("TRACK");
1021 file->tag.set_property("RECORD", record);
1022 file->tag.set_property("NUDGE", nudge);
1023 file->tag.set_property("MIXER_ID", mixer_id);
1024 file->tag.set_property("PLAY", play);
1025 file->tag.set_property("GANG", gang);
1026 file->tag.set_property("DRAW", draw);
1027 file->tag.set_property("EXPAND", expand_view);
1028 file->tag.set_property("TRACK_W", track_w);
1029 file->tag.set_property("TRACK_H", track_h);
1030 file->tag.set_property("MASKS", masks);
1033 file->append_newline();
1036 file->tag.set_title("TITLE");
1038 file->append_text(title);
1039 file->tag.set_title("/TITLE");
1041 file->append_newline();
1043 // if(data_type == TRACK_AUDIO)
1044 // file->tag.set_property("TYPE", "AUDIO");
1046 // file->tag.set_property("TYPE", "VIDEO");
1048 // file->append_tag();
1049 // file->append_newline();
1051 if( (copy_flags & COPY_EDITS) )
1052 edits->copy(start_unit, end_unit, file, output_path);
1054 if( (copy_flags & COPY_AUTOS) ) {
1056 auto_conf.set_all(1);
1057 automation->copy(start_unit, end_unit, file, 0, 0);
1060 if( (copy_flags & COPY_PLUGINS) ) {
1061 for( int i=0; i<plugin_set.total; ++i )
1062 plugin_set.values[i]->copy(start_unit, end_unit, file);
1065 copy_derived(start_unit, end_unit, file);
1067 file->tag.set_title("/TRACK");
1069 file->append_newline();
1070 file->append_newline();
1071 file->append_newline();
1072 file->append_newline();
1077 int Track::copy_assets(double start,
1079 ArrayList<Asset*> *asset_list)
1083 start = to_units(start, 0);
1084 end = to_units(end, 1);
1086 Edit *current = edits->editof((int64_t)start, PLAY_FORWARD, 0);
1089 while(current && current->startproject < end)
1091 // Check for duplicate assets
1094 for(i = 0, result = 0; i < asset_list->total; i++)
1096 if(asset_list->values[i] == current->asset) result = 1;
1098 // append pointer to new asset
1099 if(!result) asset_list->append(current->asset);
1108 int Track::blade(double position)
1110 int64_t start = to_units(position, 0);
1111 Edit *edit = edits->split_edit(start);
1112 if( !edit || edit->silence() ) return 1;
1113 edit->hard_left = 1;
1114 if( edit->previous ) edit->previous->hard_right = 1;
1118 int Track::clear(double start, double end,
1119 int edit_edits, int edit_labels, int edit_plugins,
1120 int edit_autos, Edits *trim_edits)
1122 return clear(to_units(start, 0), to_units(end, 1),
1123 edit_edits, edit_labels, edit_plugins, edit_autos, trim_edits);
1126 int Track::clear(int64_t start, int64_t end,
1127 int edit_edits, int edit_labels, int edit_plugins,
1128 int edit_autos, Edits *trim_edits)
1130 //printf("Track::clear 1 %d %d %d\n", edit_edits, edit_labels, edit_plugins);
1132 automation->clear(start, end, 0, 1);
1133 if( edit_plugins ) {
1134 int edit_keyframes = edit_plugins < 0 ? 1 : edit_autos;
1135 for(int i = 0; i < plugin_set.total; i++) {
1136 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1137 plugin_set.values[i]->clear(start, end, edit_keyframes);
1141 edits->clear(start, end);
1145 int Track::clear_handle(double start,
1152 edits->clear_handle(start, end, clear_plugins, edit_autos, distance);
1156 int Track::popup_transition(int cursor_x, int cursor_y)
1163 int Track::modify_edithandles(double oldposition, double newposition,
1164 int currentend, int handle_mode, int edit_labels,
1165 int edit_plugins, int edit_autos, int group_id)
1167 edits->modify_handles(oldposition, newposition,
1168 currentend, handle_mode, 1, edit_labels, edit_plugins,
1169 edit_autos, 0, group_id);
1173 int Track::modify_pluginhandles(double oldposition,
1181 for(int i = 0; i < plugin_set.total; i++)
1183 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1184 plugin_set.values[i]->modify_handles(oldposition, newposition,
1185 // Don't allow plugin tweeks to affect edits.
1186 currentend, handle_mode, 0, 0, 0, 0, 0, 0);
1192 int Track::paste_silence(double start, double end, int edit_plugins, int edit_autos)
1194 return paste_silence(to_units(start, 0), to_units(end, 1),
1195 edit_plugins, edit_autos);
1198 int Track::paste_silence(int64_t start, int64_t end, int edit_plugins, int edit_autos)
1200 edits->paste_silence(start, end);
1202 shift_keyframes(start, end - start);
1204 shift_effects(start, end - start, edit_autos, 0);
1209 int Track::select_edit(int cursor_x,
1217 int Track::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
1222 void Track::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
1224 for(int i = 0; i < plugin_set.total; i++)
1226 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1228 plugin = (Plugin*)plugin->next)
1230 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN)
1232 if(plugin->shared_location == old_location)
1233 plugin->shared_location = new_location;
1235 if(do_swap && plugin->shared_location == new_location)
1236 plugin->shared_location = old_location;
1242 void Track::change_modules(int old_location, int new_location, int do_swap)
1244 for(int i = 0; i < plugin_set.total; i++)
1246 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1248 plugin = (Plugin*)plugin->next)
1250 if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN ||
1251 plugin->plugin_type == PLUGIN_SHAREDMODULE)
1253 if(plugin->shared_location.module == old_location)
1254 plugin->shared_location.module = new_location;
1256 if(do_swap && plugin->shared_location.module == new_location)
1257 plugin->shared_location.module = old_location;
1264 int Track::playable_edit(int64_t position, int direction)
1267 if(direction == PLAY_REVERSE) position--;
1268 for(Edit *current = edits->first; current && !result; current = NEXT)
1270 if(current->startproject <= position &&
1271 current->startproject + current->length > position)
1273 //printf("Track::playable_edit %p %p\n", current->transition, current->asset);
1274 if(current->transition ||
1276 current->nested_edl) result = 1;
1283 int Track::need_edit(Edit *current, int test_transitions)
1285 return ((test_transitions && current->transition) ||
1286 (!test_transitions && current->asset));
1289 int64_t Track::plugin_change_duration(int64_t input_position,
1290 int64_t input_length,
1294 if(use_nudge) input_position += nudge;
1295 for(int i = 0; i < plugin_set.total; i++)
1297 int64_t new_duration = plugin_set.values[i]->plugin_change_duration(
1301 if(new_duration < input_length) input_length = new_duration;
1303 return input_length;
1306 int64_t Track::edit_change_duration(int64_t input_position,
1307 int64_t input_length,
1309 int test_transitions,
1313 int64_t edit_length = input_length;
1314 if(use_nudge) input_position += nudge;
1318 // ================================= Reverse playback
1319 // Get first edit on or after position
1320 for(current = edits->first;
1321 current && current->startproject + current->length <= input_position;
1327 if(current->startproject > input_position)
1329 // Before first edit
1333 if(need_edit(current, test_transitions))
1335 // Over an edit of interest.
1336 if(input_position - current->startproject < input_length)
1337 edit_length = input_position - current->startproject + 1;
1341 // Over an edit that isn't of interest.
1342 // Search for next edit of interest.
1343 for(current = PREVIOUS ;
1345 current->startproject + current->length > input_position - input_length &&
1346 !need_edit(current, test_transitions);
1351 need_edit(current, test_transitions) &&
1352 current->startproject + current->length > input_position - input_length)
1353 edit_length = input_position - current->startproject - current->length + 1;
1358 // Not over an edit. Try the last edit.
1359 current = edits->last;
1361 ((test_transitions && current->transition) ||
1362 (!test_transitions && current->asset)))
1363 edit_length = input_position - edits->length() + 1;
1368 // =================================== forward playback
1369 // Get first edit on or before position
1370 for(current = edits->last;
1371 current && current->startproject > input_position;
1377 if(current->startproject + current->length <= input_position)
1379 // Beyond last edit.
1383 if(need_edit(current, test_transitions))
1385 // Over an edit of interest.
1386 // Next edit is going to require a change.
1387 if(current->length + current->startproject - input_position < input_length)
1388 edit_length = current->startproject + current->length - input_position;
1392 // Over an edit that isn't of interest.
1393 // Search for next edit of interest.
1394 for(current = NEXT ;
1396 current->startproject < input_position + input_length &&
1397 !need_edit(current, test_transitions);
1402 need_edit(current, test_transitions) &&
1403 current->startproject < input_position + input_length)
1404 edit_length = current->startproject - input_position;
1409 // Not over an edit. Try the first edit.
1410 current = edits->first;
1412 ((test_transitions && current->transition) ||
1413 (!test_transitions && current->asset)))
1414 edit_length = edits->first->startproject - input_position;
1418 if(edit_length < input_length)
1421 return input_length;
1424 void Track::shuffle_edits(double start, double end, int first_track)
1426 ArrayList<Edit*> new_edits;
1427 ArrayList<Label*> new_labels;
1428 int64_t start_units = to_units(start, 0);
1429 int64_t end_units = to_units(end, 0);
1430 // Sample range of all edits selected
1431 //int64_t total_start_units = 0;
1432 //int64_t total_end_units = 0;
1433 // Edit before range
1434 Edit *start_edit = 0;
1435 int have_start_edit = 0;
1437 // Move all edit pointers to list
1438 for(Edit *current = edits->first;
1441 if(current->startproject >= start_units &&
1442 current->startproject + current->length <= end_units)
1444 if(!have_start_edit) start_edit = current->previous;
1445 have_start_edit = 1;
1446 //total_start_units = current->startproject;
1447 //total_end_units = current->startproject + current->length;
1448 new_edits.append(current);
1450 // Move label pointers
1451 if(first_track && edl->session->labels_follow_edits)
1453 double start_seconds = from_units(current->startproject);
1454 double end_seconds = from_units(current->startproject +
1456 for(Label *label = edl->labels->first;
1458 label = label->next)
1460 if(label->position >= start_seconds &&
1461 label->position < end_seconds)
1463 new_labels.append(label);
1464 edl->labels->remove_pointer(label);
1469 // Remove edit pointer
1470 Edit *previous = current;
1472 edits->remove_pointer(previous);
1480 // Insert pointers in random order
1481 while(new_edits.size())
1483 int index = rand() % new_edits.size();
1484 Edit *edit = new_edits.get(index);
1485 new_edits.remove_number(index);
1487 edits->insert_before(edits->first, edit);
1489 edits->insert_after(start_edit, edit);
1492 // Recalculate start position
1493 // Save old position for moving labels
1494 int64_t startproject1 = edit->startproject;
1495 int64_t startproject2 = 0;
1498 edit->startproject =
1500 edit->previous->startproject + edit->previous->length;
1504 edit->startproject = startproject2 = 0;
1508 // Insert label pointers
1509 if(first_track && edl->session->labels_follow_edits)
1511 double start_seconds1 = from_units(startproject1);
1512 double end_seconds1 = from_units(startproject1 + edit->length);
1513 double start_seconds2 = from_units(startproject2);
1514 for(int i = new_labels.size() - 1; i >= 0; i--)
1516 Label *label = new_labels.get(i);
1517 // Was in old edit position
1518 if(label->position >= start_seconds1 &&
1519 label->position < end_seconds1)
1521 // Move to new edit position
1522 double position = label->position -
1523 start_seconds1 + start_seconds2;
1524 edl->labels->insert_label(position);
1525 new_labels.remove_object_number(i);
1535 if(first_track && edl->session->labels_follow_edits)
1537 edl->labels->optimize();
1541 // exactly the same as shuffle_edits except for 1 line
1542 void Track::reverse_edits(double start, double end, int first_track)
1544 ArrayList<Edit*> new_edits;
1545 ArrayList<Label*> new_labels;
1546 int64_t start_units = to_units(start, 0);
1547 int64_t end_units = to_units(end, 0);
1548 // Sample range of all edits selected
1549 //int64_t total_start_units = 0;
1550 //int64_t total_end_units = 0;
1551 // Edit before range
1552 Edit *start_edit = 0;
1553 int have_start_edit = 0;
1555 // Move all edit pointers to list
1556 for(Edit *current = edits->first; current; )
1558 if(current->startproject >= start_units &&
1559 current->startproject + current->length <= end_units)
1561 if(!have_start_edit) start_edit = current->previous;
1562 have_start_edit = 1;
1563 //total_start_units = current->startproject;
1564 //total_end_units = current->startproject + current->length;
1565 new_edits.append(current);
1567 // Move label pointers
1568 if(first_track && edl->session->labels_follow_edits)
1570 double start_seconds = from_units(current->startproject);
1571 double end_seconds = from_units(current->startproject +
1573 for(Label *label = edl->labels->first;
1575 label = label->next)
1577 if(label->position >= start_seconds &&
1578 label->position < end_seconds)
1580 new_labels.append(label);
1581 edl->labels->remove_pointer(label);
1586 // Remove edit pointer
1587 Edit *previous = current;
1589 edits->remove_pointer(previous);
1597 // Insert pointers in reverse order
1598 while(new_edits.size())
1600 int index = new_edits.size() - 1;
1601 Edit *edit = new_edits.get(index);
1602 new_edits.remove_number(index);
1604 edits->insert_before(edits->first, edit);
1606 edits->insert_after(start_edit, edit);
1609 // Recalculate start position
1610 // Save old position for moving labels
1611 int64_t startproject1 = edit->startproject;
1612 int64_t startproject2 = 0;
1615 edit->startproject =
1617 edit->previous->startproject + edit->previous->length;
1621 edit->startproject = startproject2 = 0;
1625 // Insert label pointers
1626 if(first_track && edl->session->labels_follow_edits)
1628 double start_seconds1 = from_units(startproject1);
1629 double end_seconds1 = from_units(startproject1 + edit->length);
1630 double start_seconds2 = from_units(startproject2);
1631 for(int i = new_labels.size() - 1; i >= 0; i--)
1633 Label *label = new_labels.get(i);
1634 // Was in old edit position
1635 if(label->position >= start_seconds1 &&
1636 label->position < end_seconds1)
1638 // Move to new edit position
1639 double position = label->position -
1640 start_seconds1 + start_seconds2;
1641 edl->labels->insert_label(position);
1642 new_labels.remove_object_number(i);
1652 if(first_track && edl->session->labels_follow_edits)
1654 edl->labels->optimize();
1658 void Track::align_edits(double start, double end, Track *master_track)
1660 int64_t start_units = to_units(start, 0);
1661 int64_t end_units = to_units(end, 0);
1663 // All other tracks get silence or cut to align the edits on the times.
1664 Edit *master = master_track->edits->first;
1665 for(Edit *current = edits->first; current && master; ) {
1666 if( current->startproject >= start_units &&
1667 current->startproject + current->length <= end_units ) {
1668 // edit is in highlighted region
1669 int64_t master_length_units = to_units(master_track->from_units(master->length), 0);
1670 // starting time of master edit
1671 int64_t master_start_units = to_units(master_track->from_units(master->startproject), 0);
1672 // starting time of current edit
1673 int64_t current_startunits = current->startproject;
1675 // the following occur if multiple aligns are performed
1676 // master edit is not silence but current edit is silence
1677 if( !master->silence() && current->silence() ) {
1678 // try again with next edit
1682 if( master->silence() && !current->silence() ) {
1683 // master edit is silence but current edit is not silence
1684 master = master->next;
1687 if( current->length < master_length_units / 2 ) {
1688 // current edit is a glitch edit between 2 required edits
1694 // current edit starts before master edit
1695 if( current_startunits < master_start_units ) {
1696 //printf("Track::align_edits %d\n", __LINE__);
1697 edits->paste_silence(current_startunits, master_start_units);
1698 shift_keyframes(current_startunits,
1699 master_start_units - current_startunits);
1701 else if( current_startunits > master_start_units ) {
1702 // current edit starts after master edit
1703 edits->clear(master_start_units,
1704 current_startunits);
1705 if(edl->session->autos_follow_edits)
1706 shift_keyframes(master_start_units,
1707 current_startunits - master_start_units);
1710 master = master->next;
1714 master = master->next;
1721 int Track::purge_asset(Asset *asset)
1726 int Track::asset_used(Asset *asset)
1731 for(current_edit = edits->first; current_edit; current_edit = current_edit->next)
1733 if(current_edit->asset == asset)
1741 int Track::is_playable(int64_t position, int direction)
1747 int Track::plugin_used(int64_t position, int64_t direction)
1749 //printf("Track::plugin_used 1 %d\n", this->plugin_set.total);
1750 for(int i = 0; i < this->plugin_set.total; i++)
1752 Plugin *current_plugin = get_current_plugin(position,
1758 //printf("Track::plugin_used 2 %p %d %d\n", current_plugin, current_plugin->on, current_plugin->plugin_type);
1759 if(current_plugin &&
1760 current_plugin->on &&
1761 current_plugin->plugin_type != PLUGIN_NONE)
1766 //printf("Track::plugin_used 3 %p\n", current_plugin);
1770 // Audio is always rendered through VConsole
1771 int Track::direct_copy_possible(int64_t start, int direction, int use_nudge)
1776 int64_t Track::to_units(double position, int round)
1778 return (int64_t)position;
1781 double Track::to_doubleunits(double position)
1786 double Track::from_units(int64_t position)
1788 return (double)position;
1791 int64_t Track::frame_align(int64_t position, int round)
1793 if( data_type != TRACK_VIDEO && edl->session->cursor_on_frames )
1794 position = to_units(edl->align_to_frame(from_units(position), round), round);
1798 Plugin *Track::plugin_exists(int plugin_id)
1800 for( int i=0; i<plugin_set.size(); ++i ) {
1801 PluginSet *set = plugin_set.get(i);
1802 Plugin *plugin = (Plugin*)set->first;
1803 for( ; plugin; plugin=(Plugin*)plugin->next ) {
1804 if( plugin->orig_id == plugin_id )
1808 for( Edit *current=edits->first; current; current=NEXT ) {
1809 Plugin *plugin = (Plugin *)current->transition;
1810 if( plugin && plugin->orig_id == plugin_id )
1816 int Track::get_mixer_id()
1818 if( mixer_id < 0 ) {
1820 for( Track *track=tracks->first; track!=0; track=track->next )
1821 if( track->mixer_id > v ) v = track->mixer_id;
1827 void Track::get_fauto_xyz(int fauto, float &x, float &y, float &z)
1829 FloatAutos **fautos = (FloatAutos **)&automation->autos;
1830 FloatAuto *xauto = (FloatAuto *)fautos[fauto+0]->get_auto_for_editing(-1, 1);
1831 if( xauto ) x = xauto->get_value();
1832 FloatAuto *yauto = (FloatAuto *)fautos[fauto+1]->get_auto_for_editing(-1, 1);
1833 if( yauto ) y = yauto->get_value();
1834 FloatAuto *zauto = (FloatAuto *)fautos[fauto+2]->get_auto_for_editing(-1, 1);
1835 if( zauto ) z = zauto->get_value();
1837 void Track::set_fauto_xyz(int fauto, float x, float y, float z)
1839 FloatAutos **fautos = (FloatAutos **)&automation->autos;
1840 FloatAuto *xauto = (FloatAuto *)fautos[fauto+0]->get_auto_for_editing(-1, 1);
1841 if( xauto ) xauto->set_value(x);
1842 FloatAuto *yauto = (FloatAuto *)fautos[fauto+1]->get_auto_for_editing(-1, 1);
1843 if( yauto ) yauto->set_value(y);
1844 FloatAuto *zauto = (FloatAuto *)fautos[fauto+2]->get_auto_for_editing(-1, 1);
1845 if( zauto ) zauto->set_value(z);
1848 void Track::get_projector(float &x, float &y, float &z)
1850 get_fauto_xyz(AUTOMATION_PROJECTOR_X, x, y, z);
1852 void Track::set_projector(float x, float y, float z)
1854 set_fauto_xyz(AUTOMATION_PROJECTOR_X, x, y, z);
1857 void Track::get_camera(float &x, float &y, float &z)
1859 get_fauto_xyz(AUTOMATION_CAMERA_X, x, y, z);
1861 void Track::set_camera(float x, float y, float z)
1863 set_fauto_xyz(AUTOMATION_CAMERA_X, x, y, z);