3 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4 * Copyright (C) 2003-2016 Cinelerra CV contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "automation.h"
29 #include "edlsession.h"
34 #include "localsession.h"
40 #include "mainsession.h"
44 #include "trackcanvas.h"
46 #include "transportque.inc"
50 Tracks::Tracks(EDL *edl)
71 void Tracks::equivalent_output(Tracks *tracks, double *result)
73 if(total_playable_vtracks() != tracks->total_playable_vtracks())
79 Track *current = first;
80 Track *that_current = tracks->first;
81 while(current || that_current)
83 // Get next video track
84 while(current && current->data_type != TRACK_VIDEO)
87 while(that_current && that_current->data_type != TRACK_VIDEO)
88 that_current = that_current->next;
90 // One doesn't exist but the other does
91 if((!current && that_current) ||
92 (current && !that_current))
99 if(current && that_current)
101 current->equivalent_output(that_current, result);
103 that_current = that_current->next;
110 void Tracks::clear_selected_edits()
112 for( Track *track=first; track; track=track->next ) {
113 for( Edit *edit=track->edits->first; edit; edit=edit->next )
114 edit->is_selected = 0;
118 void Tracks::get_selected_edits(ArrayList<Edit*> *drag_edits)
120 drag_edits->remove_all();
121 for( Track *track=first; track; track=track->next ) {
122 if( !track->is_armed() ) continue;
123 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
124 if( !edit->is_selected ) continue;
125 drag_edits->append(edit);
130 void Tracks::select_edits(double start, double end, int v)
132 for( Track *track=first; track; track=track->next ) {
133 if( !track->is_armed() ) continue;
134 int64_t start_pos = track->to_units(start, 0);
135 int64_t end_pos = track->to_units(end, 0);
136 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
137 if( start_pos >= edit->startproject+edit->length ) continue;
138 if( edit->startproject >= end_pos ) continue;
139 edit->is_selected = v > 1 ? 1 : v < 0 ? 0 : !edit->is_selected ;
144 void Tracks::get_automation_extents(float *min,
152 int coords_undefined = 1;
153 for(Track *current = first; current; current = NEXT)
155 if(current->is_armed())
157 current->automation->get_extents(min,
160 current->to_units(start, 0),
161 current->to_units(end, 1),
168 void Tracks::copy_from(Tracks *tracks)
170 Track *new_track = 0;
173 int solo_track_id = tracks->edl->local_session->solo_track_id;
175 for(Track *current = tracks->first; current; current = NEXT)
177 switch(current->data_type)
180 new_track = add_audio_track(0, 0);
183 new_track = add_video_track(0, 0);
186 new_track = add_subttl_track(0, 0);
191 new_track->copy_from(current);
193 if( current->get_id() == solo_track_id )
194 edl->local_session->solo_track_id = new_track->get_id();
198 Tracks& Tracks::operator=(Tracks &tracks)
200 printf("Tracks::operator= 1\n");
205 int Tracks::load(FileXML *xml,
209 // add the appropriate type of track
210 char string[BCTEXTLEN];
214 xml->tag.get_property("TYPE", string);
216 if((load_flags & LOAD_ALL) == LOAD_ALL ||
217 (load_flags & LOAD_EDITS)) {
218 if(!strcmp(string, "VIDEO")) {
219 track = add_video_track(0, 0);
221 else if(!strcmp(string, "SUBTTL")) {
222 track = add_subttl_track(0, 0);
225 track = add_audio_track(0, 0); // default to audio
229 track = get_item_number(track_offset++);
233 if( track ) track->load(xml, track_offset, load_flags);
238 Track* Tracks::add_audio_track(int above, Track *dst_track)
240 ATrack* new_track = new ATrack(edl, this);
243 dst_track = (above ? first : last);
248 insert_before(dst_track, (Track*)new_track);
252 insert_after(dst_track, (Track*)new_track);
253 // Shift effects referenced below the destination track
256 // Shift effects referenced below the new track
257 for(Track *track = last;
258 track && track != new_track;
259 track = track->previous)
261 change_modules(number_of(track) - 1, number_of(track), 0);
265 new_track->create_objects();
266 new_track->set_default_title();
269 for(Track *current = first;
270 current != (Track*)new_track;
273 if(current->data_type == TRACK_AUDIO) current_pan++;
274 if(current_pan >= edl->session->audio_channels) current_pan = 0;
280 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
281 pan_auto->values[current_pan] = 1.0;
283 BC_Pan::calculate_stick_position(edl->session->audio_channels,
284 edl->session->achannel_positions,
293 Track* Tracks::add_video_track(int above, Track *dst_track)
295 VTrack* new_track = new VTrack(edl, this);
297 dst_track = (above ? first : last);
299 insert_before(dst_track, (Track*)new_track);
301 insert_after(dst_track, (Track*)new_track);
303 for(Track *track = last; track && track != new_track; track = track->previous)
304 change_modules(number_of(track) - 1, number_of(track), 0);
306 new_track->create_objects();
307 new_track->set_default_title();
312 Track* Tracks::add_subttl_track(int above, Track *dst_track)
314 STrack* new_track = new STrack(edl, this);
316 dst_track = (above ? first : last);
319 insert_before(dst_track, (Track*)new_track);
321 insert_after(dst_track, (Track*)new_track);
323 for(Track *track = last; track && track != new_track; track = track->previous)
324 change_modules(number_of(track) - 1, number_of(track), 0);
326 new_track->create_objects();
327 new_track->set_default_title();
328 // new_track->paste_silence(0,total_length(),0);
333 int Tracks::delete_track(Track *track, int gang)
335 if( !track ) return 0;
337 gang = edl->local_session->gang_tracks != GANG_NONE ? 1 : 0;
338 Track *nxt = track->next;
340 track = track->gang_master();
341 while( nxt && !nxt->master )
344 Track *current = track;
345 int old_location = number_of(current);
346 for( Track *next_track=0; current!=nxt; current=next_track ) {
347 next_track = current->next;
348 detach_shared_effects(old_location);
349 for( Track *curr=current; curr; curr=curr->next ) {
350 // Shift effects referencing effects below the deleted track
351 change_modules(number_of(curr), number_of(curr)-1, 0);
358 int Tracks::detach_shared_effects(int module)
360 for( Track *current=first; current; current=NEXT ) {
361 current->detach_shared_effects(module);
365 int Tracks::detach_ganged_effects(Plugin *plugin)
367 if( edl->local_session->gang_tracks == GANG_NONE ) return 1;
368 for( Track *current=first; current; current=NEXT ) {
369 if( current == plugin->track ) continue;
370 if( !current->armed_gang(plugin->track) ) continue;
371 current->detach_ganged_effects(plugin);
376 int Tracks::total_of(int type)
380 for(Track *current = first; current; current = NEXT)
382 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
383 Auto *mute_keyframe = 0;
384 current->automation->autos[AUTOMATION_MUTE]->
385 get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
386 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
389 (current->plays() && type == PLAY) ||
390 (current->is_armed() && type == RECORD) ||
391 (current->is_ganged() && type == GANG) ||
392 (current->draw && type == DRAW) ||
393 (mute_auto->value && type == MUTE) ||
394 (current->expand_view && type == EXPAND);
399 int Tracks::recordable_audio_tracks()
402 for(Track *current = first; current; current = NEXT)
403 if(current->data_type == TRACK_AUDIO && current->is_armed()) result++;
407 int Tracks::recordable_video_tracks()
410 for(Track *current = first; current; current = NEXT)
412 if(current->data_type == TRACK_VIDEO && current->is_armed()) result++;
418 int Tracks::playable_audio_tracks()
422 for(Track *current = first; current; current = NEXT)
424 if(current->data_type == TRACK_AUDIO && current->plays())
433 int Tracks::playable_video_tracks()
437 for(Track *current = first; current; current = NEXT)
439 if(current->data_type == TRACK_VIDEO && current->plays())
447 int Tracks::total_audio_tracks()
450 for(Track *current = first; current; current = NEXT)
451 if(current->data_type == TRACK_AUDIO) result++;
455 int Tracks::total_video_tracks()
458 for(Track *current = first; current; current = NEXT)
459 if(current->data_type == TRACK_VIDEO) result++;
463 double Tracks::total_playable_length()
466 for(Track *current = first; current; current = NEXT)
468 if( current->plays() )
470 double length = current->get_length();
471 if(length > total) total = length;
477 double Tracks::total_recordable_length()
480 for(Track *current = first; current; current = NEXT)
482 if(current->is_armed())
484 double length = current->get_length();
485 if(length > total) total = length;
491 double Tracks::total_length()
494 for(Track *current = first; current; current = NEXT)
496 double length = current->get_length();
497 if(length > total) total = length;
502 double Tracks::total_audio_length()
505 for(Track *current = first; current; current = NEXT)
507 if(current->data_type == TRACK_AUDIO &&
508 current->get_length() > total) total = current->get_length();
513 double Tracks::total_video_length()
516 for(Track *current = first; current; current = NEXT)
518 if(current->data_type == TRACK_VIDEO &&
519 current->get_length() > total) total = current->get_length();
524 double Tracks::total_length_framealigned(double fps)
526 if (total_audio_tracks() && total_video_tracks())
527 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
529 if (total_audio_tracks())
530 return floor(total_audio_length() * fps) / fps;
532 if (total_video_tracks())
533 return floor(total_video_length() * fps) / fps;
538 void Tracks::translate_fauto_xy(int fauto, float dx, float dy, int all)
540 Track *track = first;
541 for( ; track; track=track->next ) {
542 if( !all && !track->is_armed() ) continue;
543 if( track->data_type != TRACK_VIDEO ) continue;
544 ((VTrack*)track)->translate(fauto, dx, dy, all);
548 void Tracks::translate_projector(float dx, float dy, int all)
550 translate_fauto_xy(AUTOMATION_PROJECTOR_X, dx, dy, all);
553 void Tracks::translate_camera(float dx, float dy, int all)
555 translate_fauto_xy(AUTOMATION_CAMERA_X, dx, dy, all);
558 void Tracks::crop_resize(float x, float y, float z)
560 float ctr_x = edl->session->output_w / 2.;
561 float ctr_y = edl->session->output_h / 2.;
562 Track *track = first;
563 for( ; track; track=track->next ) {
564 if( !track->is_armed() ) continue;
565 if( track->data_type != TRACK_VIDEO ) continue;
567 track->get_projector(px, py, pz);
568 float old_x = px + ctr_x;
569 float old_y = py + ctr_y;
570 float nx = (old_x - x) * z;
571 float ny = (old_y - y) * z;
572 track->set_projector(nx, ny, pz * z);
576 void Tracks::crop_shrink(float x, float y, float z)
578 float ctr_x = edl->session->output_w / 2.;
579 float ctr_y = edl->session->output_h / 2.;
580 Track *track = first;
581 for( ; track; track=track->next ) {
582 if( !track->is_armed() ) continue;
583 if( track->data_type != TRACK_VIDEO ) continue;
584 float cx, cy, cz, px, py, pz;
585 track->get_camera(cx, cy, cz);
586 track->get_projector(px, py, pz);
587 float dx = x - (px + ctr_x);
588 float dy = y - (py + ctr_y);
590 cx += dx / cz; cy += dy / cz;
591 track->set_camera(cx, cy, cz * z);
593 track->set_projector(px, py, 1 / z);
597 void Tracks::update_y_pixels(Theme *theme)
599 // int y = -edl->local_session->track_start;
601 for(Track *current = first; current; current = NEXT)
603 //printf("Tracks::update_y_pixels %d\n", y);
604 current->y_pixel = y;
605 if( current->is_hidden() ) continue;
606 y += current->vertical_span(theme);
610 int Tracks::dump(FILE *fp)
612 for(Track* current = first; current; current = NEXT)
614 fprintf(fp," Track: %p\n", current);
621 void Tracks::select_all(int type,
624 for(Track* current = first; current; current = NEXT)
626 double position = edl->local_session->get_selectionstart(1);
628 if(type == PLAY) current->play = value;
629 if(type == RECORD) current->armed = value;
630 if(type == GANG) current->ganged = value;
631 if(type == DRAW) current->draw = value;
635 ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
638 if(type == EXPAND) current->expand_view = value;
642 // ===================================== file operations
644 int Tracks::popup_transition(int cursor_x, int cursor_y)
647 for(Track* current = first; current && !result; current = NEXT)
649 result = current->popup_transition(cursor_x, cursor_y);
655 int Tracks::change_channels(int oldchannels, int newchannels)
657 for(Track *current = first; current; current = NEXT)
658 { current->change_channels(oldchannels, newchannels); }
664 int Tracks::totalpixels()
667 for(Track* current = first; current; current = NEXT)
669 result += current->data_h;
674 int Tracks::number_of(Track *track)
677 for(Track *current = first; current && current != track; current = NEXT)
684 Track* Tracks::number(int number)
688 for(current = first; current && i < number; current = NEXT)
695 Track* Tracks::get_track_by_id(int id)
697 Track *track = edl->tracks->first;
698 while( track && track->get_id() != id ) track = track->next;
702 int Tracks::total_playable_vtracks()
705 for(Track *current = first; current; current = NEXT)
707 if(current->data_type == TRACK_VIDEO && current->play) result++;
712 Plugin *Tracks::plugin_exists(int plugin_id)
714 if( plugin_id < 0 ) return 0;
716 for( Track *track=first; !plugin && track; track=track->next ) {
717 plugin = track->plugin_exists(plugin_id);
722 int Tracks::track_exists(Track *track)
724 for(Track *current = first; current; current = NEXT)
726 if(current == track) return 1;
731 int Tracks::new_group(int id)
734 for( Track *track=first; track; track=track->next ) {
735 if( !track->is_armed() ) continue;
736 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
737 if( edit->group_id > 0 ) continue;
738 if( !edit->is_selected ) continue;
746 int Tracks::set_group_selected(int id, int v)
749 int gang = edl->local_session->gang_tracks != GANG_NONE ? 1 : 0;
750 for( Track *track=first; track; track=track->next ) {
751 if( track->is_hidden() ) continue;
752 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
753 if( edit->group_id != id ) continue;
754 if( v < 0 ) v = !edit->is_selected ? 1 : 0;
755 edit->select_affected_edits(v, gang);
762 int Tracks::del_group(int id)
765 for( Track *track=first; track; track=track->next ) {
766 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
767 if( edit->group_id != id ) continue;
768 edit->is_selected = 1;
776 Track *Tracks::get(int idx, int data_type)
778 for(Track *current = first; current; current = NEXT)
780 if( current->data_type != data_type ) continue;
781 if( --idx < 0 ) return current;
786 void Tracks::roll_tracks(Track *src, Track *dst, int n)
788 if( src == dst ) return;
789 while( --n >= 0 && src ) {
790 Track *nxt = src->next;
791 change_modules(number_of(src), total(), 0);
792 for( Track *track=nxt; track; track=track->next )
793 change_modules(number_of(track), number_of(track)-1, 0);
795 int ndst = dst ? number_of(dst) : total();
796 insert_before(dst, src);
797 for( Track *track=last; track && track!=src; track=track->previous )
798 change_modules(number_of(track)-1, number_of(track), 0);
799 change_modules(total(), ndst, 0);
804 double Tracks::align_timecodes()
807 for( Track *track=first; track; track=track->next ) {
808 if( !track->is_armed() ) continue;
809 double early_offset = track->edits->early_timecode();
810 if( offset < 0 || offset > early_offset )
811 offset = early_offset;
814 for( Track *track=first; track; track=track->next ) {
815 if( !track->is_armed() ) continue;
816 track->edits->align_timecodes(offset);
822 void Tracks::update_idxbl_length(int id, double dt)
824 for( Track *track=first; track; track=track->next ) {
825 if( !track->is_armed() ) continue;
826 int64_t du = track->to_units(dt,0);
827 track->edits->update_idxbl_length(id, du);
832 void Tracks::create_keyframes(double position, int mask, int mode)
834 for( Track *track=first; track; track=track->next ) {
835 if( !track->is_armed() ) continue;
836 track->create_keyframes(position, mask, mode);