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