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