4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "edlsession.h"
26 #include "keyframes.h"
28 #include "pluginautos.h"
29 #include "pluginset.h"
31 #include "transportque.inc"
35 PluginSet::PluginSet(EDL *edl, Track *track)
41 PluginSet::~PluginSet()
43 while(last) delete last;
47 void PluginSet::copy_from(PluginSet *src)
49 while(last) delete last;
50 for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
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;
58 this->record = src->record;
61 Plugin* PluginSet::get_first_plugin()
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)
67 if(current && current->plugin_type != PLUGIN_NONE)
75 int64_t PluginSet::plugin_change_duration(int64_t input_position,
79 int result = input_length;
84 int input_start = input_position - input_length;
85 for(current = last; current; current = PREVIOUS)
87 int start = current->startproject;
88 int end = start + current->length;
89 if(end > input_start && end < input_position)
91 result = input_position - end;
95 if(start > input_start && start < input_position)
97 result = input_position - start;
104 int input_end = input_position + input_length;
105 for(current = first; current; current = NEXT)
107 int start = current->startproject;
108 int end = start + current->length;
109 if(start > input_position && start < input_end)
111 result = start - input_position;
115 if(end > input_position && end < input_end)
117 result = end - input_position;
125 void PluginSet::synchronize_params(PluginSet *plugin_set)
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)
131 this_plugin->synchronize_params(that_plugin);
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,
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();
148 Edit* PluginSet::create_edit()
150 Plugin* result = new Plugin(edl, this, "");
154 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
156 Plugin *current = new Plugin(edl, this, "");
157 List<Edit>::insert_after(previous_edit, current);
158 return (Edit*)current;
161 KeyFrame *PluginSet::nearest_keyframe(int64_t pos, int dir)
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);
176 for( ; !keyframe && plugin; plugin=(Plugin*)plugin->previous ) {
177 if( plugin->plugin_type == PLUGIN_NONE ) continue;
178 keyframe = (KeyFrame *)plugin->keyframes->nearest_before(pos);
184 int PluginSet::get_number()
186 return track->plugin_set.number_of(this);
189 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
194 for(Plugin *current = (Plugin*)first;
196 current = (Plugin*)NEXT)
198 current->keyframes->clear(start, end, 1);
203 Edits::clear(start, end);
206 //void PluginSet::clear_recursive(int64_t start, int64_t end)
208 //printf("PluginSet::clear_recursive 1\n");
209 // clear(start, end, 1);
212 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
214 // Plugin keyframes are shifted in shift_effects
217 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
219 // Effects are shifted in length extension
223 void PluginSet::clear_keyframes(int64_t start, int64_t end)
225 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
227 current->clear_keyframes(start, end);
231 void PluginSet::copy_keyframes(int64_t start,
237 file->tag.set_title("PLUGINSET");
239 file->append_newline();
241 for(Plugin *current = (Plugin*)first;
243 current = (Plugin*)NEXT)
245 current->copy_keyframes(start, end, file, default_only, active_only);
248 file->tag.set_title("/PLUGINSET");
250 file->append_newline();
254 void PluginSet::paste_keyframes(int64_t start,
261 int first_keyframe = 1;
267 result = file->read_tag();
271 if(file->tag.title_is("/PLUGINSET"))
274 if(file->tag.title_is("KEYFRAME"))
276 int64_t position = file->tag.get_property("POSITION", 0);
277 if(first_keyframe && default_only)
286 // Get plugin owning keyframe
287 for(current = (Plugin*)last;
289 current = (Plugin*)PREVIOUS)
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)
296 KeyFrame *keyframe = 0;
297 if(file->tag.get_property("DEFAULT", 0) || default_only)
299 keyframe = (KeyFrame*)current->keyframes->default_auto;
305 (KeyFrame*)current->keyframes->insert_auto(position);
310 keyframe->load(file);
311 keyframe->position = position;
323 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
325 for(Plugin *current = (Plugin*)first;
327 current = (Plugin*)NEXT)
329 // Shift beginning of this effect
330 if(current->startproject >= start)
332 current->startproject += length;
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)
341 current->length += length;
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;
350 if(edit_autos) current->keyframes->paste_silence(start, start + length);
354 void PluginSet::paste_silence(int64_t start, int64_t end, int edit_autos)
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);
367 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
369 file->tag.set_title("PLUGINSET");
370 file->tag.set_property("RECORD", record);
372 file->append_newline();
374 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
376 current->copy(start, end, file);
379 file->tag.set_title("/PLUGINSET");
381 file->append_newline();
384 void PluginSet::save(FileXML *file)
386 copy(0, length(), file);
389 void PluginSet::load(FileXML *file, uint32_t load_flags)
392 // Current plugin being amended
393 Plugin *plugin = (Plugin*)first;
394 int64_t startproject = 0;
396 record = file->tag.get_property("RECORD", record);
398 result = file->read_tag();
403 if(file->tag.title_is("/PLUGINSET"))
408 if(file->tag.title_is("PLUGIN"))
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];
414 file->tag.get_property("TITLE", title);
415 Plugin::fix_plugin_title(title);
416 SharedLocation shared_location;
417 shared_location.load(file);
420 if(load_flags & LOAD_EDITS)
422 plugin = insert_plugin(title,
430 startproject += length;
433 if(load_flags & LOAD_AUTOMATION)
438 plugin = (Plugin*)plugin->next;
448 int PluginSet::optimize()
451 Plugin *current_edit;
453 // trim plugins before position 0
454 while( first && first->startproject+first->length < 0 )
456 if( first && first->startproject < 0 ) {
457 first->length += first->startproject;
458 first->startproject = 0;
461 // Delete keyframes out of range
462 for(current_edit = (Plugin*)first;
464 current_edit = (Plugin*)current_edit->next)
466 current_edit->keyframes->default_auto->position = 0;
467 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
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)
475 delete current_keyframe;
477 current_keyframe = previous_keyframe;
481 // Insert silence between plugins
482 for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
484 if(current->previous)
486 Plugin *previous = (Plugin*)PREVIOUS;
488 if(current->startproject -
489 previous->startproject -
490 previous->length > 0)
492 Plugin *new_plugin = (Plugin*)create_edit();
493 insert_before(current, new_plugin);
494 new_plugin->startproject = previous->startproject +
496 new_plugin->length = current->startproject -
497 previous->startproject -
502 if(current->startproject > 0)
504 Plugin *new_plugin = (Plugin*)create_edit();
505 insert_before(current, new_plugin);
506 new_plugin->length = current->startproject;
511 // delete 0 length plugins
516 for(current_edit = (Plugin*)first; !result && current_edit; ) {
517 Plugin* next = (Plugin*)current_edit->next;
518 if(current_edit->length == 0) {
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;
532 if(next_edit->identical(current_edit)) {
533 current_edit->length += next_edit->length;
535 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
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);
547 current_edit = next_edit;
549 // delete last edit if 0 length or silence
550 if( last && (last->silence() || !last->length) ) {
564 void PluginSet::dump(FILE *fp)
566 fprintf(fp," PLUGIN_SET:\n");
567 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)