group edge handle drag, expanders.txt, delete spurious auto in copy edl
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / pluginset.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "edl.h"
23 #include "edlsession.h"
24 #include "filexml.h"
25 #include "keyframe.h"
26 #include "keyframes.h"
27 #include "plugin.h"
28 #include "pluginautos.h"
29 #include "pluginset.h"
30 #include "track.h"
31 #include "transportque.inc"
32
33 #include <string.h>
34
35 PluginSet::PluginSet(EDL *edl, Track *track)
36  : Edits(edl, track)
37 {
38         record = 1;
39 }
40
41 PluginSet::~PluginSet()
42 {
43         while(last) delete last;
44 }
45
46
47 PluginSet& PluginSet::operator=(PluginSet& plugins)
48 {
49 printf("PluginSet::operator= 1\n");
50         copy_from(&plugins);
51         return *this;
52 }
53
54 void PluginSet::copy_from(PluginSet *src)
55 {
56         while(last) delete last;
57         for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
58         {
59                 Plugin *new_plugin;
60                 append(new_plugin = (Plugin*)create_edit());
61                 new_plugin->copy_from(current);
62         }
63         this->record = src->record;
64 }
65
66 Plugin* PluginSet::get_first_plugin()
67 {
68 // Called when a new pluginset is added.
69 // Get first non-silence plugin in the plugin set.
70         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
71         {
72                 if(current && current->plugin_type != PLUGIN_NONE)
73                 {
74                         return current;
75                 }
76         }
77         return 0;
78 }
79
80 int64_t PluginSet::plugin_change_duration(int64_t input_position,
81         int64_t input_length,
82         int reverse)
83 {
84         int result = input_length;
85         Edit *current;
86
87         if(reverse)
88         {
89                 int input_start = input_position - input_length;
90                 for(current = last; current; current = PREVIOUS)
91                 {
92                         int start = current->startproject;
93                         int end = start + current->length;
94                         if(end > input_start && end < input_position)
95                         {
96                                 result = input_position - end;
97                                 return result;
98                         }
99                         else
100                         if(start > input_start && start < input_position)
101                         {
102                                 result = input_position - start;
103                                 return result;
104                         }
105                 }
106         }
107         else
108         {
109                 int input_end = input_position + input_length;
110                 for(current = first; current; current = NEXT)
111                 {
112                         int start = current->startproject;
113                         int end = start + current->length;
114                         if(start > input_position && start < input_end)
115                         {
116                                 result = start - input_position;
117                                 return result;
118                         }
119                         else
120                         if(end > input_position && end < input_end)
121                         {
122                                 result = end - input_position;
123                                 return result;
124                         }
125                 }
126         }
127         return input_length;
128 }
129
130 void PluginSet::synchronize_params(PluginSet *plugin_set)
131 {
132         for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
133                 this_plugin && that_plugin;
134                 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
135         {
136                 this_plugin->synchronize_params(that_plugin);
137         }
138 }
139
140 Plugin* PluginSet::insert_plugin(const char *title,
141         int64_t unit_position, int64_t unit_length, int plugin_type,
142         SharedLocation *shared_location, KeyFrame *default_keyframe,
143         int do_optimize)
144 {
145         Plugin *plugin = (Plugin*)create_silence(unit_position, unit_position + unit_length);
146         plugin->init(title, unit_position, unit_length, plugin_type,
147                         shared_location, default_keyframe);
148 // May delete the plugin we just added so not desirable while loading.
149         if( do_optimize ) optimize();
150         return plugin;
151 }
152
153 Edit* PluginSet::create_edit()
154 {
155         Plugin* result = new Plugin(edl, this, "");
156         return result;
157 }
158
159 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
160 {
161         Plugin *current = new Plugin(edl, this, "");
162         List<Edit>::insert_after(previous_edit, current);
163         return (Edit*)current;
164 }
165
166 KeyFrame *PluginSet::nearest_keyframe(int64_t pos, int dir)
167 {
168         Plugin *plugin = (Plugin*)editof(pos, dir, 0);
169         if( !plugin ) return 0;
170         KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
171                 plugin->keyframes->nearest_after(pos) :
172                 plugin->keyframes->nearest_before(pos));
173         return keyframe;
174 }
175
176 int PluginSet::get_number()
177 {
178         return track->plugin_set.number_of(this);
179 }
180
181 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
182 {
183         if(edit_autos)
184         {
185 // Clear keyframes
186                 for(Plugin *current = (Plugin*)first;
187                         current;
188                         current = (Plugin*)NEXT)
189                 {
190                         current->keyframes->clear(start, end, 1);
191                 }
192         }
193
194 // Clear edits
195         Edits::clear(start, end);
196 }
197
198 //void PluginSet::clear_recursive(int64_t start, int64_t end)
199 //{
200 //printf("PluginSet::clear_recursive 1\n");
201 //      clear(start, end, 1);
202 //}
203
204 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
205 {
206 // Plugin keyframes are shifted in shift_effects
207 }
208
209 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
210 {
211 // Effects are shifted in length extension
212 }
213
214
215 void PluginSet::clear_keyframes(int64_t start, int64_t end)
216 {
217         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
218         {
219                 current->clear_keyframes(start, end);
220         }
221 }
222
223 void PluginSet::copy_keyframes(int64_t start,
224         int64_t end,
225         FileXML *file,
226         int default_only,
227         int active_only)
228 {
229         file->tag.set_title("PLUGINSET");
230         file->append_tag();
231         file->append_newline();
232
233         for(Plugin *current = (Plugin*)first;
234                 current;
235                 current = (Plugin*)NEXT)
236         {
237                 current->copy_keyframes(start, end, file, default_only, active_only);
238         }
239
240         file->tag.set_title("/PLUGINSET");
241         file->append_tag();
242         file->append_newline();
243 }
244
245
246 void PluginSet::paste_keyframes(int64_t start,
247         int64_t length,
248         FileXML *file,
249         int default_only,
250         int active_only)
251 {
252         int result = 0;
253         int first_keyframe = 1;
254         Plugin *current;
255
256
257         while(!result)
258         {
259                 result = file->read_tag();
260
261                 if(!result)
262                 {
263                         if(file->tag.title_is("/PLUGINSET"))
264                                 result = 1;
265                         else
266                         if(file->tag.title_is("KEYFRAME"))
267                         {
268                                 int64_t position = file->tag.get_property("POSITION", 0);
269                                 if(first_keyframe && default_only)
270                                 {
271                                         position = start;
272                                 }
273                                 else
274                                 {
275                                         position += start;
276                                 }
277
278 // Get plugin owning keyframe
279                                 for(current = (Plugin*)last;
280                                         current;
281                                         current = (Plugin*)PREVIOUS)
282                                 {
283 // We want keyframes to exist beyond the end of the last plugin to
284 // make editing intuitive, but it will always be possible to
285 // paste keyframes from one plugin into an incompatible plugin.
286                                         if(position >= current->startproject)
287                                         {
288                                                 KeyFrame *keyframe = 0;
289                                                 if(file->tag.get_property("DEFAULT", 0) || default_only)
290                                                 {
291                                                         keyframe = (KeyFrame*)current->keyframes->default_auto;
292                                                 }
293                                                 else
294                                                 if(!default_only)
295                                                 {
296                                                         keyframe =
297                                                                 (KeyFrame*)current->keyframes->insert_auto(position);
298                                                 }
299
300                                                 if(keyframe)
301                                                 {
302                                                         keyframe->load(file);
303                                                         keyframe->position = position;
304                                                 }
305                                                 break;
306                                         }
307                                 }
308
309                                 first_keyframe = 0;
310                         }
311                 }
312         }
313 }
314
315 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
316 {
317         for(Plugin *current = (Plugin*)first;
318                 current;
319                 current = (Plugin*)NEXT)
320         {
321 // Shift beginning of this effect
322                 if(current->startproject >= start)
323                 {
324                         current->startproject += length;
325                 }
326                 else
327 // Extend end of this effect.
328 // In loading new files, the effect should extend to fill the entire track.
329 // In muting, the effect must extend to fill the gap if another effect follows.
330 // The user should use Settings->edit effects to disable this.
331                 if(current->startproject + current->length >= start)
332                 {
333                         current->length += length;
334                 }
335
336 // Shift keyframes in this effect.
337 // If the default keyframe lands on the starting point, it must be shifted
338 // since the effect start is shifted.
339                 if(edit_autos && current->keyframes->default_auto->position >= start)
340                         current->keyframes->default_auto->position += length;
341
342                 if(edit_autos) current->keyframes->paste_silence(start, start + length);
343         }
344 }
345
346 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
347 {
348         file->tag.set_title("PLUGINSET");
349         file->tag.set_property("RECORD", record);
350         file->append_tag();
351         file->append_newline();
352
353         for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
354         {
355                 current->copy(start, end, file);
356         }
357
358         file->tag.set_title("/PLUGINSET");
359         file->append_tag();
360         file->append_newline();
361 }
362
363 void PluginSet::save(FileXML *file)
364 {
365         copy(0, length(), file);
366 }
367
368 void PluginSet::load(FileXML *file, uint32_t load_flags)
369 {
370         int result = 0;
371 // Current plugin being amended
372         Plugin *plugin = (Plugin*)first;
373         int64_t startproject = 0;
374
375         record = file->tag.get_property("RECORD", record);
376         do{
377                 result = file->read_tag();
378
379
380                 if(!result)
381                 {
382                         if(file->tag.title_is("/PLUGINSET"))
383                         {
384                                 result = 1;
385                         }
386                         else
387                         if(file->tag.title_is("PLUGIN"))
388                         {
389                                 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
390                                 int plugin_type = file->tag.get_property("TYPE", 1);
391                                 char title[BCTEXTLEN];
392                                 title[0] = 0;
393                                 file->tag.get_property("TITLE", title);
394                                 Plugin::fix_plugin_title(title);
395                                 SharedLocation shared_location;
396                                 shared_location.load(file);
397
398
399                                 if(load_flags & LOAD_EDITS)
400                                 {
401                                         plugin = insert_plugin(title,
402                                                 startproject,
403                                                 length,
404                                                 plugin_type,
405                                                 &shared_location,
406                                                 0,
407                                                 0);
408                                         plugin->load(file);
409                                         startproject += length;
410                                 }
411                                 else
412                                 if(load_flags & LOAD_AUTOMATION)
413                                 {
414                                         if(plugin)
415                                         {
416                                                 plugin->load(file);
417                                                 plugin = (Plugin*)plugin->next;
418                                         }
419                                 }
420                         }
421                 }
422         } while(!result);
423
424         optimize();
425 }
426
427 int PluginSet::optimize()
428 {
429         int result = 1;
430         Plugin *current_edit;
431
432
433 // Delete keyframes out of range
434         for(current_edit = (Plugin*)first;
435                 current_edit;
436                 current_edit = (Plugin*)current_edit->next)
437         {
438                 current_edit->keyframes->default_auto->position = 0;
439                 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
440                         current_keyframe; )
441                 {
442                         KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
443                         if(current_keyframe->position >
444                                 current_edit->startproject + current_edit->length ||
445                                 current_keyframe->position < current_edit->startproject)
446                         {
447                                 delete current_keyframe;
448                         }
449                         current_keyframe = previous_keyframe;
450                 }
451         }
452
453 // Insert silence between plugins
454         for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
455         {
456                 if(current->previous)
457                 {
458                         Plugin *previous = (Plugin*)PREVIOUS;
459
460                         if(current->startproject -
461                                 previous->startproject -
462                                 previous->length > 0)
463                         {
464                                 Plugin *new_plugin = (Plugin*)create_edit();
465                                 insert_before(current, new_plugin);
466                                 new_plugin->startproject = previous->startproject +
467                                         previous->length;
468                                 new_plugin->length = current->startproject -
469                                         previous->startproject -
470                                         previous->length;
471                         }
472                 }
473                 else
474                 if(current->startproject > 0)
475                 {
476                         Plugin *new_plugin = (Plugin*)create_edit();
477                         insert_before(current, new_plugin);
478                         new_plugin->length = current->startproject;
479                 }
480         }
481
482
483 // delete 0 length plugins
484         while(result)
485         {
486                 result = 0;
487
488                 for(current_edit = (Plugin*)first; !result && current_edit; ) {
489                         Plugin* next = (Plugin*)current_edit->next;
490                         if(current_edit->length == 0) {
491                                 delete current_edit;
492                                 result = 1;
493                         }
494                         current_edit = next;
495                 }
496
497
498 // merge identical plugins with same keyframes
499                 for( current_edit = (Plugin*)first;
500                         !result && current_edit && current_edit->next; ) {
501                         Plugin *next_edit = (Plugin*)current_edit->next;
502
503 // plugins identical
504                         if(next_edit->identical(current_edit)) {
505                                 current_edit->length += next_edit->length;
506 // Merge keyframes
507                                 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
508                                         source;
509                                         source = (KeyFrame*)source->next) {
510                                         KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
511                                         dest->copy_from(source);
512                                         current_edit->keyframes->append(dest);
513                                 }
514                                 remove(next_edit);
515                                 result = 1;
516                                 continue;
517                         }
518
519                         current_edit = next_edit;
520
521 // delete last edit if 0 length or silence
522                         if( last && (last->silence() || !last->length) ) {
523                                 delete last;
524                                 result = 1;
525                         }
526                 }
527         }
528
529         return 0;
530 }
531
532
533
534
535
536 void PluginSet::dump(FILE *fp)
537 {
538         fprintf(fp,"   PLUGIN_SET:\n");
539         for(Plugin *current = (Plugin*)first; current; current =  (Plugin*)NEXT)
540                 current->dump(fp);
541 }