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