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 Plugin *plugin = (Plugin*)editof(pos, dir, 0);
164 if( !plugin ) return 0;
165 KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
166 plugin->keyframes->nearest_after(pos) :
167 plugin->keyframes->nearest_before(pos));
171 int PluginSet::get_number()
173 return track->plugin_set.number_of(this);
176 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
181 for(Plugin *current = (Plugin*)first;
183 current = (Plugin*)NEXT)
185 current->keyframes->clear(start, end, 1);
190 Edits::clear(start, end);
193 //void PluginSet::clear_recursive(int64_t start, int64_t end)
195 //printf("PluginSet::clear_recursive 1\n");
196 // clear(start, end, 1);
199 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
201 // Plugin keyframes are shifted in shift_effects
204 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
206 // Effects are shifted in length extension
210 void PluginSet::clear_keyframes(int64_t start, int64_t end)
212 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
214 current->clear_keyframes(start, end);
218 void PluginSet::copy_keyframes(int64_t start,
224 file->tag.set_title("PLUGINSET");
226 file->append_newline();
228 for(Plugin *current = (Plugin*)first;
230 current = (Plugin*)NEXT)
232 current->copy_keyframes(start, end, file, default_only, active_only);
235 file->tag.set_title("/PLUGINSET");
237 file->append_newline();
241 void PluginSet::paste_keyframes(int64_t start,
248 int first_keyframe = 1;
254 result = file->read_tag();
258 if(file->tag.title_is("/PLUGINSET"))
261 if(file->tag.title_is("KEYFRAME"))
263 int64_t position = file->tag.get_property("POSITION", 0);
264 if(first_keyframe && default_only)
273 // Get plugin owning keyframe
274 for(current = (Plugin*)last;
276 current = (Plugin*)PREVIOUS)
278 // We want keyframes to exist beyond the end of the last plugin to
279 // make editing intuitive, but it will always be possible to
280 // paste keyframes from one plugin into an incompatible plugin.
281 if(position >= current->startproject)
283 KeyFrame *keyframe = 0;
284 if(file->tag.get_property("DEFAULT", 0) || default_only)
286 keyframe = (KeyFrame*)current->keyframes->default_auto;
292 (KeyFrame*)current->keyframes->insert_auto(position);
297 keyframe->load(file);
298 keyframe->position = position;
310 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
312 for(Plugin *current = (Plugin*)first;
314 current = (Plugin*)NEXT)
316 // Shift beginning of this effect
317 if(current->startproject >= start)
319 current->startproject += length;
322 // Extend end of this effect.
323 // In loading new files, the effect should extend to fill the entire track.
324 // In muting, the effect must extend to fill the gap if another effect follows.
325 // The user should use Settings->edit effects to disable this.
326 if(current->startproject + current->length >= start)
328 current->length += length;
331 // Shift keyframes in this effect.
332 // If the default keyframe lands on the starting point, it must be shifted
333 // since the effect start is shifted.
334 if(edit_autos && current->keyframes->default_auto->position >= start)
335 current->keyframes->default_auto->position += length;
337 if(edit_autos) current->keyframes->paste_silence(start, start + length);
341 void PluginSet::paste_silence(int64_t start, int64_t end, int edit_autos)
343 Plugin *new_plugin = (Plugin *) insert_new_edit(start);
344 int64_t length = end - start;
345 new_plugin->length = length;
346 while( (new_plugin=(Plugin *)new_plugin->next) != 0 ) {
347 new_plugin->startproject += length;
348 if( !edit_autos ) continue;
349 new_plugin->keyframes->default_auto->position += length;
350 new_plugin->keyframes->paste_silence(start, end);
354 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
356 file->tag.set_title("PLUGINSET");
357 file->tag.set_property("RECORD", record);
359 file->append_newline();
361 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
363 current->copy(start, end, file);
366 file->tag.set_title("/PLUGINSET");
368 file->append_newline();
371 void PluginSet::save(FileXML *file)
373 copy(0, length(), file);
376 void PluginSet::load(FileXML *file, uint32_t load_flags)
379 // Current plugin being amended
380 Plugin *plugin = (Plugin*)first;
381 int64_t startproject = 0;
383 record = file->tag.get_property("RECORD", record);
385 result = file->read_tag();
390 if(file->tag.title_is("/PLUGINSET"))
395 if(file->tag.title_is("PLUGIN"))
397 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
398 int plugin_type = file->tag.get_property("TYPE", 1);
399 char title[BCTEXTLEN];
401 file->tag.get_property("TITLE", title);
402 Plugin::fix_plugin_title(title);
403 SharedLocation shared_location;
404 shared_location.load(file);
407 if(load_flags & LOAD_EDITS)
409 plugin = insert_plugin(title,
417 startproject += length;
420 if(load_flags & LOAD_AUTOMATION)
425 plugin = (Plugin*)plugin->next;
435 int PluginSet::optimize()
438 Plugin *current_edit;
440 // trim plugins before position 0
441 while( first && first->startproject+first->length < 0 )
443 if( first && first->startproject < 0 ) {
444 first->length += first->startproject;
445 first->startproject = 0;
448 // Delete keyframes out of range
449 for(current_edit = (Plugin*)first;
451 current_edit = (Plugin*)current_edit->next)
453 current_edit->keyframes->default_auto->position = 0;
454 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
457 KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
458 if(current_keyframe->position >
459 current_edit->startproject + current_edit->length ||
460 current_keyframe->position < current_edit->startproject)
462 delete current_keyframe;
464 current_keyframe = previous_keyframe;
468 // Insert silence between plugins
469 for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
471 if(current->previous)
473 Plugin *previous = (Plugin*)PREVIOUS;
475 if(current->startproject -
476 previous->startproject -
477 previous->length > 0)
479 Plugin *new_plugin = (Plugin*)create_edit();
480 insert_before(current, new_plugin);
481 new_plugin->startproject = previous->startproject +
483 new_plugin->length = current->startproject -
484 previous->startproject -
489 if(current->startproject > 0)
491 Plugin *new_plugin = (Plugin*)create_edit();
492 insert_before(current, new_plugin);
493 new_plugin->length = current->startproject;
498 // delete 0 length plugins
503 for(current_edit = (Plugin*)first; !result && current_edit; ) {
504 Plugin* next = (Plugin*)current_edit->next;
505 if(current_edit->length == 0) {
513 // merge identical plugins with same keyframes
514 for( current_edit = (Plugin*)first;
515 !result && current_edit && current_edit->next; ) {
516 Plugin *next_edit = (Plugin*)current_edit->next;
519 if(next_edit->identical(current_edit)) {
520 current_edit->length += next_edit->length;
522 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
524 source = (KeyFrame*)source->next) {
525 KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
526 dest->copy_from(source);
527 current_edit->keyframes->append(dest);
534 current_edit = next_edit;
536 // delete last edit if 0 length or silence
537 if( last && (last->silence() || !last->length) ) {
551 void PluginSet::dump(FILE *fp)
553 fprintf(fp," PLUGIN_SET:\n");
554 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)