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