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
22 #include "bcsignals.h"
24 #include "edlsession.h"
27 #include "keyframes.h"
28 #include "localsession.h"
32 #include "pluginpopup.h"
33 #include "pluginset.h"
34 #include "pluginserver.h"
37 #include "virtualnode.h"
40 Plugin::Plugin(EDL *edl, Track *track, const char *title)
46 strcpy(this->title, title);
47 plugin_type = PLUGIN_NONE;
53 keyframes = new KeyFrames(edl, track);
54 keyframes->create_objects();
58 Plugin::Plugin(EDL *edl, PluginSet *plugin_set, const char *title)
59 : Edit(edl, plugin_set)
62 this->track = plugin_set->track;
63 this->plugin_set = plugin_set;
64 strcpy(this->title, title);
65 plugin_type = PLUGIN_NONE;
71 keyframes = new KeyFrames(edl, track);
72 keyframes->create_objects();
77 while(keyframes->last) delete keyframes->last;
83 return plugin_type == PLUGIN_NONE ? 1 : 0;
86 void Plugin::clear_keyframes(int64_t start, int64_t end)
88 keyframes->clear(start, end, 0);
92 void Plugin::init(const char *title,
93 int64_t unit_position, int64_t unit_length, int plugin_type,
94 SharedLocation *shared_location, KeyFrame *default_keyframe)
96 if( title ) strcpy(this->title, title);
97 if( shared_location ) this->shared_location = *shared_location;
98 this->plugin_type = plugin_type;
99 if( default_keyframe )
100 *this->keyframes->default_auto = *default_keyframe;
101 this->keyframes->default_auto->position = unit_position;
102 this->startproject = unit_position;
103 this->length = unit_length;
106 void Plugin::copy_base(Edit *edit)
108 Plugin *plugin = (Plugin*)edit;
110 this->startsource = edit->startsource;
111 this->startproject = edit->startproject;
112 this->length = edit->length;
113 this->orig_id = edit->orig_id;
115 this->plugin_type = plugin->plugin_type;
116 this->in = plugin->in;
117 this->out = plugin->out;
118 this->show = plugin->show;
119 this->on = plugin->on;
120 // dont copy gui_id, it will be a duplicate ref
121 // Should reconfigure this based on where the first track is now.
122 this->shared_location = plugin->shared_location;
123 strcpy(this->title, plugin->title);
126 void Plugin::copy_from(Edit *edit)
129 copy_keyframes((Plugin*)edit);
132 void Plugin::copy_keyframes(Plugin *plugin)
135 keyframes->copy_from(plugin->keyframes);
138 void Plugin::copy_keyframes(int64_t start,
144 // Only 1 default is copied from where the start position is
145 int64_t endproject = startproject + length;
148 start < endproject &&
149 start >= startproject))
150 keyframes->copy(start, end, file, default_only, active_only);
153 void Plugin::synchronize_params(Edit *edit)
155 Plugin *plugin = (Plugin*)edit;
156 this->in = plugin->in;
157 this->out = plugin->out;
158 this->show = plugin->show;
159 this->gui_id = plugin->gui_id;
160 this->on = plugin->on;
161 strcpy(this->title, plugin->title);
162 copy_keyframes(plugin);
165 void Plugin::shift_keyframes(int64_t position)
167 for(KeyFrame *keyframe = (KeyFrame*)keyframes->first;
169 keyframe = (KeyFrame*)keyframe->next)
171 keyframe->position += position;
176 void Plugin::equivalent_output(Edit *edit, int64_t *result)
178 Plugin *plugin = (Plugin*)edit;
179 // End of plugin changed
180 if(startproject + length != plugin->startproject + plugin->length)
182 if(*result < 0 || startproject + length < *result)
183 *result = startproject + length;
186 // Start of plugin changed
187 if( startproject != plugin->startproject || plugin_type != plugin->plugin_type ||
188 on != plugin->on || !(shared_location == plugin->shared_location) ||
189 strcmp(title, plugin->title) ) {
190 if( *result < 0 || startproject < *result )
191 *result = startproject;
195 keyframes->equivalent_output(plugin->keyframes, startproject, result);
198 const char* Plugin::type_to_text(int type)
201 case PLUGIN_STANDALONE: return _("standalone");
202 case PLUGIN_SHAREDPLUGIN: return _("shared plugin");
203 case PLUGIN_SHAREDMODULE: return _("shared module");
208 int Plugin::is_synthesis(int64_t position, int direction, int depth)
211 printf("Plugin::is_synthesis %d: depth range limit, type=%s, title=%s\n",
212 __LINE__, type_to_text(plugin_type), title);
215 switch( plugin_type ) {
216 case PLUGIN_STANDALONE: {
218 printf("Plugin::is_synthesis track not defined\n");
221 PluginServer *plugin_server = MWindow::scan_plugindb(title, track->data_type);
222 return plugin_server->get_synthesis(); }
224 // Dereference real plugin and descend another level
225 case PLUGIN_SHAREDPLUGIN: {
226 int real_module_number = shared_location.module;
227 int real_plugin_number = shared_location.plugin;
228 Track *track = edl->tracks->number(real_module_number);
229 // Get shared plugin from master track
230 Plugin *plugin = track->get_current_plugin(position,
231 real_plugin_number, direction, 0, 0);
234 return plugin->is_synthesis(position, direction, depth+1);
237 // Dereference the real track and descend
238 case PLUGIN_SHAREDMODULE: {
239 int real_module_number = shared_location.module;
240 Track *track = edl->tracks->number(real_module_number);
241 return track->is_synthesis(position, direction, depth+1); }
248 int Plugin::identical(Plugin *that)
251 if(plugin_type != that->plugin_type) return 0;
253 // Test title or location
256 case PLUGIN_STANDALONE:
257 if(strcmp(title, that->title)) return 0;
259 case PLUGIN_SHAREDPLUGIN:
260 if(shared_location.module != that->shared_location.module ||
261 shared_location.plugin != that->shared_location.plugin) return 0;
263 case PLUGIN_SHAREDMODULE:
264 if(shared_location.module != that->shared_location.module) return 0;
268 // Test remaining fields
269 return (this->on == that->on &&
270 ((KeyFrame*)keyframes->default_auto)->identical(
271 ((KeyFrame*)that->keyframes->default_auto)));
274 int Plugin::keyframe_exists(KeyFrame *ptr)
276 for(KeyFrame *current = (KeyFrame*)keyframes->first;
278 current = (KeyFrame*)NEXT)
280 if(current == ptr) return 1;
286 void Plugin::change_plugin(char *title,
287 SharedLocation *shared_location,
290 strcpy(this->title, title);
291 this->shared_location = *shared_location;
292 this->plugin_type = plugin_type;
297 KeyFrame* Plugin::get_prev_keyframe(int64_t position,
300 return keyframes->get_prev_keyframe(position, direction);
303 KeyFrame* Plugin::get_next_keyframe(int64_t position,
308 // This doesn't work for playback because edl->selectionstart doesn't
309 // change during playback at the same rate as PluginClient::source_position.
312 //printf("Plugin::get_next_keyframe position < 0\n");
313 position = track->to_units(edl->local_session->get_selectionstart(1), 0);
316 // Get keyframe after current position
317 for(current = (KeyFrame*)keyframes->first;
319 current = (KeyFrame*)NEXT)
321 if(direction == PLAY_FORWARD && current->position > position) break;
323 if(direction == PLAY_REVERSE && current->position >= position) break;
326 // Nothing after current position
327 if(!current && keyframes->last)
329 current = (KeyFrame*)keyframes->last;
335 current = (KeyFrame*)keyframes->default_auto;
341 KeyFrame* Plugin::get_keyframe()
343 return keyframes->get_keyframe();
346 void Plugin::copy(int64_t start, int64_t end, FileXML *file)
348 int64_t endproject = startproject + length;
350 if((startproject >= start && startproject <= end) || // startproject in range
351 (endproject <= end && endproject >= start) || // endproject in range
352 (startproject <= start && endproject >= end)) // range in project
355 int64_t startproject_in_selection = startproject; // start of edit in selection in project
356 int64_t startsource_in_selection = startsource; // start of source in selection in source
357 //int64_t endsource_in_selection = startsource + length; // end of source in selection
358 int64_t length_in_selection = length; // length of edit in selection
360 if(startproject < start)
361 { // start is after start of edit in project
362 int64_t length_difference = start - startproject;
364 startsource_in_selection += length_difference;
365 startproject_in_selection += length_difference;
366 length_in_selection -= length_difference;
369 // end is before end of edit in project
372 length_in_selection = end - startproject_in_selection;
375 // Plugins don't store silence
376 file->tag.set_title("PLUGIN");
377 // file->tag.set_property("STARTPROJECT", startproject_in_selection - start);
378 file->tag.set_property("LENGTH", length_in_selection);
379 file->tag.set_property("TYPE", plugin_type);
380 file->tag.set_property("TITLE", title);
382 file->append_newline();
385 if(plugin_type == PLUGIN_SHAREDPLUGIN ||
386 plugin_type == PLUGIN_SHAREDMODULE)
388 shared_location.save(file);
395 file->tag.set_title("IN");
397 file->tag.set_title("/IN");
402 file->tag.set_title("OUT");
404 file->tag.set_title("/OUT");
409 file->tag.set_title("SHOW");
411 file->tag.set_title("/SHOW");
416 file->tag.set_title("ON");
418 file->tag.set_title("/ON");
421 file->append_newline();
424 keyframes->copy(start, end, file, 0, 0);
426 file->tag.set_title("/PLUGIN");
428 file->append_newline();
432 void Plugin::load(FileXML *file)
435 int first_keyframe = 1;
438 // Currently show is ignored when loading
441 while(keyframes->last) delete keyframes->last;
444 result = file->read_tag();
446 //printf("Plugin::load 1 %s\n", file->tag.get_title());
449 if(file->tag.title_is("/PLUGIN"))
454 if(file->tag.title_is("SHARED_LOCATION"))
456 shared_location.load(file);
459 if(file->tag.title_is("IN"))
464 if(file->tag.title_is("OUT"))
469 if(file->tag.title_is("SHOW"))
474 if(file->tag.title_is("ON"))
479 if(file->tag.title_is("KEYFRAME"))
484 keyframes->default_auto->load(file);
488 // Override default keyframe
490 KeyFrame *keyframe = (KeyFrame*)keyframes->append(new KeyFrame(edl, keyframes));
491 keyframe->position = file->tag.get_property("POSITION", (int64_t)0);
492 keyframe->load(file);
499 void Plugin::get_shared_location(SharedLocation *result)
501 if(plugin_type == PLUGIN_STANDALONE && plugin_set)
503 result->module = edl->tracks->number_of(track);
504 result->plugin = track->plugin_set.number_of(plugin_set);
508 *result = this->shared_location;
512 Track* Plugin::get_shared_track()
514 return edl->tracks->get_item_number(shared_location.module);
518 void Plugin::calculate_title(char *string, int use_nudge)
520 switch( plugin_type ) {
521 case PLUGIN_STANDALONE:
523 strcpy(string, _(title));
525 case PLUGIN_SHAREDPLUGIN:
526 case PLUGIN_SHAREDMODULE:
527 shared_location.calculate_title(string, edl,
528 startproject, 0, plugin_type, use_nudge);
533 void Plugin::fix_plugin_title(char *title)
535 MWindow::fix_plugin_title(title);
539 void Plugin::paste(FileXML *file)
541 length = file->tag.get_property("LENGTH", (int64_t)0);
544 void Plugin::resample(double old_rate, double new_rate)
546 // Resample keyframes in here
547 keyframes->resample(old_rate, new_rate);
550 void Plugin::shift(int64_t difference)
552 Edit::shift(difference);
554 if(edl->session->autos_follow_edits)
555 shift_keyframes(difference);
558 void Plugin::dump(FILE *fp)
560 fprintf(fp," PLUGIN: type=%d, id %d, orig_id %d, title=\"%s\" on=%d track=%d plugin=%d gui_id=%d\n",
561 plugin_type, id, orig_id, title, on, shared_location.module, shared_location.plugin, gui_id);
562 fprintf(fp," startproject %jd length %jd\n", startproject, length);