4 * Copyright (C) 2011 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 "bcdisplayinfo.h"
27 #include "../timestretch/timestretchengine.h"
28 #include "timestretchrt.h"
29 #include "transportque.h"
39 REGISTER_PLUGIN(TimeStretchRT);
43 TimeStretchRTConfig::TimeStretchRTConfig()
52 int TimeStretchRTConfig::equivalent(TimeStretchRTConfig &src)
54 // return fabs(scale - src.scale) < 0.0001 && size == src.size;
55 return fabs(num - src.num) < 0.0001 &&
56 fabs(denom - src.denom) < 0.0001 &&
60 void TimeStretchRTConfig::copy_from(TimeStretchRTConfig &src)
63 this->denom = src.denom;
64 // this->scale = src.scale;
65 this->size = src.size;
68 void TimeStretchRTConfig::interpolate(TimeStretchRTConfig &prev,
69 TimeStretchRTConfig &next,
72 int64_t current_frame)
75 this->denom = prev.denom;
76 // this->scale = prev.scale;
77 this->size = prev.size;
81 void TimeStretchRTConfig::boundaries()
83 if(num < 0.0001) num = 0.0001;
84 if(denom < 0.0001) denom = 0.0001;
85 // if(fabs(scale) < 0.01) scale = 0.01;
86 // if(fabs(scale) > 100) scale = 100;
87 if(size < 10) size = 10;
88 if(size > 1000) size = 1000;
94 TimeStretchRTWindow::TimeStretchRTWindow(TimeStretchRT *plugin)
95 : PluginClientWindow(plugin,
102 this->plugin = plugin;
105 TimeStretchRTWindow::~TimeStretchRTWindow()
109 void TimeStretchRTWindow::create_objects()
112 int margin = plugin->get_theme()->widget_border;
115 add_subwindow(title = new BC_Title(x, y, _("Input samples:")));
116 y += title->get_h() + plugin->get_theme()->widget_border;
117 num = new TimeStretchRTScale(this,
121 &plugin->config.num);
122 num->create_objects();
123 y += num->get_h() + margin;
125 add_subwindow(title = new BC_Title(x, y, _("Output samples:")));
126 y += title->get_h() + plugin->get_theme()->widget_border;
127 denom = new TimeStretchRTScale(this,
131 &plugin->config.denom);
132 denom->create_objects();
134 y += denom->get_h() + margin;
135 add_subwindow(title = new BC_Title(x, y, _("Window size (ms):")));
136 y += title->get_h() + margin;
137 size = new TimeStretchRTSize(this,
141 size->create_objects();
151 TimeStretchRTScale::TimeStretchRTScale(TimeStretchRTWindow *window,
152 TimeStretchRT *plugin,
156 : BC_TumbleTextBox(window,
164 this->plugin = plugin;
169 int TimeStretchRTScale::handle_event()
171 *value = atof(get_text());
172 plugin->config.boundaries();
173 plugin->send_configure_change();
180 TimeStretchRTSize::TimeStretchRTSize(TimeStretchRTWindow *window,
181 TimeStretchRT *plugin,
184 : BC_TumbleTextBox(window,
192 this->plugin = plugin;
196 int TimeStretchRTSize::handle_event()
198 plugin->config.size = atoi(get_text());
199 plugin->send_configure_change();
210 TimeStretchRT::TimeStretchRT(PluginServer *server)
211 : PluginAClient(server)
213 need_reconfigure = 1;
220 TimeStretchRT::~TimeStretchRT()
225 const char* TimeStretchRT::plugin_title() { return N_("Time Stretch RT"); }
226 int TimeStretchRT::is_realtime() { return 1; }
227 int TimeStretchRT::is_synthesis() { return 1; }
230 NEW_WINDOW_MACRO(TimeStretchRT, TimeStretchRTWindow)
232 LOAD_CONFIGURATION_MACRO(TimeStretchRT, TimeStretchRTConfig)
235 int TimeStretchRT::process_buffer(int64_t size,
237 int64_t start_position,
240 need_reconfigure = load_configuration();
242 if(!engine) engine = new TimeStretchEngine(config.denom / config.num,
246 if(start_position != dest_start) need_reconfigure = 1;
247 dest_start = start_position;
249 // Get start position of the input.
250 // Sample 0 is the keyframe position
253 int64_t prev_position = edl_to_local(
255 get_source_position())->position);
257 if(prev_position == 0)
259 prev_position = get_source_start();
262 source_start = (int64_t)((start_position - prev_position) *
263 config.num / config.denom) + prev_position;
266 engine->update(config.denom / config.num, sample_rate, config.size);
267 need_reconfigure = 0;
268 // printf("TimeStretchRT::process_buffer %d start_position=%lld prev_position=%lld scale=%f source_start=%lld\n",
276 // process buffers until output length is reached
278 while(!error && engine->get_output_size() < size)
281 // printf("TimeStretchRT::process_buffer %d buffer=%p size=%lld source_start=%lld\n",
293 if(get_direction() == PLAY_FORWARD)
294 source_start += size;
296 source_start -= size;
298 //printf("TimeStretchRT::process_buffer %d size=%d\n", __LINE__, size);
299 engine->process(buffer, size);
300 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
303 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
306 engine->read_output(buffer, size);
308 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
309 if(get_direction() == PLAY_FORWARD)
321 void TimeStretchRT::save_data(KeyFrame *keyframe)
325 // cause data to be stored directly in text
326 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
327 output.tag.set_title("TIMESTRETCHRT");
328 output.tag.set_property("NUM", config.num);
329 output.tag.set_property("DENOM", config.denom);
330 output.tag.set_property("SIZE", config.size);
332 output.tag.set_title("/TIMESTRETCHRT");
334 output.append_newline();
335 output.terminate_string();
338 void TimeStretchRT::read_data(KeyFrame *keyframe)
342 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
344 while(!input.read_tag())
346 if(input.tag.title_is("TIMESTRETCHRT"))
348 config.num = input.tag.get_property("NUM", config.num);
349 config.denom = input.tag.get_property("DENOM", config.denom);
350 config.size = input.tag.get_property("SIZE", config.size);
355 void TimeStretchRT::update_gui()
359 if(load_configuration())
362 thread->window->lock_window("TimeStretchRT::update_gui");
363 ((TimeStretchRTWindow*)thread->window)->num->update((float)config.num);
364 ((TimeStretchRTWindow*)thread->window)->denom->update((float)config.denom);
365 ((TimeStretchRTWindow*)thread->window)->size->update((int64_t)config.size);
366 thread->window->unlock_window();