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 PluginSet& PluginSet::operator=(PluginSet& plugins)
49 printf("PluginSet::operator= 1\n");
54 void PluginSet::copy_from(PluginSet *src)
56 while(last) delete last;
57 for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
60 append(new_plugin = (Plugin*)create_edit());
61 new_plugin->copy_from(current);
63 this->record = src->record;
66 Plugin* PluginSet::get_first_plugin()
68 // Called when a new pluginset is added.
69 // Get first non-silence plugin in the plugin set.
70 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
72 if(current && current->plugin_type != PLUGIN_NONE)
80 int64_t PluginSet::plugin_change_duration(int64_t input_position,
84 int result = input_length;
89 int input_start = input_position - input_length;
90 for(current = last; current; current = PREVIOUS)
92 int start = current->startproject;
93 int end = start + current->length;
94 if(end > input_start && end < input_position)
96 result = input_position - end;
100 if(start > input_start && start < input_position)
102 result = input_position - start;
109 int input_end = input_position + input_length;
110 for(current = first; current; current = NEXT)
112 int start = current->startproject;
113 int end = start + current->length;
114 if(start > input_position && start < input_end)
116 result = start - input_position;
120 if(end > input_position && end < input_end)
122 result = end - input_position;
130 void PluginSet::synchronize_params(PluginSet *plugin_set)
132 for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
133 this_plugin && that_plugin;
134 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
136 this_plugin->synchronize_params(that_plugin);
140 Plugin* PluginSet::insert_plugin(const char *title,
141 int64_t unit_position, int64_t unit_length, int plugin_type,
142 SharedLocation *shared_location, KeyFrame *default_keyframe,
145 Plugin *plugin = (Plugin*)create_silence(unit_position, unit_position + unit_length);
146 plugin->init(title, unit_position, unit_length, plugin_type,
147 shared_location, default_keyframe);
148 // May delete the plugin we just added so not desirable while loading.
149 if( do_optimize ) optimize();
153 Edit* PluginSet::create_edit()
155 Plugin* result = new Plugin(edl, this, "");
159 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
161 Plugin *current = new Plugin(edl, this, "");
162 List<Edit>::insert_after(previous_edit, current);
163 return (Edit*)current;
166 KeyFrame *PluginSet::nearest_keyframe(int64_t pos, int dir)
168 Plugin *plugin = (Plugin*)editof(pos, dir, 0);
169 if( !plugin ) return 0;
170 KeyFrame *keyframe = (KeyFrame *)(dir == PLAY_FORWARD ?
171 plugin->keyframes->nearest_after(pos) :
172 plugin->keyframes->nearest_before(pos));
176 int PluginSet::get_number()
178 return track->plugin_set.number_of(this);
181 void PluginSet::clear(int64_t start, int64_t end, int edit_autos)
186 for(Plugin *current = (Plugin*)first;
188 current = (Plugin*)NEXT)
190 current->keyframes->clear(start, end, 1);
195 Edits::clear(start, end);
198 //void PluginSet::clear_recursive(int64_t start, int64_t end)
200 //printf("PluginSet::clear_recursive 1\n");
201 // clear(start, end, 1);
204 void PluginSet::shift_keyframes_recursive(int64_t position, int64_t length)
206 // Plugin keyframes are shifted in shift_effects
209 void PluginSet::shift_effects_recursive(int64_t position, int64_t length, int edit_autos)
211 // Effects are shifted in length extension
215 void PluginSet::clear_keyframes(int64_t start, int64_t end)
217 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
219 current->clear_keyframes(start, end);
223 void PluginSet::copy_keyframes(int64_t start,
229 file->tag.set_title("PLUGINSET");
231 file->append_newline();
233 for(Plugin *current = (Plugin*)first;
235 current = (Plugin*)NEXT)
237 current->copy_keyframes(start, end, file, default_only, active_only);
240 file->tag.set_title("/PLUGINSET");
242 file->append_newline();
246 void PluginSet::paste_keyframes(int64_t start,
253 int first_keyframe = 1;
259 result = file->read_tag();
263 if(file->tag.title_is("/PLUGINSET"))
266 if(file->tag.title_is("KEYFRAME"))
268 int64_t position = file->tag.get_property("POSITION", 0);
269 if(first_keyframe && default_only)
278 // Get plugin owning keyframe
279 for(current = (Plugin*)last;
281 current = (Plugin*)PREVIOUS)
283 // We want keyframes to exist beyond the end of the last plugin to
284 // make editing intuitive, but it will always be possible to
285 // paste keyframes from one plugin into an incompatible plugin.
286 if(position >= current->startproject)
288 KeyFrame *keyframe = 0;
289 if(file->tag.get_property("DEFAULT", 0) || default_only)
291 keyframe = (KeyFrame*)current->keyframes->default_auto;
297 (KeyFrame*)current->keyframes->insert_auto(position);
302 keyframe->load(file);
303 keyframe->position = position;
315 void PluginSet::shift_effects(int64_t start, int64_t length, int edit_autos)
317 for(Plugin *current = (Plugin*)first;
319 current = (Plugin*)NEXT)
321 // Shift beginning of this effect
322 if(current->startproject >= start)
324 current->startproject += length;
327 // Extend end of this effect.
328 // In loading new files, the effect should extend to fill the entire track.
329 // In muting, the effect must extend to fill the gap if another effect follows.
330 // The user should use Settings->edit effects to disable this.
331 if(current->startproject + current->length >= start)
333 current->length += length;
336 // Shift keyframes in this effect.
337 // If the default keyframe lands on the starting point, it must be shifted
338 // since the effect start is shifted.
339 if(edit_autos && current->keyframes->default_auto->position >= start)
340 current->keyframes->default_auto->position += length;
342 if(edit_autos) current->keyframes->paste_silence(start, start + length);
346 void PluginSet::paste_silence(int64_t start, int64_t end, int edit_autos)
348 Plugin *new_plugin = (Plugin *) insert_new_edit(start);
349 int64_t length = end - start;
350 new_plugin->length = length;
351 while( (new_plugin=(Plugin *)new_plugin->next) != 0 ) {
352 new_plugin->startproject += length;
353 if( !edit_autos ) continue;
354 new_plugin->keyframes->default_auto->position += length;
355 new_plugin->keyframes->paste_silence(start, end);
359 void PluginSet::copy(int64_t start, int64_t end, FileXML *file)
361 file->tag.set_title("PLUGINSET");
362 file->tag.set_property("RECORD", record);
364 file->append_newline();
366 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
368 current->copy(start, end, file);
371 file->tag.set_title("/PLUGINSET");
373 file->append_newline();
376 void PluginSet::save(FileXML *file)
378 copy(0, length(), file);
381 void PluginSet::load(FileXML *file, uint32_t load_flags)
384 // Current plugin being amended
385 Plugin *plugin = (Plugin*)first;
386 int64_t startproject = 0;
388 record = file->tag.get_property("RECORD", record);
390 result = file->read_tag();
395 if(file->tag.title_is("/PLUGINSET"))
400 if(file->tag.title_is("PLUGIN"))
402 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
403 int plugin_type = file->tag.get_property("TYPE", 1);
404 char title[BCTEXTLEN];
406 file->tag.get_property("TITLE", title);
407 Plugin::fix_plugin_title(title);
408 SharedLocation shared_location;
409 shared_location.load(file);
412 if(load_flags & LOAD_EDITS)
414 plugin = insert_plugin(title,
422 startproject += length;
425 if(load_flags & LOAD_AUTOMATION)
430 plugin = (Plugin*)plugin->next;
440 int PluginSet::optimize()
443 Plugin *current_edit;
446 // Delete keyframes out of range
447 for(current_edit = (Plugin*)first;
449 current_edit = (Plugin*)current_edit->next)
451 current_edit->keyframes->default_auto->position = 0;
452 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
455 KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
456 if(current_keyframe->position >
457 current_edit->startproject + current_edit->length ||
458 current_keyframe->position < current_edit->startproject)
460 delete current_keyframe;
462 current_keyframe = previous_keyframe;
466 // Insert silence between plugins
467 for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
469 if(current->previous)
471 Plugin *previous = (Plugin*)PREVIOUS;
473 if(current->startproject -
474 previous->startproject -
475 previous->length > 0)
477 Plugin *new_plugin = (Plugin*)create_edit();
478 insert_before(current, new_plugin);
479 new_plugin->startproject = previous->startproject +
481 new_plugin->length = current->startproject -
482 previous->startproject -
487 if(current->startproject > 0)
489 Plugin *new_plugin = (Plugin*)create_edit();
490 insert_before(current, new_plugin);
491 new_plugin->length = current->startproject;
496 // delete 0 length plugins
501 for(current_edit = (Plugin*)first; !result && current_edit; ) {
502 Plugin* next = (Plugin*)current_edit->next;
503 if(current_edit->length == 0) {
511 // merge identical plugins with same keyframes
512 for( current_edit = (Plugin*)first;
513 !result && current_edit && current_edit->next; ) {
514 Plugin *next_edit = (Plugin*)current_edit->next;
517 if(next_edit->identical(current_edit)) {
518 current_edit->length += next_edit->length;
520 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
522 source = (KeyFrame*)source->next) {
523 KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
524 dest->copy_from(source);
525 current_edit->keyframes->append(dest);
532 current_edit = next_edit;
534 // delete last edit if 0 length or silence
535 if( last && (last->silence() || !last->length) ) {
549 void PluginSet::dump(FILE *fp)
551 fprintf(fp," PLUGIN_SET:\n");
552 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)