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"
27 #include "pluginaclient.h"
29 #include "transportque.h"
35 class ReverseAudioConfig
43 class ReverseAudioEnabled : public BC_CheckBox
46 ReverseAudioEnabled(ReverseAudio *plugin,
53 class ReverseAudioWindow : public PluginClientWindow
56 ReverseAudioWindow(ReverseAudio *plugin);
57 ~ReverseAudioWindow();
58 void create_objects();
61 ReverseAudioEnabled *enabled;
66 class ReverseAudio : public PluginAClient
69 ReverseAudio(PluginServer *server);
72 PLUGIN_CLASS_MEMBERS(ReverseAudioConfig)
74 void save_data(KeyFrame *keyframe);
75 void read_data(KeyFrame *keyframe);
78 int process_buffer(int64_t size,
80 int64_t start_position,
83 int64_t input_position;
93 REGISTER_PLUGIN(ReverseAudio);
97 ReverseAudioConfig::ReverseAudioConfig()
106 ReverseAudioWindow::ReverseAudioWindow(ReverseAudio *plugin)
107 : PluginClientWindow(plugin, 265, 60, 265, 60, 0)
109 this->plugin = plugin;
112 ReverseAudioWindow::~ReverseAudioWindow()
116 void ReverseAudioWindow::create_objects()
120 add_subwindow(enabled = new ReverseAudioEnabled(plugin,
132 ReverseAudioEnabled::ReverseAudioEnabled(ReverseAudio *plugin,
137 plugin->config.enabled,
140 this->plugin = plugin;
143 int ReverseAudioEnabled::handle_event()
145 plugin->config.enabled = get_value();
146 plugin->send_configure_change();
158 ReverseAudio::ReverseAudio(PluginServer *server)
159 : PluginAClient(server)
165 ReverseAudio::~ReverseAudio()
170 const char* ReverseAudio::plugin_title() { return _("Reverse audio"); }
171 int ReverseAudio::is_realtime() { return 1; }
174 NEW_WINDOW_MACRO(ReverseAudio, ReverseAudioWindow)
177 int ReverseAudio::process_buffer(int64_t size,
179 int64_t start_position,
182 for(int i = 0; i < size; i += fragment_size)
184 fragment_size = size - i;
185 load_configuration();
188 int offset = buffer->get_offset();
189 buffer->set_offset(offset + i);
195 buffer->set_offset(offset);
196 double *buffer_samples = buffer->get_data();
198 for(int start = i, end = i + fragment_size - 1;
202 double temp = buffer_samples[start];
203 buffer_samples[start] = buffer_samples[end];
204 buffer_samples[end] = temp;
209 int offset = buffer->get_offset();
210 buffer->set_offset(offset + i);
216 buffer->set_offset(offset);
219 if(get_direction() == PLAY_FORWARD)
220 start_position += fragment_size;
222 start_position -= fragment_size;
232 int ReverseAudio::load_configuration()
234 KeyFrame *prev_keyframe, *next_keyframe;
235 next_keyframe = get_next_keyframe(get_source_position());
236 prev_keyframe = get_prev_keyframe(get_source_position());
237 read_data(next_keyframe);
238 // Previous keyframe stays in config object.
239 read_data(prev_keyframe);
241 int64_t prev_position = edl_to_local(prev_keyframe->position);
242 int64_t next_position = edl_to_local(next_keyframe->position);
244 // printf("ReverseAudio::load_configuration 1 %lld %lld %lld %lld\n",
247 // prev_keyframe->position,
248 // next_keyframe->position);
249 // Defeat default keyframe
250 if(prev_position == 0 && next_position == 0)
252 next_position = prev_position = get_source_start();
255 // Get range to flip in requested rate
256 int64_t range_start = prev_position;
257 int64_t range_end = next_position;
259 // Between keyframe and edge of range or no keyframes
260 if(range_start == range_end)
262 // Between first keyframe and start of effect
263 if(get_source_position() >= get_source_start() &&
264 get_source_position() < range_start)
266 range_start = get_source_start();
269 // Between last keyframe and end of effect
270 if(get_source_position() >= range_start &&
271 get_source_position() < get_source_start() + get_total_len())
273 range_end = get_source_start() + get_total_len();
277 // Should never get here
282 // Convert start position to new direction
283 if(get_direction() == PLAY_FORWARD)
285 // Truncate next buffer to keyframe
286 if(range_end - get_source_position() < fragment_size)
287 fragment_size = range_end - get_source_position();
288 input_position = get_source_position() - range_start;
289 input_position = range_end - input_position - fragment_size;
293 if(get_source_position() - range_start < fragment_size)
294 fragment_size = get_source_position() - range_start;
295 input_position = range_end - get_source_position();
296 input_position = range_start + input_position + fragment_size;
298 // printf("ReverseAudio::load_configuration 20 start=%lld end=%lld current=%lld input=%lld\n",
301 // get_source_position(),
308 void ReverseAudio::save_data(KeyFrame *keyframe)
312 // cause data to be stored directly in text
313 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
314 output.tag.set_title("REVERSEAUDIO");
315 output.tag.set_property("ENABLED", config.enabled);
317 output.tag.set_title("/REVERSEAUDIO");
319 output.append_newline();
320 output.terminate_string();
323 void ReverseAudio::read_data(KeyFrame *keyframe)
327 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
329 while(!input.read_tag())
331 if(input.tag.title_is("REVERSEAUDIO"))
333 config.enabled = input.tag.get_property("ENABLED", config.enabled);
338 void ReverseAudio::update_gui()
342 load_configuration();
343 thread->window->lock_window();
344 ((ReverseAudioWindow*)thread->window)->enabled->update(config.enabled);
345 thread->window->unlock_window();