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