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"
34 PluginSet::PluginSet(EDL *edl, Track *track)
40 PluginSet::~PluginSet()
42 while(last) delete last;
46 PluginSet& PluginSet::operator=(PluginSet& plugins)
48 printf("PluginSet::operator= 1\n");
53 void PluginSet::copy_from(PluginSet *src)
55 while(last) delete last;
56 for(Plugin *current = (Plugin*)src->first; current; current = (Plugin*)NEXT)
59 append(new_plugin = (Plugin*)create_edit());
60 new_plugin->copy_from(current);
62 this->record = src->record;
65 Plugin* PluginSet::get_first_plugin()
67 // Called when a new pluginset is added.
68 // Get first non-silence plugin in the plugin set.
69 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
71 if(current && current->plugin_type != PLUGIN_NONE)
79 int64_t PluginSet::plugin_change_duration(int64_t input_position,
83 int result = input_length;
88 int input_start = input_position - input_length;
89 for(current = last; current; current = PREVIOUS)
91 int start = current->startproject;
92 int end = start + current->length;
93 if(end > input_start && end < input_position)
95 result = input_position - end;
99 if(start > input_start && start < input_position)
101 result = input_position - start;
108 int input_end = input_position + input_length;
109 for(current = first; current; current = NEXT)
111 int start = current->startproject;
112 int end = start + current->length;
113 if(start > input_position && start < input_end)
115 result = start - input_position;
119 if(end > input_position && end < input_end)
121 result = end - input_position;
129 void PluginSet::synchronize_params(PluginSet *plugin_set)
131 for(Plugin *this_plugin = (Plugin*)first, *that_plugin = (Plugin*)plugin_set->first;
132 this_plugin && that_plugin;
133 this_plugin = (Plugin*)this_plugin->next, that_plugin = (Plugin*)that_plugin->next)
135 this_plugin->synchronize_params(that_plugin);
139 Plugin* PluginSet::insert_plugin(const char *title,
140 int64_t unit_position,
143 SharedLocation *shared_location,
144 KeyFrame *default_keyframe,
147 Plugin *plugin = (Plugin*)create_silence(unit_position, unit_position + unit_length);
148 if(title) strcpy(plugin->title, title);
149 if(shared_location) plugin->shared_location = *shared_location;
150 plugin->plugin_type = plugin_type;
153 *plugin->keyframes->default_auto = *default_keyframe;
154 plugin->keyframes->default_auto->position = unit_position;
156 // May delete the plugin we just added so not desirable while loading.
157 if(do_optimize) optimize();
161 Edit* PluginSet::create_edit()
163 Plugin* result = new Plugin(edl, this, "");
167 Edit* PluginSet::insert_edit_after(Edit *previous_edit)
169 Plugin *current = new Plugin(edl, this, "");
170 List<Edit>::insert_after(previous_edit, current);
171 return (Edit*)current;
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::copy(int64_t start, int64_t end, FileXML *file)
347 file->tag.set_title("PLUGINSET");
348 file->tag.set_property("RECORD", record);
350 file->append_newline();
352 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)
354 current->copy(start, end, file);
357 file->tag.set_title("/PLUGINSET");
359 file->append_newline();
362 void PluginSet::save(FileXML *file)
364 copy(0, length(), file);
367 void PluginSet::load(FileXML *file, uint32_t load_flags)
370 // Current plugin being amended
371 Plugin *plugin = (Plugin*)first;
372 int64_t startproject = 0;
374 record = file->tag.get_property("RECORD", record);
376 result = file->read_tag();
381 if(file->tag.title_is("/PLUGINSET"))
386 if(file->tag.title_is("PLUGIN"))
388 int64_t length = file->tag.get_property("LENGTH", (int64_t)0);
389 int plugin_type = file->tag.get_property("TYPE", 1);
390 char title[BCTEXTLEN];
392 file->tag.get_property("TITLE", title);
393 SharedLocation shared_location;
394 shared_location.load(file);
397 if(load_flags & LOAD_EDITS)
399 plugin = insert_plugin(title,
407 startproject += length;
410 if(load_flags & LOAD_AUTOMATION)
415 plugin = (Plugin*)plugin->next;
425 int PluginSet::optimize()
428 Plugin *current_edit;
431 // Delete keyframes out of range
432 for(current_edit = (Plugin*)first;
434 current_edit = (Plugin*)current_edit->next)
436 current_edit->keyframes->default_auto->position = 0;
437 for(KeyFrame *current_keyframe = (KeyFrame*)current_edit->keyframes->last;
440 KeyFrame *previous_keyframe = (KeyFrame*)current_keyframe->previous;
441 if(current_keyframe->position >
442 current_edit->startproject + current_edit->length ||
443 current_keyframe->position < current_edit->startproject)
445 delete current_keyframe;
447 current_keyframe = previous_keyframe;
451 // Insert silence between plugins
452 for(Plugin *current = (Plugin*)last; current; current = (Plugin*)PREVIOUS)
454 if(current->previous)
456 Plugin *previous = (Plugin*)PREVIOUS;
458 if(current->startproject -
459 previous->startproject -
460 previous->length > 0)
462 Plugin *new_plugin = (Plugin*)create_edit();
463 insert_before(current, new_plugin);
464 new_plugin->startproject = previous->startproject +
466 new_plugin->length = current->startproject -
467 previous->startproject -
472 if(current->startproject > 0)
474 Plugin *new_plugin = (Plugin*)create_edit();
475 insert_before(current, new_plugin);
476 new_plugin->length = current->startproject;
481 // delete 0 length plugins
486 for(current_edit = (Plugin*)first; !result && current_edit; ) {
487 Plugin* next = (Plugin*)current_edit->next;
488 if(current_edit->length == 0) {
496 // merge identical plugins with same keyframes
497 for( current_edit = (Plugin*)first;
498 !result && current_edit && current_edit->next; ) {
499 Plugin *next_edit = (Plugin*)current_edit->next;
502 if(next_edit->identical(current_edit)) {
503 current_edit->length += next_edit->length;
505 for(KeyFrame *source = (KeyFrame*)next_edit->keyframes->first;
507 source = (KeyFrame*)source->next) {
508 KeyFrame *dest = new KeyFrame(edl, current_edit->keyframes);
510 current_edit->keyframes->append(dest);
517 current_edit = next_edit;
519 // delete last edit if 0 length or silence
520 if( last && (last->silence() || !last->length) ) {
534 void PluginSet::dump(FILE *fp)
536 fprintf(fp," PLUGIN_SET:\n");
537 for(Plugin *current = (Plugin*)first; current; current = (Plugin*)NEXT)