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