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()
50 int TimeStretchRTConfig::equivalent(TimeStretchRTConfig &src)
52 return fabs(scale - src.scale) < 0.0001 && size == src.size;
55 void TimeStretchRTConfig::copy_from(TimeStretchRTConfig &src)
57 this->scale = src.scale;
58 this->size = src.size;
61 void TimeStretchRTConfig::interpolate(TimeStretchRTConfig &prev,
62 TimeStretchRTConfig &next,
65 int64_t current_frame)
67 this->scale = prev.scale;
68 this->size = prev.size;
72 void TimeStretchRTConfig::boundaries()
74 if(fabs(scale) < 0.01) scale = 0.01;
75 if(fabs(scale) > 100) scale = 100;
76 if(size < 10) size = 10;
77 if(size > 1000) size = 1000;
83 TimeStretchRTWindow::TimeStretchRTWindow(TimeStretchRT *plugin)
84 : PluginClientWindow(plugin,
91 this->plugin = plugin;
94 TimeStretchRTWindow::~TimeStretchRTWindow()
98 void TimeStretchRTWindow::create_objects()
103 add_subwindow(title = new BC_Title(x, y, _("Fraction of original speed:")));
104 y += title->get_h() + plugin->get_theme()->widget_border;
105 scale = new TimeStretchRTScale(this,
109 scale->create_objects();
111 y += scale->get_h() + plugin->get_theme()->widget_border;
112 add_subwindow(title = new BC_Title(x, y, _("Window size (ms):")));
113 y += title->get_h() + plugin->get_theme()->widget_border;
114 size = new TimeStretchRTSize(this,
118 size->create_objects();
128 TimeStretchRTScale::TimeStretchRTScale(TimeStretchRTWindow *window,
129 TimeStretchRT *plugin,
132 : BC_TumbleTextBox(window,
133 (float)(1.0 / plugin->config.scale),
140 this->plugin = plugin;
144 int TimeStretchRTScale::handle_event()
146 plugin->config.scale = 1.0 / atof(get_text());
147 plugin->send_configure_change();
154 TimeStretchRTSize::TimeStretchRTSize(TimeStretchRTWindow *window,
155 TimeStretchRT *plugin,
158 : BC_TumbleTextBox(window,
166 this->plugin = plugin;
170 int TimeStretchRTSize::handle_event()
172 plugin->config.size = atoi(get_text());
173 plugin->send_configure_change();
184 TimeStretchRT::TimeStretchRT(PluginServer *server)
185 : PluginAClient(server)
187 need_reconfigure = 1;
194 TimeStretchRT::~TimeStretchRT()
199 const char* TimeStretchRT::plugin_title() { return _("Time Stretch RT"); }
200 int TimeStretchRT::is_realtime() { return 1; }
201 int TimeStretchRT::is_synthesis() { return 1; }
204 NEW_WINDOW_MACRO(TimeStretchRT, TimeStretchRTWindow)
206 LOAD_CONFIGURATION_MACRO(TimeStretchRT, TimeStretchRTConfig)
209 int TimeStretchRT::process_buffer(int64_t size,
211 int64_t start_position,
214 need_reconfigure = load_configuration();
216 if(!engine) engine = new TimeStretchEngine(config.scale,
220 if(start_position != dest_start) need_reconfigure = 1;
221 dest_start = start_position;
223 // Get start position of the input.
224 // Sample 0 is the keyframe position
225 //printf("TimeStretchRT::process_buffer %d %f\n", __LINE__, config.scale);
228 int64_t prev_position = edl_to_local(
230 get_source_position())->position);
232 if(prev_position == 0)
234 prev_position = get_source_start();
237 source_start = (int64_t)((start_position - prev_position) /
238 config.scale) + prev_position;
241 engine->update(config.scale, sample_rate, config.size);
242 need_reconfigure = 0;
243 //printf("TimeStretchRT::process_buffer %d start_position=" _LD
244 // " prev_position=%jd scale=%f source_start=%jd\n",
245 // __LINE__, start_position, prev_position, config.scale, source_start);
248 // process buffers until output length is reached
250 while(!error && engine->get_output_size() < size)
253 // printf("TimeStretchRT::process_buffer %d buffer=%p size=%lld source_start=%lld\n",
265 if(get_direction() == PLAY_FORWARD)
266 source_start += size;
268 source_start -= size;
270 //printf("TimeStretchRT::process_buffer %d size=%d\n", __LINE__, size);
271 engine->process(buffer, size);
272 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
275 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
278 engine->read_output(buffer, size);
280 //printf("TimeStretchRT::process_buffer %d\n", __LINE__);
281 if(get_direction() == PLAY_FORWARD)
293 void TimeStretchRT::save_data(KeyFrame *keyframe)
297 // cause data to be stored directly in text
298 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
299 output.tag.set_title("TIMESTRETCHRT");
300 output.tag.set_property("SCALE", config.scale);
301 output.tag.set_property("SIZE", config.size);
303 output.tag.set_title("/TIMESTRETCHRT");
305 output.append_newline();
306 output.terminate_string();
309 void TimeStretchRT::read_data(KeyFrame *keyframe)
313 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
315 while(!input.read_tag())
317 if(input.tag.title_is("TIMESTRETCHRT"))
319 config.scale = input.tag.get_property("SCALE", config.scale);
320 config.size = input.tag.get_property("SIZE", config.size);
325 void TimeStretchRT::update_gui()
329 if(load_configuration())
332 thread->window->lock_window("TimeStretchRT::update_gui");
333 ((TimeStretchRTWindow*)thread->window)->scale->update((float)(1.0 / config.scale));
334 ((TimeStretchRTWindow*)thread->window)->size->update((int64_t)config.size);
335 thread->window->unlock_window();