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 Plugin *plugin = (Plugin*)editof(pos, dir, 0);
168 if( !plugin ) return 0;
169 KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
170 plugin->keyframes->nearest_after(pos) :
171 plugin->keyframes->nearest_before(pos));
175 int PluginSet::get_number()
177 return track->plugin_set.number_of(this);
180 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
185 for(Plugin *current = (Plugin*)first;
187 current = (Plugin*)NEXT)
189 current->keyframes->clear(start, end, 1);
194 Edits::clear(start, end);
197 //void PluginSet::clear_recursive(int64_t start, int64_t end)
199 //printf("PluginSet::clear_recursive 1\n");
200 // clear(start, end, 1);
203 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
205 // Plugin keyframes are shifted in shift_effects
208 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
210 // Effects are shifted in length extension
214 void PluginSet::clear_keyframes(int64_t start, int64_t end)
216 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
218 current->clear_keyframes(start, end);
222 void PluginSet::copy_keyframes(int64_t start,
228 file->tag.set_title("PLUGINSET");
230 file->append_newline();
232 for(Plugin *current = (Plugin*)first;
234 current = (Plugin*)NEXT)
236 current->copy_keyframes(start, end, file, default_only, active_only);
239 file->tag.set_title("/PLUGINSET");
241 file->append_newline();
245 void PluginSet::paste_keyframes(int64_t start,
252 int first_keyframe = 1;
258 result = file->read_tag();
262 if(file->tag.title_is("/PLUGINSET"))
265 if(file->tag.title_is("KEYFRAME"))
267 int64_t position = file->tag.get_property("POSITION", 0);
268 if(first_keyframe && default_only)
277 // Get plugin owning keyframe
278 for(current = (Plugin*)last;
280 current = (Plugin*)PREVIOUS)
282 // We want keyframes to exist beyond the end of the last plugin to
283 // make editing intuitive, but it will always be possible to
284 // paste keyframes from one plugin into an incompatible plugin.
285 if(position >= current->startproject)
287 KeyFrame *keyframe = 0;
288 if(file->tag.get_property("DEFAULT", 0) || default_only)
290 keyframe = (KeyFrame*)current->keyframes->default_auto;
296 (KeyFrame*)current->keyframes->insert_auto(position);
301 keyframe->load(file);
302 keyframe->position = position;
314 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
316 for(Plugin *current = (Plugin*)first;
318 current = (Plugin*)NEXT)
320 // Shift beginning of this effect
321 if(current->startproject >= start)
323 current->startproject += length;
326 // Extend end of this effect.
327 // In loading new files, the effect should extend to fill the entire track.
328 // In muting, the effect must extend to fill the gap if another effect follows.
329 // The user should use Settings->edit effects to disable this.
330 if(current->startproject + current->length >= start)
332 current->length += length;
335 // Shift keyframes in this effect.
336 // If the default keyframe lands on the starting point, it must be shifted
337 // since the effect start is shifted.
338 if(edit_autos && current->keyframes->default_auto->position >= start)
339 current->keyframes->default_auto->position += length;
341 if(edit_autos) current->keyframes->paste_silence(start, start + length);
345 void PluginSet::paste_silence(int64_t start, int64_t end, int edit_autos)
347 Plugin *new_plugin = (Plugin *) insert_new_edit(start);
348 int64_t length = end - start;
349 new_plugin->length = length;
350 while( (new_plugin=(Plugin *)new_plugin->next) != 0 ) {
351 new_plugin->startproject += length;
352 if( !edit_autos ) continue;
353 new_plugin->keyframes->default_auto->position += length;
354 new_plugin->keyframes->paste_silence(start, end);
358 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
360 file->tag.set_title("PLUGINSET");
361 file->tag.set_property("RECORD", record);
363 file->append_newline();
365 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
367 current->copy(start, end, file);
370 file->tag.set_title("/PLUGINSET");
372 file->append_newline();
375 void PluginSet::save(FileXML *file)
377 copy(0, length(), file);
380 void PluginSet::load(FileXML *file, uint32_t load_flags)
383 // Current plugin being amended
384 Plugin *plugin = (Plugin*)first;
385 int64_t startproject = 0;
387 record = file->tag.get_property("RECORD", record);
389 result = file->read_tag();
394 if(file->tag.title_is("/PLUGINSET"))
399 if(file->tag.title_is("PLUGIN"))
401 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
402 int plugin_type = file->tag.get_property("TYPE", 1);
403 char title[BCTEXTLEN];
405 file->tag.get_property("TITLE", title);
406 Plugin::fix_plugin_title(title);
407 SharedLocation shared_location;
408 shared_location.load(file);
411 if(load_flags & LOAD_EDITS)
413 plugin = insert_plugin(title,
421 startproject += length;
424 if(load_flags & LOAD_AUTOMATION)
429 plugin = (Plugin*)plugin->next;
439 int PluginSet::optimize()
442 Plugin *current_edit;
444 // trim plugins before position 0
445 while( first && first->startproject+first->length < 0 )
447 if( first && first->startproject < 0 ) {
448 first->length += first->startproject;
449 first->startproject = 0;
452 // Delete keyframes out of range
453 for(current_edit = (Plugin*)first;
455 current_edit = (Plugin*)current_edit->next)
457 current_edit->keyframes->default_auto->position = 0;
458 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
461 KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
462 if(current_keyframe->position >
463 current_edit->startproject + current_edit->length ||
464 current_keyframe->position < current_edit->startproject)
466 delete current_keyframe;
468 current_keyframe = previous_keyframe;
472 // Insert silence between plugins
473 for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
475 if(current->previous)
477 Plugin *previous = (Plugin*)PREVIOUS;
479 if(current->startproject -
480 previous->startproject -
481 previous->length > 0)
483 Plugin *new_plugin = (Plugin*)create_edit();
484 insert_before(current, new_plugin);
485 new_plugin->startproject = previous->startproject +
487 new_plugin->length = current->startproject -
488 previous->startproject -
493 if(current->startproject > 0)
495 Plugin *new_plugin = (Plugin*)create_edit();
496 insert_before(current, new_plugin);
497 new_plugin->length = current->startproject;
502 // delete 0 length plugins
507 for(current_edit = (Plugin*)first; !result && current_edit; ) {
508 Plugin* next = (Plugin*)current_edit->next;
509 if(current_edit->length == 0) {
517 // merge identical plugins with same keyframes
518 for( current_edit = (Plugin*)first;
519 !result && current_edit && current_edit->next; ) {
520 Plugin *next_edit = (Plugin*)current_edit->next;
523 if(next_edit->identical(current_edit)) {
524 current_edit->length += next_edit->length;
526 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
528 source = (KeyFrame*)source->next) {
529 KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
530 dest->copy_from(source);
531 current_edit->keyframes->append(dest);
538 current_edit = next_edit;
540 // delete last edit if 0 length or silence
541 if( last && (last->silence() || !last->length) ) {
555 void PluginSet::dump(FILE *fp)
557 fprintf(fp," PLUGIN_SET:\n");
558 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)