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