r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / tracksedit.C
blobaa705e0cd710bd19d9bf4eebe3957bc4c7b2a5d0
1 #include "assets.h"
2 #include "atrack.h"
3 #include "automation.h"
4 #include "aedits.h"
5 #include "edit.h"
6 #include "edits.h"
7 #include "edl.h"
8 #include "edlsession.h"
9 #include "filexml.h"
10 #include "intauto.h"
11 #include "intautos.h"
12 #include "localsession.h"
13 #include "mainundo.h"
14 #include "module.h"
15 #include "mainsession.h"
16 #include "pluginserver.h"
17 #include "pluginset.h"
18 #include "timebar.h"
19 #include "trackcanvas.h"
20 #include "tracks.h"
21 #include "trackscroll.h"
22 #include "transition.h"
23 #include "transportque.h"
24 #include "vtrack.h"
25 #include <string.h>
27 int Tracks::clear(double start, double end, int clear_plugins)
29         Track *current_track;
31         for(current_track = first; 
32                 current_track; 
33                 current_track = current_track->next)
34         {
35                 if(current_track->record) 
36                 {
37                         current_track->clear(start, 
38                                 end, 
39                                 1, 
40                                 1, 
41                                 clear_plugins, 
42                                 1); 
43                 }
44         }
45         return 0;
48 void Tracks::clear_automation(double selectionstart, double selectionend)
50         Track* current_track;
52         for(current_track = first; current_track; current_track = current_track->next)
53         {
54                 if(current_track->record)
55                 {
56                         current_track->clear_automation(selectionstart, 
57                                 selectionend, 
58                                 0,
59                                 0); 
60                 }
61         }
64 int Tracks::clear_default_keyframe()
66         for(Track *current = first; current; current = NEXT)
67         {
68                 if(current->record)
69                         current->clear_automation(0, 0, 0, 1);
70         }
71         return 0;
74 int Tracks::clear_handle(double start, 
75         double end,
76         double &longest_distance,
77         int clear_labels,
78         int clear_plugins)
80         Track* current_track;
81         double distance;
83         for(current_track = first; current_track; current_track = current_track->next)
84         {
85                 if(current_track->record)
86                 {
87                         current_track->clear_handle(start, 
88                                 end, 
89                                 clear_labels,
90                                 clear_plugins, 
91                                 distance);
92                         if(distance > longest_distance) longest_distance = distance;
93                 }
94         }
96         return 0;
99 int Tracks::copy_automation(double selectionstart, 
100         double selectionend, 
101         FileXML *file,
102         int default_only,
103         int autos_only)
105 // called by MWindow::copy_automation for copying automation alone
106         Track* current_track;
108         file->tag.set_title("AUTO_CLIPBOARD");
109         file->tag.set_property("LENGTH", selectionend - selectionstart);
110         file->tag.set_property("FRAMERATE", edl->session->frame_rate);
111         file->tag.set_property("SAMPLERATE", edl->session->sample_rate);
112         file->append_tag();
113         file->append_newline();
114         file->append_newline();
116         for(current_track = first; 
117                 current_track; 
118                 current_track = current_track->next)
119         {
120                 if(current_track->record)
121                 {
122                         current_track->copy_automation(selectionstart, 
123                                 selectionend, 
124                                 file,
125                                 default_only,
126                                 autos_only);
127                 }
128         }
130         file->tag.set_title("/AUTO_CLIPBOARD");
131         file->append_tag();
132         file->append_newline();
133         file->terminate_string();
134         return 0;
137 int Tracks::copy_default_keyframe(FileXML *file)
139         copy_automation(0, 0, file, 1, 0);
140         return 0;
143 int Tracks::delete_tracks()
145         int result = 1, total_deleted = 0;
146         Track *track, *next_track, *shifted_track;
147         int deleted_number;
148         
149         while(result)
150         {
151 // keep deleting until all the recordable tracks are gone
152                 result = 0;
154 //printf("Tracks::delete_tracks 1\n");
155 // Stop when first recordable track is reached
156                 for(track = first; 
157                         track && !result;  
158                         track = next_track)
159                 {
160 //printf("Tracks::delete_tracks 2\n");
161                         next_track = track->next;
162                         
163                         if(track->record)
164                         {
165                                 deleted_number = number_of(track);
166 // Delete the track.
167                                 delete track;
169 // Shift all the plugin pointers.
170                                 for(shifted_track = next_track;
171                                         shifted_track;
172                                         shifted_track = shifted_track->next)
173                                 {
174                                         shifted_track->delete_module_pointers(deleted_number);
175                                 }
176                                 result = 1;
177                                 total_deleted++;
178                         }
179                 }
180 //printf("Tracks::delete_tracks 3\n");
181         }
182 //printf("Tracks::delete_tracks 4\n");
183         return total_deleted;
186 void Tracks::move_edits(ArrayList<Edit*> *edits, 
187         Track *track,
188         double position,
189         int edit_labels,  // Ignored
190         int edit_plugins)  // Ignored
192         int current_aedit = 0;
193         int current_vedit = 0;
195 //printf("Tracks::move_edits 1\n");
196         for(Track *dest_track = track; dest_track; dest_track = dest_track->next)
197         {
198                 if(dest_track->record)
199                 {
200 // Need a local copy of the source edit since the original source edit may
201 // change in the editing operation.
202                         Edit *source_edit = 0;
203                         Track *source_track = 0;
204 // Get source track
205                         if(dest_track->data_type == TRACK_AUDIO &&
206                                 current_aedit < edits->total)
207                         {
208                                 while(current_aedit < edits->total &&
209                                         edits->values[current_aedit]->track->data_type != TRACK_AUDIO)
210                                         current_aedit++;
212                                 if(current_aedit < edits->total)
213                                 {
214                                         source_edit = edits->values[current_aedit];
215                                         source_track = source_edit->track;
216                                         current_aedit++;
217                                 }
218                         }
219                         else
220                         if(dest_track->data_type == TRACK_VIDEO &&
221                                 current_vedit < edits->total)
222                         {
223                                 while(current_vedit < edits->total &&
224                                         edits->values[current_vedit]->track->data_type != TRACK_VIDEO)
225                                         current_vedit++;
227                                 if(current_vedit < edits->total)
228                                 {
229                                         source_edit = edits->values[current_vedit];
230                                         source_track = source_edit->track;
231                                         current_vedit++;
232                                 }
233                         }
235 //printf("Tracks::move_edits 2 %s %s %d\n", source_track->title, dest_track->title, source_edit->length);
236                         if(source_edit)
237                         {
238 // Copy keyframes
239                                 FileXML temp;
240                                 AutoConf temp_autoconf;
241                                 int64_t position_i = source_track->to_units(position, 0);
242 // Source edit changes
243                                 int64_t source_length = source_edit->length;
245                                 temp_autoconf.set_all();
246                                 source_track->automation->copy(source_edit->startproject, 
247                                         source_edit->startproject + source_edit->length, 
248                                         &temp, 
249                                         0,
250                                         0);
251                                 temp.terminate_string();
252                                 temp.rewind();
255 //printf("Tracks::move_edits 1 %d\n", source_edit->length);
256 // Insert new edit
257                                 Edit *dest_edit = dest_track->edits->shift(position_i, 
258                                         source_length);
259                                 Edit *result = dest_track->edits->insert_before(dest_edit, 
260                                         new Edit(edl, dest_track));
261                                 result->copy_from(source_edit);
262                                 result->startproject = position_i;
263                                 result->length = source_length;
265 // Insert new keyframes
266 //printf("Tracks::move_edits 2 %d %p\n", result->startproject, result->asset);
267                                 dest_track->automation->paste_silence(source_edit->startproject, 
268                                         source_edit->startproject + source_length);
269                                 while(!temp.read_tag())
270                                         dest_track->automation->paste(position_i, 
271                                                 source_length, 
272                                                 1.0, 
273                                                 &temp, 
274                                                 0,
275                                                 &temp_autoconf);
277 //printf("Tracks::move_edits 5\n");
278 //dest_track->dump();
280 // Clear source
281                                 int64_t clear_start = source_edit->startproject;
282                                 int64_t clear_end = clear_start + source_length;
284 //printf("Tracks::move_edits 7 %d %d\n", clear_start, clear_end);
285                                 source_track->edits->clear(clear_start, 
286                                         clear_end);
287                                 source_track->automation->clear(clear_start,
288                                         clear_end,
289                                         &temp_autoconf,
290                                         1);
293 //printf("Tracks::move_edits 8 %d %d\n", clear_start, source_edit->length);
294 //dest_track->dump();
295 //printf("Tracks::move_edits 9\n");
296                                 source_track->optimize();
297                                 dest_track->optimize();
298 //printf("Tracks::move_edits 10\n");
299 //dump();
300 //                              delete source_edit;
301                         }
303                 }
304         }
307 void Tracks::move_effect(Plugin *plugin,
308         PluginSet *dest_plugin_set,
309         Track *dest_track, 
310         int64_t dest_position)
312         Track *source_track = plugin->track;
313         Plugin *result = 0;
315 // Insert on an existing plugin set
316         if(!dest_track && dest_plugin_set)
317         {
318                 Track *dest_track = dest_plugin_set->track;
321 // Assume this operation never splits a plugin
322 // Shift destination plugins back
323                 dest_plugin_set->shift(dest_position, plugin->length);
325 // Insert new plugin
326                 Plugin *current = 0;
327                 for(current = (Plugin*)dest_plugin_set->first; current; current = (Plugin*)NEXT)
328                         if(current->startproject >= dest_position) break;
330                 result = (Plugin*)dest_plugin_set->insert_before(current, 
331                         new Plugin(edl, dest_plugin_set, ""));
332         }
333         else
334 // Create a new plugin set
335         {
336                 double length = 0;
337                 double start = 0;
338                 if(edl->local_session->get_selectionend() > 
339                         edl->local_session->get_selectionstart())
340                 {
341                         start = edl->local_session->get_selectionstart();
342                         length = edl->local_session->get_selectionend() - 
343                                 start;
344                 }
345                 else
346                 if(dest_track->get_length() > 0)
347                 {
348                         start = 0;
349                         length = dest_track->get_length();
350                 }
351                 else
352                 {
353                         start = 0;
354                         length = dest_track->from_units(plugin->length);
355                 }
358                 result = dest_track->insert_effect("", 
359                                 &plugin->shared_location, 
360                                 0,
361                                 0,
362                                 start,
363                                 length,
364                                 plugin->plugin_type);
365         }
369         result->copy_from(plugin);
370         result->shift(dest_position - plugin->startproject);
372 // Clear new plugin from old set
373         plugin->plugin_set->clear(plugin->startproject, plugin->startproject + plugin->length);
376         source_track->optimize();
381 int Tracks::concatenate_tracks(int edit_plugins)
383         Track *output_track, *first_output_track, *input_track;
384         int i, data_type = TRACK_AUDIO;
385         double output_start;
386         FileXML *clipboard;
387         int result = 0;
388         IntAuto *play_keyframe = 0;
390 // Relocate tracks
391         for(i = 0; i < 2; i++)
392         {
393 // Get first output track
394                 for(output_track = first; 
395                         output_track; 
396                         output_track = output_track->next)
397                         if(output_track->data_type == data_type && 
398                                 output_track->record) break;
400                 first_output_track = output_track;
402 // Get first input track
403                 for(input_track = first;
404                         input_track;
405                         input_track = input_track->next)
406                 {
407                         if(input_track->data_type == data_type &&
408                                 input_track->play && 
409                                 !input_track->record) break;
410                 }
413                 if(output_track && input_track)
414                 {
415 // Transfer input track to end of output track one at a time
416                         while(input_track)
417                         {
418                                 output_start = output_track->get_length();
419                                 output_track->insert_track(input_track, 
420                                         output_start, 
421                                         0,
422                                         edit_plugins);
424 // Get next source and destination
425                                 for(input_track = input_track->next; 
426                                         input_track; 
427                                         input_track = input_track->next)
428                                 {
430                                         if(input_track->data_type == data_type && 
431                                                 !input_track->record && 
432                                                 input_track->play) break;
433                                 }
435                                 for(output_track = output_track->next; 
436                                         output_track; 
437                                         output_track = output_track->next)
438                                 {
439                                         if(output_track->data_type == data_type && 
440                                                 output_track->record) break;
441                                 }
443                                 if(!output_track)
444                                 {
445                                         output_track = first_output_track;
446                                 }
447                         }
448                         result = 1;
449                 }
451                 if(data_type == TRACK_AUDIO) data_type = TRACK_VIDEO;
452         }
454         return result;
457 int Tracks::delete_audio_track()
459         Track *current;
461         for(current = last; current && current->data_type != TRACK_AUDIO; current = PREVIOUS)
462         {
463                 ;
464         }
466         if(current) delete_track(current);
469 int Tracks::delete_video_track()
471         Track *current;
473         for(current = last; current && current->data_type != TRACK_VIDEO; current = PREVIOUS)
474         {
475                 ;
476         }
478         if(current) delete_track(current);
481 int Tracks::delete_all_tracks()
483         while(last) delete last;
484         return 0;
488 void Tracks::change_modules(int old_location, int new_location, int do_swap)
490         for(Track* current = first ; current; current = current->next)
491         {
492                 current->change_modules(old_location, new_location, do_swap);
493         }
496 void Tracks::change_plugins(SharedLocation &old_location, SharedLocation &new_location, int do_swap)
498         for(Track* current = first ; current; current = current->next)
499         {
500                 current->change_plugins(old_location, new_location, do_swap);
501         }
506 // =========================================== EDL editing
509 int Tracks::copy(double start, 
510         double end, 
511         int all, 
512         FileXML *file, 
513         char *output_path)
515 // nothing selected
516         if(start == end && !all) return 1;
518         Track* current;
520         for(current = first; 
521                 current; 
522                 current = NEXT)
523         {
524                 if(current->record || all)
525                 {
526                         current->copy(start, end, file,output_path);
527                 }
528         }
530         return 0;
535 int Tracks::move_track_up(Track *track)
537         Track *next_track = track->previous;
538         if(!next_track) next_track = last;
540         change_modules(number_of(track), number_of(next_track), 1);
542 // printf("Tracks::move_track_up 1 %p %p\n", track, next_track);
543 // int count = 0;
544 // for(Track *current = first; current && count < 5; current = NEXT, count++)
545 //      printf("Tracks::move_track_up %p %p %p\n", current->previous, current, current->next);
546 // printf("Tracks::move_track_up 2\n");
547 // 
548         swap(track, next_track);
550 // count = 0;
551 // for(Track *current = first; current && count < 5; current = NEXT, count++)
552 //      printf("Tracks::move_track_up %p %p %p\n", current->previous, current, current->next);
553 // printf("Tracks::move_track_up 3\n");
555         return 0;
558 int Tracks::move_track_down(Track *track)
560         Track *next_track = track->next;
561         if(!next_track) next_track = first;
563         change_modules(number_of(track), number_of(next_track), 1);
564         swap(track, next_track);
565         return 0;
569 int Tracks::move_tracks_up()
571         Track *track, *next_track;
572         int result = 0;
574         for(track = first;
575                 track; 
576                 track = next_track)
577         {
578                 next_track = track->next;
580                 if(track->record)
581                 {
582                         if(track->previous)
583                         {
584                                 change_modules(number_of(track->previous), number_of(track), 1);
586                                 swap(track->previous, track);
587                                 result = 1;
588                         }
589                 }
590         }
592         return result;
595 int Tracks::move_tracks_down()
597         Track *track, *previous_track;
598         int result = 0;
599         
600         for(track = last;
601                 track; 
602                 track = previous_track)
603         {
604                 previous_track = track->previous;
606                 if(track->record)
607                 {
608                         if(track->next)
609                         {
610                                 change_modules(number_of(track), number_of(track->next), 1);
612                                 swap(track, track->next);
613                                 result = 1;
614                         }
615                 }
616         }
617         
618         return result;
623 void Tracks::paste_audio_transition(PluginServer *server)
625         for(Track *current = first; current; current = NEXT)
626         {
627                 if(current->data_type == TRACK_AUDIO &&
628                         current->record)
629                 {
630                         int64_t position = current->to_units(
631                                 edl->local_session->get_selectionstart(), 0);
632                         Edit *current_edit = current->edits->editof(position, PLAY_FORWARD);
633                         if(current_edit)
634                         {
635                                 paste_transition(server, current_edit);
636                         }
637                 }
638         }
641 void Tracks::paste_automation(double selectionstart, 
642         FileXML *file,
643         int default_only)
645         Track* current_atrack = 0;
646         Track* current_vtrack = 0;
647         int result = 0;
648         double length;
649         double frame_rate = edl->session->frame_rate;
650         int64_t sample_rate = edl->session->sample_rate;
651         char string[BCTEXTLEN];
652         sprintf(string, "");
654 // Search for start
655         do{
656           result = file->read_tag();
657         }while(!result && 
658                 !file->tag.title_is("AUTO_CLIPBOARD"));
660         if(!result)
661         {
662                 length = file->tag.get_property("LENGTH", 0);
663                 frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
664                 sample_rate = file->tag.get_property("SAMPLERATE", sample_rate);
667                 do
668                 {
669                         result = file->read_tag();
671                         if(!result)
672                         {
673                                 if(file->tag.title_is("/AUTO_CLIPBOARD"))
674                                 {
675                                         result = 1;
676                                 }
677                                 else
678                                 if(file->tag.title_is("TRACK"))
679                                 {
680                                         file->tag.get_property("TYPE", string);
681                                         
682                                         if(!strcmp(string, "AUDIO"))
683                                         {
684 // Get next audio track
685                                                 if(!current_atrack)
686                                                         current_atrack = first;
687                                                 else
688                                                         current_atrack = current_atrack->next;
690                                                 while(current_atrack && 
691                                                         (current_atrack->data_type != TRACK_AUDIO ||
692                                                         !current_atrack->record))
693                                                         current_atrack = current_atrack->next;
695 // Paste it
696                                                 if(current_atrack)
697                                                 {
698                                                         current_atrack->paste_automation(selectionstart,
699                                                                 length,
700                                                                 frame_rate,
701                                                                 sample_rate,
702                                                                 file,
703                                                                 default_only);
704                                                 }
705                                         }
706                                         else
707                                         {
708 // Get next video track
709                                                 if(!current_vtrack)
710                                                         current_vtrack = first;
711                                                 else
712                                                         current_vtrack = current_vtrack->next;
714                                                 while(current_vtrack && 
715                                                         (current_vtrack->data_type != TRACK_VIDEO ||
716                                                         !current_vtrack->record))
717                                                         current_vtrack = current_vtrack->next;
719 // Paste it
720                                                 if(current_vtrack)
721                                                 {
722 //printf("Tracks::paste_automation 1 %s %d\n", current_vtrack->title, current_vtrack->record);
723                                                         current_vtrack->paste_automation(selectionstart,
724                                                                 length,
725                                                                 frame_rate,
726                                                                 sample_rate,
727                                                                 file,
728                                                                 default_only);
729                                                 }
730                                         }
731                                 }
732                         }
733                 }while(!result);
734         }
737 int Tracks::paste_default_keyframe(FileXML *file)
739         paste_automation(0, file, 1);
740         return 0;
743 void Tracks::paste_transition(PluginServer *server, Edit *dest_edit)
745         dest_edit->insert_transition(server->title);
748 void Tracks::paste_video_transition(PluginServer *server, int first_track)
750         for(Track *current = first; current; current = NEXT)
751         {
752                 if(current->data_type == TRACK_VIDEO &&
753                         current->record)
754                 {
755                         int64_t position = current->to_units(
756                                 edl->local_session->get_selectionstart(), 0);
757                         Edit *current_edit = current->edits->editof(position, PLAY_FORWARD);
758                         if(current_edit)
759                         {
760                                 paste_transition(server, current_edit);
761                         }
762                         if(first_track) break;
763                 }
764         }
768 int Tracks::paste_silence(double start, double end, int edit_plugins)
770         Track* current_track;
772         for(current_track = first; 
773                 current_track; 
774                 current_track = current_track->next)
775         {
776                 if(current_track->record) 
777                 { 
778                         current_track->paste_silence(start, end, edit_plugins); 
779                 }
780         }
781         return 0;
786 int Tracks::select_auto(int cursor_x, int cursor_y)
788         int result = 0;
789         for(Track* current = first; current && !result; current = NEXT) { result = current->select_auto(&auto_conf, cursor_x, cursor_y); }
790         return result;
793 int Tracks::move_auto(int cursor_x, int cursor_y, int shift_down)
795         int result = 0;
797         for(Track* current = first; current && !result; current = NEXT) 
798         {
799                 result = current->move_auto(&auto_conf, cursor_x, cursor_y, shift_down); 
800         }
801         return 0;
804 int Tracks::modify_edithandles(double &oldposition, 
805         double &newposition, 
806         int currentend, 
807         int handle_mode,
808         int edit_labels,
809         int edit_plugins)
811         Track *current;
813         for(current = first; current; current = NEXT)
814         {
815                 if(current->record)
816                 {
817                         current->modify_edithandles(oldposition, 
818                                 newposition, 
819                                 currentend, 
820                                 handle_mode,
821                                 edit_labels,
822                                 edit_plugins);
823                 }
824         }
825         return 0;
828 int Tracks::modify_pluginhandles(double &oldposition, 
829         double &newposition, 
830         int currentend, 
831         int handle_mode,
832         int edit_labels)
834         Track *current;
836         for(current = first; current; current = NEXT)
837         {
838                 if(current->record)
839                 {
840                         current->modify_pluginhandles(oldposition, 
841                                 newposition, 
842                                 currentend, 
843                                 handle_mode,
844                                 edit_labels);
845                 }
846         }
847         return 0;
852 int Tracks::purge_asset(Asset *asset)
854         Track *current_track;
855         int result = 0;
856         
857         for(current_track = first; current_track; current_track = current_track->next)
858         {
859                 result += current_track->purge_asset(asset); 
860         }
861         return result;
864 int Tracks::asset_used(Asset *asset)
866         Track *current_track;
867         int result = 0;
868         
869         for(current_track = first; current_track; current_track = current_track->next)
870         {
871                 result += current_track->asset_used(asset); 
872         }
873         return result;
876 int Tracks::scale_time(float rate_scale, int ignore_record, int scale_edits, int scale_autos, int64_t start, int64_t end)
878         Track *current_track;
880         for(current_track = first; 
881                 current_track; 
882                 current_track = current_track->next)
883         {
884                 if((current_track->record || ignore_record) && 
885                         current_track->data_type == TRACK_VIDEO)
886                 {
887                         current_track->scale_time(rate_scale, scale_edits, scale_autos, start, end);
888                 }
889         }
890         return 0;