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 "bcdisplayinfo.h"
25 #include "delayaudio.h"
35 PluginClient* new_plugin(PluginServer *server)
37 return new DelayAudio(server);
41 DelayAudio::DelayAudio(PluginServer *server)
42 : PluginAClient(server)
47 DelayAudio::~DelayAudio()
51 if(buffer) delete buffer;
56 NEW_WINDOW_MACRO(DelayAudio, DelayAudioWindow)
58 const char* DelayAudio::plugin_title() { return N_("Delay audio"); }
59 int DelayAudio::is_realtime() { return 1; }
62 void DelayAudio::reset()
68 int DelayAudio::load_configuration()
70 KeyFrame *prev_keyframe;
71 prev_keyframe = get_prev_keyframe(get_source_position());
73 DelayAudioConfig old_config;
74 old_config.copy_from(config);
75 read_data(prev_keyframe);
77 if(!old_config.equivalent(config))
87 void DelayAudio::read_data(KeyFrame *keyframe)
90 input.set_shared_input(keyframe->xbuf);
95 result = input.read_tag();
99 if(input.tag.title_is("DELAYAUDIO"))
101 config.length = input.tag.get_property("LENGTH", (double)config.length);
108 void DelayAudio::save_data(KeyFrame *keyframe)
111 output.set_shared_output(keyframe->xbuf);
113 output.tag.set_title("DELAYAUDIO");
114 output.tag.set_property("LENGTH", (double)config.length);
116 output.tag.set_title("/DELAYAUDIO");
118 output.append_newline();
119 output.terminate_string();
122 void DelayAudio::reconfigure()
124 input_start = (int64_t)(config.length * PluginAClient::project_sample_rate + 0.5);
125 int64_t new_allocation = input_start + PluginClient::in_buffer_size;
126 Samples *new_buffer = new Samples(new_allocation);
127 bzero(new_buffer->get_data(), sizeof(double) * new_allocation);
129 // printf("DelayAudio::reconfigure %f %d %d %d\n",
131 // PluginAClient::project_sample_rate,
132 // PluginClient::in_buffer_size,
139 int size = MIN(new_allocation, allocation);
141 memcpy(new_buffer->get_data(),
143 (size - PluginClient::in_buffer_size) * sizeof(double));
147 allocation = new_allocation;
149 allocation = new_allocation;
150 need_reconfigure = 0;
153 int DelayAudio::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
156 load_configuration();
157 if(need_reconfigure) reconfigure();
159 // printf("DelayAudio::process_realtime %d %d\n",
160 // input_start, size);
164 double *buffer_samples = buffer->get_data();
165 double *output_samples = output_ptr->get_data();
166 double *input_samples = input_ptr->get_data();
167 memcpy(buffer_samples + input_start, input_samples, size * sizeof(double));
168 memcpy(output_samples, buffer_samples, size * sizeof(double));
170 for(int i = size, j = 0; i < allocation; i++, j++)
172 buffer_samples[j] = buffer_samples[i];
181 void DelayAudio::update_gui()
185 load_configuration();
186 ((DelayAudioWindow*)thread->window)->lock_window();
187 ((DelayAudioWindow*)thread->window)->update_gui();
188 ((DelayAudioWindow*)thread->window)->unlock_window();
207 DelayAudioWindow::DelayAudioWindow(DelayAudio *plugin)
208 : PluginClientWindow(plugin, 285, 80, 285, 80, 0)
210 this->plugin = plugin;
213 DelayAudioWindow::~DelayAudioWindow()
217 void DelayAudioWindow::create_objects()
219 add_subwindow(new BC_Title(10, 10, _("Delay seconds:")));
220 length = new DelayAudioTextBox(
225 length->create_objects();
230 void DelayAudioWindow::update_gui()
232 char string[BCTEXTLEN];
233 sprintf(string, "%.04f", plugin->config.length);
234 length->update(string);
248 DelayAudioTextBox::DelayAudioTextBox(
250 DelayAudioWindow *window,
253 : BC_TumbleTextBox(window,
254 (float)plugin->config.length,
261 this->plugin = plugin;
265 DelayAudioTextBox::~DelayAudioTextBox()
269 int DelayAudioTextBox::handle_event()
271 plugin->config.length = atof(get_text());
272 if(plugin->config.length < 0) plugin->config.length = 0;
273 plugin->send_configure_change();
284 DelayAudioConfig::DelayAudioConfig()
289 int DelayAudioConfig::equivalent(DelayAudioConfig &that)
291 return(EQUIV(this->length, that.length));
294 void DelayAudioConfig::copy_from(DelayAudioConfig &that)
296 this->length = that.length;