1cf0f69dbcd6a51c14251977759d64ca3445a189
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / track.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include "asset.h"
22 #include "autoconf.h"
23 #include "automation.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "edit.h"
27 #include "edits.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "filexml.h"
31 #include "floatauto.h"
32 #include "floatautos.h"
33 #include "keyframe.h"
34 #include "labels.h"
35 #include "localsession.h"
36 #include "maskauto.h"
37 #include "module.h"
38 #include "patch.h"
39 #include "patchbay.h"
40 #include "plugin.h"
41 #include "pluginset.h"
42 #include "mainsession.h"
43 #include "theme.h"
44 #include "intautos.h"
45 #include "track.h"
46 #include "trackcanvas.h"
47 #include "tracks.h"
48 #include "transition.h"
49 #include "transportque.inc"
50 #include "vedit.h"
51 #include "vframe.h"
52 #include <string.h>
53
54
55 Track::Track(EDL *edl, Tracks *tracks) : ListItem<Track>()
56 {
57         this->edl = edl;
58         this->tracks = tracks;
59         y_pixel = 0;
60         data_h = 64;
61         expand_view = 0;
62         draw = 1;
63         ganged = 1;
64         master = 0;
65         title[0] = 0;
66         armed = 1;
67         play = 1;
68         nudge = 0;
69         track_w = edl->session->output_w;
70         track_h = edl->session->output_h;
71         id = EDL::next_id();
72         mixer_id = -1;
73         masks = (1<<SUBMASKS)-1;
74 }
75
76 Track::~Track()
77 {
78         delete automation;
79         delete edits;
80         plugin_set.remove_all_objects();
81 }
82
83 void Track::create_objects()
84 {
85 }
86
87
88 int Track::copy_settings(Track *track)
89 {
90         this->expand_view = track->expand_view;
91         this->draw = track->draw;
92         this->ganged = track->ganged;
93         this->master = track->master;
94         this->armed = track->armed;
95         this->nudge = track->nudge;
96         this->mixer_id = track->mixer_id;
97         this->play = track->play;
98         this->data_h = track->data_h;
99         this->track_w = track->track_w;
100         this->track_h = track->track_h;
101         this->masks = track->masks;
102         strcpy(this->title, track->title);
103         return 0;
104 }
105
106 int Track::get_id()
107 {
108         return id;
109 }
110
111
112 int Track::load_defaults(BC_Hash *defaults)
113 {
114         return 0;
115 }
116
117 void Track::equivalent_output(Track *track, double *result)
118 {
119         if(data_type != track->data_type ||
120                 track_w != track->track_w ||
121                 track_h != track->track_h ||
122                 play != track->play ||
123                 nudge != track->nudge)
124                 *result = 0;
125
126 // Convert result to track units
127         int64_t result2 = -1;
128         automation->equivalent_output(track->automation, &result2);
129         edits->equivalent_output(track->edits, &result2);
130
131         int plugin_sets = MIN(plugin_set.total, track->plugin_set.total);
132 // Test existing plugin sets
133         for(int i = 0; i < plugin_sets; i++)
134         {
135                 plugin_set.values[i]->equivalent_output(
136                         track->plugin_set.values[i],
137                         &result2);
138         }
139
140 // New EDL has more plugin sets.  Get starting plugin in new plugin sets
141         for(int i = plugin_sets; i < plugin_set.total; i++)
142         {
143                 Plugin *current = plugin_set.values[i]->get_first_plugin();
144                 if(current)
145                 {
146                         if(result2 < 0 || current->startproject < result2)
147                                 result2 = current->startproject;
148                 }
149         }
150
151 // New EDL has fewer plugin sets.  Get starting plugin in old plugin set
152         for(int i = plugin_sets; i < track->plugin_set.total; i++)
153         {
154                 Plugin *current = track->plugin_set.values[i]->get_first_plugin();
155                 if(current)
156                 {
157                         if(result2 < 0 || current->startproject < result2)
158                                 result2 = current->startproject;
159                 }
160         }
161
162 // Number of plugin sets differs but somehow we didn't find the start of the
163 // change.  Assume 0
164         if(track->plugin_set.total != plugin_set.total && result2 < 0)
165                 result2 = 0;
166
167         if(result2 >= 0 &&
168                 (*result < 0 || from_units(result2) < *result))
169                 *result = from_units(result2);
170 }
171
172
173 int Track::is_synthesis(int64_t position, int direction, int depth)
174 {
175         int result = 0;
176         for( int i = 0; !result && i<plugin_set.total; ++i ) {
177                 Plugin *plugin = get_current_plugin(position, i, direction, 0, 0);
178                 if( !plugin ) continue;
179 // Assume data from a shared track is synthesized
180                 result = plugin->plugin_type == PLUGIN_SHAREDMODULE ? 1 :
181                         plugin->is_synthesis(position, direction, depth);
182         }
183         return result;
184 }
185
186 void Track::copy_from(Track *track)
187 {
188         copy_settings(track);
189         edits->copy_from(track->edits);
190         this->plugin_set.remove_all_objects();
191
192         for( int i=0; i<track->plugin_set.total; ++i ) {
193                 PluginSet *new_plugin_set = plugin_set.append(new PluginSet(edl, this));
194                 new_plugin_set->copy_from(track->plugin_set.values[i]);
195         }
196         automation->copy_from(track->automation);
197         this->track_w = track->track_w;
198         this->track_h = track->track_h;
199 }
200
201 Track& Track::operator=(Track& track)
202 {
203 printf("Track::operator= 1\n");
204         copy_from(&track);
205         return *this;
206 }
207
208 int Track::vertical_span(Theme *theme)
209 {
210         int result = 0;
211         if( show_titles() )
212                 result += theme->get_image("title_bg_data")->get_h();
213         if( show_assets() )
214                 result += data_h;
215         if( expand_view )
216                 result += plugin_set.total * theme->get_image("plugin_bg_data")->get_h();
217         result = MAX(result, theme->title_h);
218         return result;
219 }
220
221 double Track::get_length()
222 {
223         double total_length = 0;
224         double length = 0;
225
226 // Test edits
227         if(edits->last)
228         {
229                 length = from_units(edits->last->startproject + edits->last->length);
230                 if(length > total_length) total_length = length;
231         }
232
233 // Test plugins
234         for(int i = 0; i < plugin_set.total; i++)
235         {
236                 if( !plugin_set.values[i]->last ) continue;
237                 length = from_units(plugin_set.values[i]->last->startproject +
238                         plugin_set.values[i]->last->length);
239                 if(length > total_length) total_length = length;
240         }
241
242 // Test keyframes
243         length = from_units(automation->get_length());
244         if(length > total_length) total_length = length;
245
246
247         return total_length;
248 }
249
250 int Track::has_speed()
251 {
252         FloatAutos *autos = (FloatAutos*)automation->autos[AUTOMATION_SPEED];
253         if(autos)
254         {
255                 if(autos->first)
256                 {
257                         for(FloatAuto *current = (FloatAuto*)autos->first;
258                                 current;
259                                 current = (FloatAuto*)current->next)
260                         {
261                                 if(!EQUIV(current->get_value(), 1.0) ||
262                                         !EQUIV(current->get_control_in_value(), 0.0) ||
263                                         !EQUIV(current->get_control_out_value(), 0.0))
264                                 {
265                                         return 1;
266                                 }
267                         }
268                 }
269         }
270
271         return 0;
272 }
273
274 int64_t Track::speed_length(int64_t start, int64_t end)
275 {
276         if( !has_speed() ) return end - start;
277         FloatAutos *speeds = (FloatAutos *) automation->autos[AUTOMATION_SPEED];
278         return speeds->automation_integral(start, end-start, PLAY_FORWARD);
279 }
280
281 int Track::show_assets()
282 {
283         return expand_view || edl->session->show_assets ? 1 : 0;
284 }
285
286 int Track::show_titles()
287 {
288         return expand_view || edl->session->show_titles ? 1 : 0;
289 }
290
291 int Track::show_transitions()
292 {
293         return expand_view || edl->session->auto_conf->transitions ? 1 : 0;
294 }
295
296 void Track::get_source_dimensions(double position, int &w, int &h)
297 {
298         int64_t native_position = to_units(position, 0);
299         for(Edit *current = edits->first; current; current = NEXT)
300         {
301                 if(current->startproject <= native_position &&
302                         current->startproject + current->length > native_position &&
303                         current->asset)
304                 {
305                         w = current->asset->width;
306                         h = current->asset->height;
307                         return;
308                 }
309         }
310 }
311
312
313 int64_t Track::horizontal_span()
314 {
315         return (int64_t)(get_length() *
316                 edl->session->sample_rate /
317                 edl->local_session->zoom_sample +
318                 0.5);
319 }
320
321
322 int Track::load(FileXML *file, int track_offset, uint32_t load_flags)
323 {
324         int result = 0;
325         int current_plugin = 0;
326
327
328         armed = file->tag.get_property("RECORD", armed);
329         play = file->tag.get_property("PLAY", play);
330         ganged = file->tag.get_property("GANG", ganged);
331         master = file->tag.get_property("MASTER", 1);
332         draw = file->tag.get_property("DRAW", draw);
333         nudge = file->tag.get_property("NUDGE", nudge);
334         mixer_id = file->tag.get_property("MIXER_ID", mixer_id);
335         expand_view = file->tag.get_property("EXPAND", expand_view);
336         data_h = file->tag.get_property("DATA_H", data_h);
337         track_w = file->tag.get_property("TRACK_W", track_w);
338         track_h = file->tag.get_property("TRACK_H", track_h);
339         masks = file->tag.get_property("MASKS", masks);
340
341         load_header(file, load_flags);
342
343         do{
344                 result = file->read_tag();
345
346                 if(!result)
347                 {
348                         if(file->tag.title_is("/TRACK"))
349                         {
350                                 result = 1;
351                         }
352                         else
353                         if(file->tag.title_is("TITLE"))
354                         {
355                                 XMLBuffer data;
356                                 file->read_text_until("/TITLE", &data);
357                                 memset(title, 0, sizeof(title));
358                                 strncpy(title, data.cstr(), sizeof(title)-1);
359                         }
360                         else
361                         if(load_flags && automation->load(file)
362                         /* strstr(file->tag.get_title(), "AUTOS") */)
363                         {
364                                 ;
365                         }
366                         else
367                         if(file->tag.title_is("EDITS"))
368                         {
369                                 if(load_flags & LOAD_EDITS)
370                                         edits->load(file, track_offset);
371                                 else
372                                         result = file->skip_tag();
373                         }
374                         else
375                         if(file->tag.title_is("PLUGINSET"))
376                         {
377                                 if(load_flags & LOAD_EDITS)
378                                 {
379                                         PluginSet *plugin_set = new PluginSet(edl, this);
380                                         this->plugin_set.append(plugin_set);
381                                         plugin_set->load(file, load_flags);
382                                 }
383                                 else
384                                 if(load_flags & LOAD_AUTOMATION)
385                                 {
386                                         if(current_plugin < this->plugin_set.total)
387                                         {
388                                                 PluginSet *plugin_set = this->plugin_set.values[current_plugin];
389                                                 plugin_set->load(file, load_flags);
390                                                 current_plugin++;
391                                         }
392                                 }
393                                 else
394                                         result = file->skip_tag();
395                         }
396                         else
397                                 load_derived(file, load_flags);
398                 }
399         }while(!result);
400
401
402
403         return 0;
404 }
405
406 void Track::insert_asset(Asset *asset,
407         EDL *nested_edl,
408         double length,
409         double position,
410         int track_number)
411 {
412         edits->insert_asset(asset,
413                 nested_edl,
414                 to_units(length, 1),
415                 to_units(position, 0),
416                 track_number);
417 }
418
419 // Insert data
420
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.
424
425 // Plugins:  This is an arbitrary behavior
426 //
427 // 1) No plugin in source track: Paste silence into destination
428 // plugin sets.
429 // 2) Plugin in source track: plugin in source track is inserted into
430 // existing destination track plugin sets, new sets being added when
431 // necessary.
432
433 void Track::insert_track(Track *track,
434         double position,
435         int replace_default,
436         int edit_plugins,
437         int edit_autos,
438         double edl_length)
439 {
440 // Calculate minimum length of data to pad.
441         int64_t min_length = to_units(
442                 MAX(edl_length, track->get_length()),
443                 1);
444 //printf("Track::insert_track %d %s %jd\n", __LINE__, title, min_length);
445
446 // Decide whether to copy settings based on load_mode
447         if(replace_default) copy_settings(track);
448
449         edits->insert_edits(track->edits,
450                 to_units(position, 0),
451                 min_length,
452                 edit_autos);
453
454         if(edit_plugins)
455                 insert_plugin_set(track,
456                         to_units(position, 0),
457                         min_length,
458                         edit_autos);
459
460         if(edit_autos)
461                 automation->insert_track(track->automation,
462                         to_units(position, 0),
463                         min_length,
464                         replace_default);
465
466         optimize();
467
468 }
469
470 // Called by insert_track
471 void Track::insert_plugin_set(Track *track,
472         int64_t position,
473         int64_t min_length,
474         int edit_autos)
475 {
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));
481
482                         plugin_set.values[i]->insert_edits(track->plugin_set.values[i],
483                                         position, min_length, edit_autos);
484                 }
485         }
486         else
487                 shift_effects(position, min_length, edit_autos, 0);
488 }
489
490 Plugin* Track::insert_effect(const char *title, SharedLocation *shared_location,
491                 KeyFrame *default_keyframe, PluginSet *plugin_set,
492                 double start, double length, int plugin_type)
493 {
494         if( !plugin_set ) {
495                 plugin_set = new PluginSet(edl, this);
496                 this->plugin_set.append(plugin_set);
497         }
498         Plugin *plugin = plugin_set->insert_plugin(title,
499                         to_units(start, 0), to_units(length, 1), plugin_type,
500                         shared_location, default_keyframe, 1);
501         expand_view = 1;
502         return plugin;
503 }
504
505 void Track::move_plugins_up(PluginSet *plugin_set)
506 {
507         for(int i = 0; i < this->plugin_set.total; i++)
508         {
509                 if(this->plugin_set.values[i] == plugin_set)
510                 {
511                         if(i == 0) break;
512
513                         PluginSet *temp = this->plugin_set.values[i - 1];
514                         this->plugin_set.values[i - 1] = this->plugin_set.values[i];
515                         this->plugin_set.values[i] = temp;
516
517                         SharedLocation old_location, new_location;
518                         new_location.module = old_location.module = tracks->number_of(this);
519                         old_location.plugin = i;
520                         new_location.plugin = i - 1;
521                         tracks->change_plugins(old_location, new_location, 1);
522                         break;
523                 }
524         }
525 }
526
527 void Track::move_plugins_down(PluginSet *plugin_set)
528 {
529         for(int i = 0; i < this->plugin_set.total; i++)
530         {
531                 if(this->plugin_set.values[i] == plugin_set)
532                 {
533                         if(i == this->plugin_set.total - 1) break;
534
535                         PluginSet *temp = this->plugin_set.values[i + 1];
536                         this->plugin_set.values[i + 1] = this->plugin_set.values[i];
537                         this->plugin_set.values[i] = temp;
538
539                         SharedLocation old_location, new_location;
540                         new_location.module = old_location.module = tracks->number_of(this);
541                         old_location.plugin = i;
542                         new_location.plugin = i + 1;
543                         tracks->change_plugins(old_location, new_location, 1);
544                         break;
545                 }
546         }
547 }
548
549
550 void Track::remove_asset(Indexable *asset)
551 {
552         for(Edit *edit = edits->first; edit; edit = edit->next)
553         {
554                 if(asset->is_asset &&
555                         edit->asset &&
556                         edit->asset == (Asset*)asset)
557                 {
558                         edit->asset = 0;
559                 }
560                 else
561                 if(!asset->is_asset &&
562                         edit->nested_edl &&
563                         edit->nested_edl == (EDL*)asset)
564                 {
565                         edit->nested_edl = 0;
566                 }
567         }
568         optimize();
569 }
570
571 void Track::remove_pluginset(PluginSet *plugin_set)
572 {
573         int i;
574         for(i = 0; i < this->plugin_set.total; i++)
575                 if(plugin_set == this->plugin_set.values[i]) break;
576
577         this->plugin_set.remove_object(plugin_set);
578         for( ++i ; i<=this->plugin_set.total; ++i ) {
579                 SharedLocation old_location, new_location;
580                 new_location.module = old_location.module = tracks->number_of(this);
581                 old_location.plugin = i;
582                 new_location.plugin = i - 1;
583                 tracks->change_plugins(old_location, new_location, 0);
584         }
585 }
586
587 void Track::shift_keyframes(int64_t position, int64_t length)
588 {
589         automation->paste_silence(position, position + length);
590 // Effect keyframes are shifted in shift_effects
591 }
592
593 void Track::shift_effects(int64_t position, int64_t length, int edit_autos, Edits *trim_edits)
594 {
595         for( int i=0; i<plugin_set.total; ++i ) {
596                 if( !trim_edits || trim_edits == (Edits*)plugin_set.values[i] )
597                         plugin_set.values[i]->shift_effects(position, length, edit_autos);
598         }
599 }
600
601 void Track::detach_effect(Plugin *plugin)
602 {
603 //printf("Track::detach_effect 1\n");
604         for( int i=0; i<plugin_set.size(); ++i ) {
605                 PluginSet *pluginset = plugin_set[i];
606                 Plugin *dest = (Plugin*)pluginset->first;
607                 while( dest && dest != plugin ) dest = (Plugin*)dest->next;
608                 if( !dest ) continue;
609                 tracks->detach_ganged_effects(plugin);
610                 int64_t start = plugin->startproject;
611                 int64_t end = start + plugin->length;
612                 pluginset->clear(start, end, 1);
613                 optimize();
614 //printf("Track::detach_effect 2 %d\n", plugin_set->length());
615 // Delete 0 length pluginsets
616                 return;
617         }
618 }
619
620 void Track::detach_shared_effects(int module)
621 {
622         for( int i=0; i<plugin_set.size(); ++i ) {
623                 PluginSet *pluginset = this->plugin_set[i];
624                 Plugin *dest = (Plugin*)pluginset->first;
625                 for( ; dest; dest=(Plugin*)dest->next ) {
626                         if( (dest->plugin_type != PLUGIN_SHAREDPLUGIN &&
627                              dest->plugin_type != PLUGIN_SHAREDMODULE) ) continue;
628                         if( dest->shared_location.module != module ) continue;
629                         int64_t start = dest->startproject;
630                         int64_t end = start + dest->length;
631                         pluginset->clear(start, end, 1);
632                 }
633         }
634         optimize();
635 }
636
637 void Track::detach_ganged_effects(Plugin *plugin)
638 {
639         for( int i=0; i<plugin_set.size(); ++i ) {
640                 PluginSet *pluginset = this->plugin_set[i];
641                 Plugin *dest = (Plugin*)pluginset->first;
642                 for( ; dest; dest=(Plugin*)dest->next ) {
643                         if( strcmp(dest->title, plugin->title) != 0 ) continue;
644                         if( dest->startproject != plugin->startproject ) continue;
645                         if( dest->length != plugin->length ) continue;
646                         int64_t start = dest->startproject;
647                         int64_t end = start + dest->length;
648                         pluginset->clear(start, end, 1);
649                 }
650         }
651         optimize();
652 }
653
654 void Track::resample(double old_rate, double new_rate)
655 {
656         edits->resample(old_rate, new_rate);
657         automation->resample(old_rate, new_rate);
658         for(int i = 0; i < plugin_set.total; i++)
659                 plugin_set.values[i]->resample(old_rate, new_rate);
660         nudge = (int64_t)(nudge * new_rate / old_rate);
661 }
662
663
664 void Track::optimize()
665 {
666         edits->optimize();
667         for(int i = 0; i < plugin_set.total; ) {
668                 PluginSet *plugin_set = this->plugin_set.values[i];
669                 plugin_set->optimize();
670 //printf("Track::optimize %d\n", plugin_set.values[i]->total());
671 // new definition of empty track...
672                 if( !plugin_set->last ||
673                     (plugin_set->last == plugin_set->first &&
674                      plugin_set->last->silence()) ) {
675                         remove_pluginset(plugin_set);
676                         continue;
677                 }
678                 ++i;
679         }
680 }
681
682 Plugin* Track::get_current_plugin(double position,
683         int plugin_set,
684         int direction,
685         int convert_units,
686         int use_nudge)
687 {
688         Plugin *current;
689         if(convert_units) position = to_units(position, 0);
690         if(use_nudge) position += nudge;
691
692         if(plugin_set >= this->plugin_set.total || plugin_set < 0) return 0;
693
694 //printf("Track::get_current_plugin 1 %d %d %d\n", position, this->plugin_set.total, direction);
695         if(direction == PLAY_FORWARD)
696         {
697                 for(current = (Plugin*)this->plugin_set.values[plugin_set]->last;
698                         current;
699                         current = (Plugin*)PREVIOUS)
700                 {
701 // printf("Track::get_current_plugin 2 %d %ld %ld\n",
702 // current->startproject,
703 // current->startproject + current->length,
704 // position);
705                         if(current->startproject <= position &&
706                                 current->startproject + current->length > position)
707                         {
708                                 return current;
709                         }
710                 }
711         }
712         else
713         if(direction == PLAY_REVERSE)
714         {
715                 for(current = (Plugin*)this->plugin_set.values[plugin_set]->first;
716                         current;
717                         current = (Plugin*)NEXT)
718                 {
719                         if(current->startproject < position &&
720                                 current->startproject + current->length >= position)
721                         {
722                                 return current;
723                         }
724                 }
725         }
726
727         return 0;
728 }
729
730 Plugin* Track::get_current_transition(double position,
731         int direction,
732         int convert_units,
733         int use_nudge)
734 {
735         Edit *current;
736         Plugin *result = 0;
737         if(convert_units) position = to_units(position, 0);
738         if(use_nudge) position += nudge;
739
740         if(direction == PLAY_FORWARD)
741         {
742                 for(current = edits->last; current; current = PREVIOUS)
743                 {
744                         if(current->startproject <= position && current->startproject + current->length > position)
745                         {
746 //printf("Track::get_current_transition %p\n", current->transition);
747                                 if(current->transition && position < current->startproject + current->transition->length)
748                                 {
749                                         result = current->transition;
750                                         break;
751                                 }
752                         }
753                 }
754         }
755         else
756         if(direction == PLAY_REVERSE)
757         {
758                 for(current = edits->first; current; current = NEXT)
759                 {
760                         if(current->startproject < position && current->startproject + current->length >= position)
761                         {
762                                 if(current->transition && position <= current->startproject + current->transition->length)
763                                 {
764                                         result = current->transition;
765                                         break;
766                                 }
767                         }
768                 }
769         }
770
771         return result;
772 }
773
774 void Track::synchronize_params(Track *track)
775 {
776         for(Edit *this_edit = edits->first, *that_edit = track->edits->first;
777                 this_edit && that_edit;
778                 this_edit = this_edit->next, that_edit = that_edit->next)
779         {
780                 this_edit->synchronize_params(that_edit);
781         }
782
783         for(int i = 0; i < plugin_set.total && i < track->plugin_set.total; i++)
784                 plugin_set.values[i]->synchronize_params(track->plugin_set.values[i]);
785
786         automation->copy_from(track->automation);
787         this->nudge = track->nudge;
788 }
789
790
791 int Track::dump(FILE *fp)
792 {
793         fprintf(fp,"   Title %s\n", title);
794         fprintf(fp,"   Data type %d, draw %d, gang %d, master %d, mixer_id %d\n"
795                    "      play %d, armed %d, nudge %jd, masks 0x%04x\n",
796                 data_type, draw, ganged, master, mixer_id, play, armed, nudge, masks);
797         fprintf(fp,"   Edits:\n");
798         for(Edit* current = edits->first; current; current = NEXT)
799                 current->dump(fp);
800         automation->dump(fp);
801         fprintf(fp,"   Plugin Sets: %d\n", plugin_set.total);
802
803         for( int i=0; i<plugin_set.total; ++i )
804                 plugin_set[i]->dump(fp);
805         return 0;
806 }
807
808 // ======================================== accounting
809
810 int Track::number_of()
811 {
812         return tracks->number_of(this);
813 }
814
815
816
817 // ================================================= editing
818
819 int Track::select_auto(AutoConf *auto_conf, int cursor_x, int cursor_y)
820 {
821         return 0;
822 }
823
824 int Track::move_auto(AutoConf *auto_conf, int cursor_x, int cursor_y, int shift_down)
825 {
826         return 0;
827 }
828
829 int Track::release_auto()
830 {
831         return 0;
832 }
833
834 // used for copying automation alone
835 int Track::copy_automation(double selectionstart,
836         double selectionend,
837         FileXML *file,
838         int default_only,
839         int active_only)
840 {
841         int64_t start = to_units(selectionstart, 0);
842         int64_t end = to_units(selectionend, 1);
843
844         file->tag.set_title("TRACK");
845 // Video or audio
846     save_header(file);
847         file->append_tag();
848         file->append_newline();
849
850         automation->copy(start, end, file, default_only, active_only);
851
852         if(edl->session->auto_conf->plugins)
853         {
854                 for(int i = 0; i < plugin_set.total; i++)
855                 {
856
857                         plugin_set.values[i]->copy_keyframes(start,
858                                 end,
859                                 file,
860                                 default_only,
861                                 active_only);
862                 }
863         }
864
865         file->tag.set_title("/TRACK");
866         file->append_tag();
867         file->append_newline();
868         file->append_newline();
869         file->append_newline();
870         file->append_newline();
871
872         return 0;
873 }
874
875 int Track::paste_automation(double selectionstart, double total_length,
876         double frame_rate, int64_t sample_rate,
877         FileXML *file, int default_only, int active_only)
878 {
879 // Only used for pasting automation alone.
880         double scale = data_type == TRACK_AUDIO ?
881                 edl->session->sample_rate / sample_rate :
882                 edl->session->frame_rate / frame_rate ;
883
884         total_length *= scale;
885         int64_t start = to_units(selectionstart, 0);
886         int64_t length = to_units(total_length, 1);
887         int result = 0;
888         int current_pluginset = 0;
889 //printf("Track::paste_automation 1\n");
890
891         while( !(result = file->read_tag()) ) {
892                 if( file->tag.title_is("/TRACK") ) break;
893                 if( automation->paste(start, length, scale, file,
894                                 default_only, active_only, 0) )
895                         continue;
896                 if( file->tag.title_is("PLUGINSET") ) {
897                         if( current_pluginset < plugin_set.total ) {
898                                 plugin_set.values[current_pluginset]->
899                                         paste_keyframes(start, length, file,
900                                                 default_only, active_only);
901                                 ++current_pluginset;
902                         }
903                 }
904         }
905
906         return 0;
907 }
908
909 void Track::clear_automation(double selectionstart,
910         double selectionend,
911         int shift_autos,
912         int default_only)
913 {
914         int64_t start = to_units(selectionstart, 0);
915         int64_t end = to_units(selectionend, 1);
916
917         automation->clear(start, end, edl->session->auto_conf, 0);
918
919         if(edl->session->auto_conf->plugins)
920         {
921                 for(int i = 0; i < plugin_set.total; i++)
922                 {
923                         plugin_set.values[i]->clear_keyframes(start, end);
924                 }
925         }
926
927 }
928
929 void Track::set_automation_mode(double selectionstart,
930         double selectionend,
931         int mode)
932 {
933         int64_t start = to_units(selectionstart, 0);
934         int64_t end = to_units(selectionend, 1);
935
936         automation->set_automation_mode(start, end, mode, edl->session->auto_conf);
937 }
938
939
940
941
942 int Track::copy(int copy_flags, double start, double end,
943                 FileXML *file, const char *output_path)
944 {
945 // Use a copy of the selection in converted units
946 // So copy_automation doesn't reconvert.
947         int64_t start_unit = to_units(start, 0);
948         int64_t end_unit = to_units(end, 1);
949
950
951
952
953         file->tag.set_title("TRACK");
954         file->tag.set_property("RECORD", armed);
955         file->tag.set_property("NUDGE", nudge);
956         file->tag.set_property("MIXER_ID", mixer_id);
957         file->tag.set_property("PLAY", play);
958         file->tag.set_property("GANG", ganged);
959         file->tag.set_property("MASTER", master);
960         file->tag.set_property("DRAW", draw);
961         file->tag.set_property("EXPAND", expand_view);
962         file->tag.set_property("DATA_H", data_h);
963         file->tag.set_property("TRACK_W", track_w);
964         file->tag.set_property("TRACK_H", track_h);
965         file->tag.set_property("MASKS", masks);
966         save_header(file);
967         file->append_tag();
968         file->append_newline();
969         save_derived(file);
970
971         file->tag.set_title("TITLE");
972         file->append_tag();
973         file->append_text(title);
974         file->tag.set_title("/TITLE");
975         file->append_tag();
976         file->append_newline();
977
978 //      if(data_type == TRACK_AUDIO)
979 //              file->tag.set_property("TYPE", "AUDIO");
980 //      else
981 //              file->tag.set_property("TYPE", "VIDEO");
982 //
983 //      file->append_tag();
984 //      file->append_newline();
985
986         if( (copy_flags & COPY_EDITS) )
987                 edits->copy(start_unit, end_unit, file, output_path);
988
989         if( (copy_flags & COPY_AUTOS) ) {
990                 AutoConf auto_conf;
991                 auto_conf.set_all(1);
992                 automation->copy(start_unit, end_unit, file, 0, 0);
993         }
994
995         if( (copy_flags & COPY_PLUGINS) ) {
996                 for( int i=0; i<plugin_set.total; ++i )
997                         plugin_set.values[i]->copy(start_unit, end_unit, file);
998         }
999
1000         copy_derived(start_unit, end_unit, file);
1001
1002         file->tag.set_title("/TRACK");
1003         file->append_tag();
1004         file->append_newline();
1005         file->append_newline();
1006         file->append_newline();
1007         file->append_newline();
1008
1009         return 0;
1010 }
1011
1012 int Track::copy_assets(double start,
1013         double end,
1014         ArrayList<Asset*> *asset_list)
1015 {
1016         int i, result = 0;
1017
1018         start = to_units(start, 0);
1019         end = to_units(end, 1);
1020
1021         Edit *current = edits->editof((int64_t)start, PLAY_FORWARD, 0);
1022
1023 // Search all edits
1024         while(current && current->startproject < end)
1025         {
1026 // Check for duplicate assets
1027                 if(current->asset)
1028                 {
1029                         for(i = 0, result = 0; i < asset_list->total; i++)
1030                         {
1031                                 if(asset_list->values[i] == current->asset) result = 1;
1032                         }
1033 // append pointer to new asset
1034                         if(!result) asset_list->append(current->asset);
1035                 }
1036
1037                 current = NEXT;
1038         }
1039
1040         return 0;
1041 }
1042
1043 int Track::blade(double position)
1044 {
1045         int64_t start = to_units(position, 0);
1046         Edit *edit = edits->split_edit(start);
1047         if( !edit || edit->silence() ) return 1;
1048         edit->hard_left = 1;
1049         if( edit->previous ) edit->previous->hard_right = 1;
1050         return 0;
1051 }
1052
1053 int Track::clear(double start, double end,
1054         int edit_edits, int edit_labels, int edit_plugins,
1055         int edit_autos, Edits *trim_edits)
1056 {
1057         return clear(to_units(start, 0), to_units(end, 1),
1058                 edit_edits, edit_labels, edit_plugins, edit_autos, trim_edits);
1059 }
1060
1061 int Track::clear(int64_t start, int64_t end,
1062         int edit_edits, int edit_labels, int edit_plugins,
1063         int edit_autos, Edits *trim_edits)
1064 {
1065         if( edit_edits )
1066                 edits->clear(start, end);
1067 //printf("Track::clear 1 %d %d %d\n", edit_edits, edit_labels, edit_plugins);
1068         if( edit_autos )
1069                 automation->clear(start, end, 0, 1);
1070         if( edit_plugins ) {
1071                 int edit_keyframes = edit_plugins < 0 ? 1 : edit_autos;
1072                 for(int i = 0; i < plugin_set.total; i++) {
1073                         if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1074                                 plugin_set.values[i]->clear(start, end, edit_keyframes);
1075                 }
1076         }
1077         return 0;
1078 }
1079
1080 int Track::clear_handle(double start,
1081         double end,
1082         int clear_labels,
1083         int clear_plugins,
1084         int edit_autos,
1085         double &distance)
1086 {
1087         edits->clear_handle(start, end, clear_plugins, edit_autos, distance);
1088         return 0;
1089 }
1090
1091 int Track::popup_transition(int cursor_x, int cursor_y)
1092 {
1093         return 0;
1094 }
1095
1096
1097
1098 int Track::modify_edithandles(double oldposition, double newposition,
1099         int currentend, int handle_mode, int edit_labels,
1100         int edit_plugins, int edit_autos, int group_id)
1101 {
1102         edits->modify_handles(oldposition, newposition,
1103                 currentend, handle_mode, 1, edit_labels, edit_plugins,
1104                 edit_autos, 0, group_id);
1105         return 0;
1106 }
1107
1108 int Track::modify_pluginhandles(double oldposition,
1109         double newposition,
1110         int currentend,
1111         int handle_mode,
1112         int edit_labels,
1113         int edit_autos,
1114         Edits *trim_edits)
1115 {
1116         for(int i = 0; i < plugin_set.total; i++)
1117         {
1118                 if(!trim_edits || trim_edits == (Edits*)plugin_set.values[i])
1119                         plugin_set.values[i]->modify_handles(oldposition, newposition,
1120 // Don't allow plugin tweeks to affect edits.
1121                                 currentend, handle_mode, 0, 0, 0, 0, 0, 0);
1122         }
1123         return 0;
1124 }
1125
1126
1127 int Track::paste_silence(double start, double end, int edit_plugins, int edit_autos)
1128 {
1129         return paste_silence(to_units(start, 0), to_units(end, 1),
1130                         edit_plugins, edit_autos);
1131 }
1132
1133 int Track::paste_silence(int64_t start, int64_t end, int edit_plugins, int edit_autos)
1134 {
1135         edits->paste_silence(start, end);
1136         if( edit_autos )
1137                 shift_keyframes(start, end - start);
1138         if( edit_plugins )
1139                 shift_effects(start, end - start, edit_autos, 0);
1140         edits->optimize();
1141         return 0;
1142 }
1143
1144 int Track::select_edit(int cursor_x,
1145         int cursor_y,
1146         double &new_start,
1147         double &new_end)
1148 {
1149         return 0;
1150 }
1151
1152 int Track::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
1153 {
1154         return 0;
1155 }
1156
1157 void Track::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
1158 {
1159         for(int i = 0; i < plugin_set.total; i++)
1160         {
1161                 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1162                         plugin;
1163                         plugin = (Plugin*)plugin->next)
1164                 {
1165                         if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN)
1166                         {
1167                                 if(plugin->shared_location == old_location)
1168                                         plugin->shared_location = new_location;
1169                                 else
1170                                 if(do_swap && plugin->shared_location == new_location)
1171                                         plugin->shared_location = old_location;
1172                         }
1173                 }
1174         }
1175 }
1176
1177 void Track::change_modules(int old_location, int new_location, int do_swap)
1178 {
1179         for(int i = 0; i < plugin_set.total; i++)
1180         {
1181                 for(Plugin *plugin = (Plugin*)plugin_set.values[i]->first;
1182                         plugin;
1183                         plugin = (Plugin*)plugin->next)
1184                 {
1185                         if(plugin->plugin_type == PLUGIN_SHAREDPLUGIN ||
1186                                 plugin->plugin_type == PLUGIN_SHAREDMODULE)
1187                         {
1188                                 if(plugin->shared_location.module == old_location)
1189                                         plugin->shared_location.module = new_location;
1190                                 else
1191                                 if(do_swap && plugin->shared_location.module == new_location)
1192                                         plugin->shared_location.module = old_location;
1193                         }
1194                 }
1195         }
1196 }
1197
1198
1199 int Track::playable_edit(int64_t position, int direction)
1200 {
1201         int result = 0;
1202         if(direction == PLAY_REVERSE) position--;
1203         for(Edit *current = edits->first; current && !result; current = NEXT)
1204         {
1205                 if(current->startproject <= position &&
1206                         current->startproject + current->length > position)
1207                 {
1208 //printf("Track::playable_edit %p %p\n", current->transition, current->asset);
1209                         if(current->transition ||
1210                                 current->asset ||
1211                                 current->nested_edl) result = 1;
1212                 }
1213         }
1214         return result;
1215 }
1216
1217
1218 int Track::need_edit(Edit *current, int test_transitions)
1219 {
1220         return ((test_transitions && current->transition) ||
1221                 (!test_transitions && current->asset));
1222 }
1223
1224 int64_t Track::plugin_change_duration(int64_t input_position,
1225         int64_t input_length,
1226         int reverse,
1227         int use_nudge)
1228 {
1229         if(use_nudge) input_position += nudge;
1230         for(int i = 0; i < plugin_set.total; i++)
1231         {
1232                 int64_t new_duration = plugin_set.values[i]->plugin_change_duration(
1233                         input_position,
1234                         input_length,
1235                         reverse);
1236                 if(new_duration < input_length) input_length = new_duration;
1237         }
1238         return input_length;
1239 }
1240
1241 int64_t Track::edit_change_duration(int64_t input_position,
1242         int64_t input_length,
1243         int reverse,
1244         int test_transitions,
1245         int use_nudge)
1246 {
1247         Edit *current;
1248         int64_t edit_length = input_length;
1249         if(use_nudge) input_position += nudge;
1250
1251         if(reverse)
1252         {
1253 // ================================= Reverse playback
1254 // Get first edit on or after position
1255                 for(current = edits->first;
1256                         current && current->startproject + current->length <= input_position;
1257                         current = NEXT)
1258                         ;
1259
1260                 if(current)
1261                 {
1262                         if(current->startproject > input_position)
1263                         {
1264 // Before first edit
1265                                 ;
1266                         }
1267                         else
1268                         if(need_edit(current, test_transitions))
1269                         {
1270 // Over an edit of interest.
1271                                 if(input_position - current->startproject < input_length)
1272                                         edit_length = input_position - current->startproject + 1;
1273                         }
1274                         else
1275                         {
1276 // Over an edit that isn't of interest.
1277 // Search for next edit of interest.
1278                                 for(current = PREVIOUS ;
1279                                         current &&
1280                                         current->startproject + current->length > input_position - input_length &&
1281                                         !need_edit(current, test_transitions);
1282                                         current = PREVIOUS)
1283                                         ;
1284
1285                                         if(current &&
1286                                                 need_edit(current, test_transitions) &&
1287                                                 current->startproject + current->length > input_position - input_length)
1288                         edit_length = input_position - current->startproject - current->length + 1;
1289                         }
1290                 }
1291                 else
1292                 {
1293 // Not over an edit.  Try the last edit.
1294                         current = edits->last;
1295                         if(current &&
1296                                 ((test_transitions && current->transition) ||
1297                                 (!test_transitions && current->asset)))
1298                                 edit_length = input_position - edits->length() + 1;
1299                 }
1300         }
1301         else
1302         {
1303 // =================================== forward playback
1304 // Get first edit on or before position
1305                 for(current = edits->last;
1306                         current && current->startproject > input_position;
1307                         current = PREVIOUS)
1308                         ;
1309
1310                 if(current)
1311                 {
1312                         if(current->startproject + current->length <= input_position)
1313                         {
1314 // Beyond last edit.
1315                                 ;
1316                         }
1317                         else
1318                         if(need_edit(current, test_transitions))
1319                         {
1320 // Over an edit of interest.
1321 // Next edit is going to require a change.
1322                                 if(current->length + current->startproject - input_position < input_length)
1323                                         edit_length = current->startproject + current->length - input_position;
1324                         }
1325                         else
1326                         {
1327 // Over an edit that isn't of interest.
1328 // Search for next edit of interest.
1329                                 for(current = NEXT ;
1330                                         current &&
1331                                         current->startproject < input_position + input_length &&
1332                                         !need_edit(current, test_transitions);
1333                                         current = NEXT)
1334                                         ;
1335
1336                                         if(current &&
1337                                                 need_edit(current, test_transitions) &&
1338                                                 current->startproject < input_position + input_length)
1339                                                 edit_length = current->startproject - input_position;
1340                         }
1341                 }
1342                 else
1343                 {
1344 // Not over an edit.  Try the first edit.
1345                         current = edits->first;
1346                         if(current &&
1347                                 ((test_transitions && current->transition) ||
1348                                 (!test_transitions && current->asset)))
1349                                 edit_length = edits->first->startproject - input_position;
1350                 }
1351         }
1352
1353         if(edit_length < input_length)
1354                 return edit_length;
1355         else
1356                 return input_length;
1357 }
1358
1359 void Track::shuffle_edits(double start, double end, int first_track)
1360 {
1361         ArrayList<Edit*> new_edits;
1362         ArrayList<Label*> new_labels;
1363         int64_t start_units = to_units(start, 0);
1364         int64_t end_units = to_units(end, 0);
1365 // Sample range of all edits selected
1366         //int64_t total_start_units = 0;
1367         //int64_t total_end_units = 0;
1368 // Edit before range
1369         Edit *start_edit = 0;
1370         int have_start_edit = 0;
1371
1372 // Move all edit pointers to list
1373         for(Edit *current = edits->first;
1374                 current; )
1375         {
1376                 if(current->startproject >= start_units &&
1377                         current->startproject + current->length <= end_units)
1378                 {
1379                         if(!have_start_edit) start_edit = current->previous;
1380                         have_start_edit = 1;
1381                         //total_start_units = current->startproject;
1382                         //total_end_units = current->startproject + current->length;
1383                         new_edits.append(current);
1384
1385 // Move label pointers
1386                         if(first_track && edl->session->labels_follow_edits)
1387                         {
1388                                 double start_seconds = from_units(current->startproject);
1389                                 double end_seconds = from_units(current->startproject +
1390                                         current->length);
1391                                 for(Label *label = edl->labels->first;
1392                                         label;
1393                                         label = label->next)
1394                                 {
1395                                         if(label->position >= start_seconds &&
1396                                                 label->position < end_seconds)
1397                                         {
1398                                                 new_labels.append(label);
1399                                                 edl->labels->remove_pointer(label);
1400                                         }
1401                                 }
1402                         }
1403
1404 // Remove edit pointer
1405                         Edit *previous = current;
1406                         current = NEXT;
1407                         edits->remove_pointer(previous);
1408                 }
1409                 else
1410                 {
1411                         current = NEXT;
1412                 }
1413         }
1414
1415 // Insert pointers in random order
1416         while(new_edits.size())
1417         {
1418                 int index = rand() % new_edits.size();
1419                 Edit *edit = new_edits.get(index);
1420                 new_edits.remove_number(index);
1421                 if( !start_edit )
1422                         edits->insert_before(edits->first, edit);
1423                 else
1424                         edits->insert_after(start_edit, edit);
1425                 start_edit = edit;
1426
1427 // Recalculate start position
1428 // Save old position for moving labels
1429                 int64_t startproject1 = edit->startproject;
1430                 int64_t startproject2 = 0;
1431                 if(edit->previous)
1432                 {
1433                         edit->startproject =
1434                                 startproject2 =
1435                                 edit->previous->startproject + edit->previous->length;
1436                 }
1437                 else
1438                 {
1439                         edit->startproject = startproject2 = 0;
1440                 }
1441
1442
1443 // Insert label pointers
1444                 if(first_track && edl->session->labels_follow_edits)
1445                 {
1446                         double start_seconds1 = from_units(startproject1);
1447                         double end_seconds1 = from_units(startproject1 + edit->length);
1448                         double start_seconds2 = from_units(startproject2);
1449                         for(int i = new_labels.size() - 1; i >= 0; i--)
1450                         {
1451                                 Label *label = new_labels.get(i);
1452 // Was in old edit position
1453                                 if(label->position >= start_seconds1 &&
1454                                         label->position < end_seconds1)
1455                                 {
1456 // Move to new edit position
1457                                         double position = label->position -
1458                                                 start_seconds1 + start_seconds2;
1459                                         edl->labels->insert_label(position);
1460                                         new_labels.remove_object_number(i);
1461                                 }
1462                         }
1463                 }
1464
1465
1466         }
1467
1468         optimize();
1469
1470         if(first_track && edl->session->labels_follow_edits)
1471         {
1472                 edl->labels->optimize();
1473         }
1474 }
1475
1476 // exactly the same as shuffle_edits except for 1 line
1477 void Track::reverse_edits(double start, double end, int first_track)
1478 {
1479         ArrayList<Edit*> new_edits;
1480         ArrayList<Label*> new_labels;
1481         int64_t start_units = to_units(start, 0);
1482         int64_t end_units = to_units(end, 0);
1483 // Sample range of all edits selected
1484         //int64_t total_start_units = 0;
1485         //int64_t total_end_units = 0;
1486 // Edit before range
1487         Edit *start_edit = 0;
1488         int have_start_edit = 0;
1489
1490 // Move all edit pointers to list
1491         for(Edit *current = edits->first; current; )
1492         {
1493                 if(current->startproject >= start_units &&
1494                         current->startproject + current->length <= end_units)
1495                 {
1496                         if(!have_start_edit) start_edit = current->previous;
1497                         have_start_edit = 1;
1498                         //total_start_units = current->startproject;
1499                         //total_end_units = current->startproject + current->length;
1500                         new_edits.append(current);
1501
1502 // Move label pointers
1503                         if(first_track && edl->session->labels_follow_edits)
1504                         {
1505                                 double start_seconds = from_units(current->startproject);
1506                                 double end_seconds = from_units(current->startproject +
1507                                         current->length);
1508                                 for(Label *label = edl->labels->first;
1509                                         label;
1510                                         label = label->next)
1511                                 {
1512                                         if(label->position >= start_seconds &&
1513                                                 label->position < end_seconds)
1514                                         {
1515                                                 new_labels.append(label);
1516                                                 edl->labels->remove_pointer(label);
1517                                         }
1518                                 }
1519                         }
1520
1521 // Remove edit pointer
1522                         Edit *previous = current;
1523                         current = NEXT;
1524                         edits->remove_pointer(previous);
1525                 }
1526                 else
1527                 {
1528                         current = NEXT;
1529                 }
1530         }
1531
1532 // Insert pointers in reverse order
1533         while(new_edits.size())
1534         {
1535                 int index = new_edits.size() - 1;
1536                 Edit *edit = new_edits.get(index);
1537                 new_edits.remove_number(index);
1538                 if( !start_edit )
1539                         edits->insert_before(edits->first, edit);
1540                 else
1541                         edits->insert_after(start_edit, edit);
1542                 start_edit = edit;
1543
1544 // Recalculate start position
1545 // Save old position for moving labels
1546                 int64_t startproject1 = edit->startproject;
1547                 int64_t startproject2 = 0;
1548                 if(edit->previous)
1549                 {
1550                         edit->startproject =
1551                                 startproject2 =
1552                                 edit->previous->startproject + edit->previous->length;
1553                 }
1554                 else
1555                 {
1556                         edit->startproject = startproject2 = 0;
1557                 }
1558
1559
1560 // Insert label pointers
1561                 if(first_track && edl->session->labels_follow_edits)
1562                 {
1563                         double start_seconds1 = from_units(startproject1);
1564                         double end_seconds1 = from_units(startproject1 + edit->length);
1565                         double start_seconds2 = from_units(startproject2);
1566                         for(int i = new_labels.size() - 1; i >= 0; i--)
1567                         {
1568                                 Label *label = new_labels.get(i);
1569 // Was in old edit position
1570                                 if(label->position >= start_seconds1 &&
1571                                         label->position < end_seconds1)
1572                                 {
1573 // Move to new edit position
1574                                         double position = label->position -
1575                                                 start_seconds1 + start_seconds2;
1576                                         edl->labels->insert_label(position);
1577                                         new_labels.remove_object_number(i);
1578                                 }
1579                         }
1580                 }
1581
1582
1583         }
1584
1585         optimize();
1586
1587         if(first_track && edl->session->labels_follow_edits)
1588         {
1589                 edl->labels->optimize();
1590         }
1591 }
1592
1593 void Track::align_edits(double start, double end, Track *master_track)
1594 {
1595         int64_t start_units = to_units(start, 0);
1596         int64_t end_units = to_units(end, 0);
1597
1598 // All other tracks get silence or cut to align the edits on the times.
1599         Edit *master = master_track->edits->first;
1600         for(Edit *current = edits->first; current && master; ) {
1601                 if( current->startproject >= start_units &&
1602                     current->startproject + current->length <= end_units ) {
1603 // edit is in highlighted region
1604                         int64_t master_length_units = to_units(master_track->from_units(master->length), 0);
1605 // starting time of master edit
1606                         int64_t master_start_units = to_units(master_track->from_units(master->startproject), 0);
1607 // starting time of current edit
1608                         int64_t current_startunits = current->startproject;
1609
1610 // the following occur if multiple aligns are performed
1611 // master edit is not silence but current edit is silence
1612                         if( !master->silence() && current->silence() ) {
1613 // try again with next edit
1614                                 current = NEXT;
1615                                 continue;
1616                         }
1617                         if( master->silence() && !current->silence() ) {
1618 // master edit is silence but current edit is not silence
1619                                 master = master->next;
1620                                 continue;
1621                         }
1622                         if( current->length < master_length_units / 2 ) {
1623 // current edit is a glitch edit between 2 required edits
1624                                 current = NEXT;
1625                                 continue;
1626                         }
1627
1628                         current = NEXT;
1629 // current edit starts before master edit
1630                         if( current_startunits < master_start_units ) {
1631 //printf("Track::align_edits %d\n", __LINE__);
1632                                 edits->paste_silence(current_startunits, master_start_units);
1633                                 shift_keyframes(current_startunits,
1634                                         master_start_units - current_startunits);
1635                         }
1636                         else if( current_startunits > master_start_units ) {
1637 // current edit starts after master edit
1638                                 edits->clear(master_start_units,
1639                                         current_startunits);
1640                                 if(edl->session->autos_follow_edits)
1641                                         shift_keyframes(master_start_units,
1642                                                 current_startunits - master_start_units);
1643                         }
1644
1645                         master = master->next;
1646                 }
1647                 else {
1648                         current = NEXT;
1649                         master = master->next;
1650                 }
1651         }
1652
1653         optimize();
1654 }
1655
1656 int Track::purge_asset(Asset *asset)
1657 {
1658         return 0;
1659 }
1660
1661 int Track::asset_used(Asset *asset)
1662 {
1663         Edit* current_edit;
1664         int result = 0;
1665
1666         for(current_edit = edits->first; current_edit; current_edit = current_edit->next)
1667         {
1668                 if(current_edit->asset == asset)
1669                 {
1670                         result++;
1671                 }
1672         }
1673         return result;
1674 }
1675
1676 int Track::is_playable(int64_t position, int direction)
1677 {
1678         return 1;
1679 }
1680
1681
1682 int Track::plugin_used(int64_t position, int64_t direction)
1683 {
1684 //printf("Track::plugin_used 1 %d\n", this->plugin_set.total);
1685         for(int i = 0; i < this->plugin_set.total; i++)
1686         {
1687                 Plugin *current_plugin = get_current_plugin(position,
1688                         i,
1689                         direction,
1690                         0,
1691                         0);
1692
1693 //printf("Track::plugin_used 2 %p %d %d\n", current_plugin, current_plugin->on, current_plugin->plugin_type);
1694                 if(current_plugin &&
1695                         current_plugin->on &&
1696                         current_plugin->plugin_type != PLUGIN_NONE)
1697                 {
1698                         return 1;
1699                 }
1700         }
1701 //printf("Track::plugin_used 3 %p\n", current_plugin);
1702         return 0;
1703 }
1704
1705 // Audio is always rendered through VConsole
1706 int Track::direct_copy_possible(int64_t start, int direction, int use_nudge)
1707 {
1708         return 1;
1709 }
1710
1711 int64_t Track::to_units(double position, int round)
1712 {
1713         return (int64_t)position;
1714 }
1715
1716 double Track::to_doubleunits(double position)
1717 {
1718         return position;
1719 }
1720
1721 double Track::from_units(int64_t position)
1722 {
1723         return (double)position;
1724 }
1725
1726 int64_t Track::frame_align(int64_t position, int round)
1727 {
1728         if( data_type != TRACK_VIDEO && edl->session->cursor_on_frames )
1729                 position = to_units(edl->align_to_frame(from_units(position), round), round);
1730         return position;
1731 }
1732
1733 Plugin *Track::plugin_exists(int plugin_id)
1734 {
1735         for( int i=0; i<plugin_set.size(); ++i ) {
1736                 PluginSet *set = plugin_set.get(i);
1737                 Plugin *plugin = (Plugin*)set->first;
1738                 for( ; plugin; plugin=(Plugin*)plugin->next ) {
1739                         if( plugin->orig_id == plugin_id )
1740                                 return plugin;
1741                 }
1742         }
1743         for( Edit *current=edits->first; current; current=NEXT ) {
1744                 Plugin *plugin = (Plugin *)current->transition;
1745                 if( plugin && plugin->orig_id == plugin_id )
1746                         return plugin;
1747         }
1748         return 0;
1749 }
1750
1751 int Track::get_mixer_id()
1752 {
1753         if( mixer_id < 0 ) {
1754                 int v = 0;
1755                 for( Track *track=tracks->first; track!=0; track=track->next )
1756                         if( track->mixer_id > v ) v = track->mixer_id;
1757                 mixer_id = v + 1;
1758         }
1759         return mixer_id;
1760 }
1761
1762 void Track::get_fauto_xyz(int fauto, float &x, float &y, float &z)
1763 {
1764         FloatAutos **fautos = (FloatAutos **)&automation->autos;
1765         FloatAuto *xauto = (FloatAuto *)fautos[fauto+0]->get_auto_for_editing(-1, 1);
1766         if( xauto ) x = xauto->get_value();
1767         FloatAuto *yauto = (FloatAuto *)fautos[fauto+1]->get_auto_for_editing(-1, 1);
1768         if( yauto ) y = yauto->get_value();
1769         FloatAuto *zauto = (FloatAuto *)fautos[fauto+2]->get_auto_for_editing(-1, 1);
1770         if( zauto ) z = zauto->get_value();
1771 }
1772 void Track::set_fauto_xyz(int fauto, float x, float y, float z)
1773 {
1774         FloatAutos **fautos = (FloatAutos **)&automation->autos;
1775         FloatAuto *xauto = (FloatAuto *)fautos[fauto+0]->get_auto_for_editing(-1, 1);
1776         if( xauto ) xauto->set_value(x);
1777         FloatAuto *yauto = (FloatAuto *)fautos[fauto+1]->get_auto_for_editing(-1, 1);
1778         if( yauto ) yauto->set_value(y);
1779         FloatAuto *zauto = (FloatAuto *)fautos[fauto+2]->get_auto_for_editing(-1, 1);
1780         if( zauto ) zauto->set_value(z);
1781 }
1782
1783 void Track::get_projector(float &x, float &y, float &z)
1784 {
1785         get_fauto_xyz(AUTOMATION_PROJECTOR_X, x, y, z);
1786 }
1787 void Track::set_projector(float x, float y, float z)
1788 {
1789         set_fauto_xyz(AUTOMATION_PROJECTOR_X, x, y, z);
1790 }
1791
1792 void Track::get_camera(float &x, float &y, float &z)
1793 {
1794         get_fauto_xyz(AUTOMATION_CAMERA_X, x, y, z);
1795 }
1796 void Track::set_camera(float x, float y, float z)
1797 {
1798         set_fauto_xyz(AUTOMATION_CAMERA_X, x, y, z);
1799 }
1800
1801 Track *Track::gang_master()
1802 {
1803         Track *track = this;
1804         switch( edl->session->gang_tracks ) {
1805         case GANG_NONE:
1806                 return track;
1807         case GANG_CHANNELS: {
1808                 Track *current = track;
1809                 int data_type = track->data_type;
1810                 while( current && !track->master ) {
1811                         if( !(current = current->previous) ) break;
1812                         if( current->data_type == data_type ) track = current;
1813                         if( track->master ) break;
1814                 }
1815                 break; }
1816         case GANG_MEDIA: {
1817                 while( track && !track->master ) track = track->previous;
1818                 break; }
1819         }
1820         if( !track ) track = tracks->first;
1821         return track;
1822 }
1823
1824 int Track::is_hidden()
1825 {
1826         if( master ) return 0;
1827         if( edl->session->gang_tracks == GANG_MEDIA ) return 1;
1828         if( edl->session->gang_tracks == GANG_CHANNELS ) {
1829                 for( Track *track=previous; track; track=track->previous ) {
1830                         if( track->data_type == data_type ) return 1;
1831                         if( track->master ) return 0;
1832                 }
1833         }
1834         return 0;
1835 }
1836 int Track::is_armed()
1837 {
1838         return gang_master()->armed;
1839 }
1840
1841 int Track::is_ganged()
1842 {
1843         return gang_master()->ganged;
1844 }
1845
1846 int Track::armed_gang(Track *track)
1847 {
1848         if( edl->session->gang_tracks == GANG_NONE ) return ganged;
1849         Track *current = gang_master();
1850         for(;;) {
1851                 if( track == current ) return 1;
1852                 current = current->next;
1853                 if( !current || current->master ) return 0;
1854         }
1855         return 1;
1856 }
1857
1858
1859 int Track::index_in(Mixer *mixer)
1860 {
1861         if( !mixer || mixer_id < 0 ) return -1;
1862         int k = mixer->mixer_ids.size();
1863         while( --k >= 0 && mixer_id != mixer->mixer_ids[k] );
1864         return k;
1865 }
1866