e2abde91f9cf6b94362bb28ba28c0dd389f73e27
[goodguy/history.git] / cinelerra-5.1 / cinelerra / tracks.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 "atrack.h"
22 #include "automation.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "edit.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "file.h"
30 #include "filexml.h"
31 #include "intauto.h"
32 #include "intautos.h"
33 #include "localsession.h"
34 #include "module.h"
35 #include "panauto.h"
36 #include "panautos.h"
37 #include "patchbay.h"
38 #include "mainsession.h"
39 #include "strack.h"
40 #include "theme.h"
41 #include "track.h"
42 #include "trackcanvas.h"
43 #include "tracks.h"
44 #include "transportque.inc"
45 #include "vtrack.h"
46 #include <string.h>
47
48 Tracks::Tracks(EDL *edl)
49  : List<Track>()
50 {
51         this->edl = edl;
52 }
53
54 Tracks::Tracks()
55  : List<Track>()
56 {
57 }
58
59
60 Tracks::~Tracks()
61 {
62         delete_all_tracks();
63 }
64
65
66
67
68
69 void Tracks::equivalent_output(Tracks *tracks, double *result)
70 {
71         if(total_playable_vtracks() != tracks->total_playable_vtracks())
72         {
73                 *result = 0;
74         }
75         else
76         {
77                 Track *current = first;
78                 Track *that_current = tracks->first;
79                 while(current || that_current)
80                 {
81 // Get next video track
82                         while(current && current->data_type != TRACK_VIDEO)
83                                 current = NEXT;
84
85                         while(that_current && that_current->data_type != TRACK_VIDEO)
86                                 that_current = that_current->next;
87
88 // One doesn't exist but the other does
89                         if((!current && that_current) ||
90                                 (current && !that_current))
91                         {
92                                 *result = 0;
93                                 break;
94                         }
95                         else
96 // Both exist
97                         if(current && that_current)
98                         {
99                                 current->equivalent_output(that_current, result);
100                                 current = NEXT;
101                                 that_current = that_current->next;
102                         }
103                 }
104         }
105 }
106
107
108
109
110 void Tracks::get_affected_edits(ArrayList<Edit*> *drag_edits, double position, Track *start_track)
111 {
112         drag_edits->remove_all();
113
114         for(Track *track = start_track;
115                 track;
116                 track = track->next)
117         {
118 //printf("Tracks::get_affected_edits 1 %p %d %d\n", track, track->data_type, track->record);
119                 if(track->record)
120                 {
121                         for(Edit *edit = track->edits->first; edit; edit = edit->next)
122                         {
123                                 double startproject = track->from_units(edit->startproject);
124 //printf("Tracks::get_affected_edits 1 %d\n", edl->equivalent(startproject, position));
125                                 if(edl->equivalent(startproject, position))
126                                 {
127                                         drag_edits->append(edit);
128                                         break;
129                                 }
130                         }
131                 }
132         }
133
134 }
135
136 void Tracks::get_automation_extents(float *min,
137         float *max,
138         double start,
139         double end,
140         int autogrouptype)
141 {
142         *min = 0;
143         *max = 0;
144         int coords_undefined = 1;
145         for(Track *current = first; current; current = NEXT)
146         {
147                 if(current->record)
148                 {
149                         current->automation->get_extents(min,
150                                 max,
151                                 &coords_undefined,
152                                 current->to_units(start, 0),
153                                 current->to_units(end, 1),
154                                 autogrouptype);
155                 }
156         }
157 }
158
159
160 void Tracks::copy_from(Tracks *tracks)
161 {
162         Track *new_track = 0;
163
164         delete_all_tracks();
165         for(Track *current = tracks->first; current; current = NEXT)
166         {
167                 switch(current->data_type)
168                 {
169                 case TRACK_AUDIO:
170                         new_track = add_audio_track(0, 0);
171                         break;
172                 case TRACK_VIDEO:
173                         new_track = add_video_track(0, 0);
174                         break;
175                 case TRACK_SUBTITLE:
176                         new_track = add_subttl_track(0, 0);
177                         break;
178                 default:
179                         continue;
180                 }
181                 new_track->copy_from(current);
182         }
183 }
184
185 Tracks& Tracks::operator=(Tracks &tracks)
186 {
187 printf("Tracks::operator= 1\n");
188         copy_from(&tracks);
189         return *this;
190 }
191
192 int Tracks::load(FileXML *xml,
193         int &track_offset,
194         uint32_t load_flags)
195 {
196 // add the appropriate type of track
197         char string[BCTEXTLEN];
198         Track *track = 0;
199         string[0] = 0;
200
201         xml->tag.get_property("TYPE", string);
202
203         if((load_flags & LOAD_ALL) == LOAD_ALL ||
204                 (load_flags & LOAD_EDITS))
205         {
206                 if(!strcmp(string, "VIDEO"))
207                 {
208                         add_video_track(0, 0);
209                 }
210                 else
211                 if(!strcmp(string, "SUBTTL"))
212                 {
213                         add_subttl_track(0, 0);
214                 }
215                 else
216                 {
217                         add_audio_track(0, 0);    // default to audio
218                 }
219                 track = last;
220         }
221         else
222         {
223                 track = get_item_number(track_offset);
224                 track_offset++;
225         }
226
227 // load it
228         if(track) track->load(xml, track_offset, load_flags);
229
230         return 0;
231 }
232
233 Track* Tracks::add_audio_track(int above, Track *dst_track)
234 {
235         ATrack* new_track = new ATrack(edl, this);
236         if(!dst_track)
237         {
238                 dst_track = (above ? first : last);
239         }
240
241         if(above)
242         {
243                 insert_before(dst_track, (Track*)new_track);
244         }
245         else
246         {
247                 insert_after(dst_track, (Track*)new_track);
248 // Shift effects referenced below the destination track
249         }
250
251 // Shift effects referenced below the new track
252         for(Track *track = last;
253                 track && track != new_track;
254                 track = track->previous)
255         {
256                 change_modules(number_of(track) - 1, number_of(track), 0);
257         }
258
259
260         new_track->create_objects();
261         new_track->set_default_title();
262
263         int current_pan = 0;
264         for(Track *current = first;
265                 current != (Track*)new_track;
266                 current = NEXT)
267         {
268                 if(current->data_type == TRACK_AUDIO) current_pan++;
269                 if(current_pan >= edl->session->audio_channels) current_pan = 0;
270         }
271
272
273
274         PanAuto* pan_auto =
275                 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
276         pan_auto->values[current_pan] = 1.0;
277
278         BC_Pan::calculate_stick_position(edl->session->audio_channels,
279                 edl->session->achannel_positions,
280                 pan_auto->values,
281                 MAX_PAN,
282                 PAN_RADIUS,
283                 pan_auto->handle_x,
284                 pan_auto->handle_y);
285         return new_track;
286 }
287
288 Track* Tracks::add_video_track(int above, Track *dst_track)
289 {
290 const int debug = 0;
291 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
292         VTrack* new_track = new VTrack(edl, this);
293 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
294         if(!dst_track)
295                 dst_track = (above ? first : last);
296
297 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
298         if(above)
299         {
300                 insert_before(dst_track, (Track*)new_track);
301         }
302         else
303         {
304                 insert_after(dst_track, (Track*)new_track);
305         }
306 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
307
308
309
310 // Shift effects referenced below the new track
311         for(Track *track = last;
312                 track && track != new_track;
313                 track = track->previous)
314         {
315                 change_modules(number_of(track) - 1, number_of(track), 0);
316         }
317
318 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
319
320
321         new_track->create_objects();
322 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
323         new_track->set_default_title();
324 if(debug) printf("Tracks::add_video_track %d\n", __LINE__);
325         return new_track;
326 }
327
328
329 Track* Tracks::add_subttl_track(int above, Track *dst_track)
330 {
331 const int debug = 0;
332 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
333         STrack* new_track = new STrack(edl, this);
334 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
335         if(!dst_track)
336                 dst_track = (above ? first : last);
337
338 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
339         if(above)
340         {
341                 insert_before(dst_track, (Track*)new_track);
342         }
343         else
344         {
345                 insert_after(dst_track, (Track*)new_track);
346         }
347 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
348
349
350
351 // Shift effects referenced below the new track
352         for(Track *track = last;
353                 track && track != new_track;
354                 track = track->previous)
355         {
356                 change_modules(number_of(track) - 1, number_of(track), 0);
357         }
358
359 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
360
361
362         new_track->create_objects();
363 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
364         new_track->set_default_title();
365 if(debug) printf("Tracks::add_subttl_track %d\n", __LINE__);
366
367 //      new_track->paste_silence(0,total_length(),0);
368         return new_track;
369 }
370
371
372 int Tracks::delete_track(Track *track)
373 {
374         if (!track)
375                 return 0;
376
377         int old_location = number_of(track);
378         detach_shared_effects(old_location);
379
380 // Shift effects referencing effects below the deleted track
381         for(Track *current = track;
382                 current;
383                 current = NEXT)
384         {
385                 change_modules(number_of(current), number_of(current) - 1, 0);
386         }
387         if(track) delete track;
388
389         return 0;
390 }
391
392 int Tracks::detach_shared_effects(int module)
393 {
394         for(Track *current = first; current; current = NEXT)
395         {
396                 current->detach_shared_effects(module);
397         }
398
399         return 0;
400  }
401
402 int Tracks::total_of(int type)
403 {
404         int result = 0;
405
406         for(Track *current = first; current; current = NEXT)
407         {
408                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
409                 Auto *mute_keyframe = current->automation->autos[AUTOMATION_MUTE]->
410                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
411                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
412
413                 result +=
414                         (current->play && type == PLAY) ||
415                         (current->record && type == RECORD) ||
416                         (current->gang && type == GANG) ||
417                         (current->draw && type == DRAW) ||
418                         (mute_auto->value && type == MUTE) ||
419                         (current->expand_view && type == EXPAND);
420         }
421         return result;
422 }
423
424 int Tracks::recordable_audio_tracks()
425 {
426         int result = 0;
427         for(Track *current = first; current; current = NEXT)
428                 if(current->data_type == TRACK_AUDIO && current->record) result++;
429         return result;
430 }
431
432 int Tracks::recordable_video_tracks()
433 {
434         int result = 0;
435         for(Track *current = first; current; current = NEXT)
436         {
437                 if(current->data_type == TRACK_VIDEO && current->record) result++;
438         }
439         return result;
440 }
441
442
443 int Tracks::playable_audio_tracks()
444 {
445         int result = 0;
446
447         for(Track *current = first; current; current = NEXT)
448         {
449                 if(current->data_type == TRACK_AUDIO && current->play)
450                 {
451                         result++;
452                 }
453         }
454
455         return result;
456 }
457
458 int Tracks::playable_video_tracks()
459 {
460         int result = 0;
461
462         for(Track *current = first; current; current = NEXT)
463         {
464                 if(current->data_type == TRACK_VIDEO && current->play)
465                 {
466                         result++;
467                 }
468         }
469         return result;
470 }
471
472 int Tracks::total_audio_tracks()
473 {
474         int result = 0;
475         for(Track *current = first; current; current = NEXT)
476                 if(current->data_type == TRACK_AUDIO) result++;
477         return result;
478 }
479
480 int Tracks::total_video_tracks()
481 {
482         int result = 0;
483         for(Track *current = first; current; current = NEXT)
484                 if(current->data_type == TRACK_VIDEO) result++;
485         return result;
486 }
487
488 double Tracks::total_playable_length()
489 {
490         double total = 0;
491         for(Track *current = first; current; current = NEXT)
492         {
493                 double length = current->get_length();
494                 if(length > total) total = length;
495         }
496         return total;
497 }
498
499 double Tracks::total_recordable_length()
500 {
501         double total = 0;
502         for(Track *current = first; current; current = NEXT)
503         {
504                 if(current->record)
505                 {
506                         double length = current->get_length();
507                         if(length > total) total = length;
508                 }
509         }
510         return total;
511 }
512
513 double Tracks::total_length()
514 {
515         double total = 0;
516         for(Track *current = first; current; current = NEXT)
517         {
518                 if(current->get_length() > total) total = current->get_length();
519         }
520         return total;
521 }
522
523 double Tracks::total_audio_length()
524 {
525         double total = 0;
526         for(Track *current = first; current; current = NEXT)
527         {
528                 if(current->data_type == TRACK_AUDIO &&
529                         current->get_length() > total) total = current->get_length();
530         }
531         return total;
532 }
533
534 double Tracks::total_video_length()
535 {
536         double total = 0;
537         for(Track *current = first; current; current = NEXT)
538         {
539                 if(current->data_type == TRACK_VIDEO &&
540                         current->get_length() > total) total = current->get_length();
541         }
542         return total;
543 }
544
545 double Tracks::total_length_framealigned(double fps)
546 {
547         if (total_audio_tracks() && total_video_tracks())
548                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
549
550         if (total_audio_tracks())
551                 return floor(total_audio_length() * fps) / fps;
552
553         if (total_video_tracks())
554                 return floor(total_video_length() * fps) / fps;
555
556         return 0;
557 }
558
559 void Tracks::translate_projector(float offset_x, float offset_y)
560 {
561         for(Track *current = first; current; current = NEXT)
562         {
563                 if(current->data_type == TRACK_VIDEO)
564                 {
565                         ((VTrack*)current)->translate(offset_x, offset_y, 0);
566                 }
567         }
568 }
569
570 void Tracks::update_y_pixels(Theme *theme)
571 {
572 //      int y = -edl->local_session->track_start;
573         int y = 0;
574         for(Track *current = first; current; current = NEXT)
575         {
576 //printf("Tracks::update_y_pixels %d\n", y);
577                 current->y_pixel = y;
578                 y += current->vertical_span(theme);
579         }
580 }
581
582 int Tracks::dump(FILE *fp)
583 {
584         for(Track* current = first; current; current = NEXT)
585         {
586                 fprintf(fp,"  Track: %p\n", current);
587                 current->dump(fp);
588                 fprintf(fp,"\n");
589         }
590         return 0;
591 }
592
593 void Tracks::select_all(int type,
594                 int value)
595 {
596         for(Track* current = first; current; current = NEXT)
597         {
598                 double position = edl->local_session->get_selectionstart(1);
599
600                 if(type == PLAY) current->play = value;
601                 if(type == RECORD) current->record = value;
602                 if(type == GANG) current->gang = value;
603                 if(type == DRAW) current->draw = value;
604
605                 if(type == MUTE)
606                 {
607                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
608                 }
609
610                 if(type == EXPAND) current->expand_view = value;
611         }
612 }
613
614 // ===================================== file operations
615
616 int Tracks::popup_transition(int cursor_x, int cursor_y)
617 {
618         int result = 0;
619         for(Track* current = first; current && !result; current = NEXT)
620         {
621                 result = current->popup_transition(cursor_x, cursor_y);
622         }
623         return result;
624 }
625
626
627 int Tracks::change_channels(int oldchannels, int newchannels)
628 {
629         for(Track *current = first; current; current = NEXT)
630         { current->change_channels(oldchannels, newchannels); }
631         return 0;
632 }
633
634
635
636 int Tracks::totalpixels()
637 {
638         int result = 0;
639         for(Track* current = first; current; current = NEXT)
640         {
641                 result += edl->local_session->zoom_track;
642         }
643         return result;
644 }
645
646 int Tracks::number_of(Track *track)
647 {
648         int i = 0;
649         for(Track *current = first; current && current != track; current = NEXT)
650         {
651                 i++;
652         }
653         return i;
654 }
655
656 Track* Tracks::number(int number)
657 {
658         Track *current;
659         int i = 0;
660         for(current = first; current && i < number; current = NEXT)
661         {
662                 i++;
663         }
664         return current;
665 }
666
667
668 int Tracks::total_playable_vtracks()
669 {
670         int result = 0;
671         for(Track *current = first; current; current = NEXT)
672         {
673                 if(current->data_type == TRACK_VIDEO && current->play) result++;
674         }
675         return result;
676 }
677
678 int Tracks::plugin_exists(Plugin *plugin)
679 {
680         for(Track *track = first; track; track = track->next)
681         {
682                 if(track->plugin_exists(plugin)) return 1;
683         }
684         return 0;
685 }
686
687 int Tracks::track_exists(Track *track)
688 {
689         for(Track *current = first; current; current = NEXT)
690         {
691                 if(current == track) return 1;
692         }
693         return 0;
694 }
695
696
697 Track *Tracks::get(int idx, int data_type)
698 {
699         for(Track *current = first; current; current = NEXT)
700         {
701                 if( current->data_type != data_type ) continue;
702                 if( --idx < 0 ) return current;
703         }
704         return 0;
705 }
706
707