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
23 #include "audiodevice.h"
24 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
29 #include "edlsession.h"
34 #include "pluginaclient.h"
35 #include "pluginserver.h"
36 #include "recordconfig.h"
38 #include "transportque.inc"
45 class LiveAudioWindow;
59 class LiveAudioWindow : public PluginClientWindow
62 LiveAudioWindow(LiveAudio *plugin);
65 void create_objects();
75 class LiveAudio : public PluginAClient
78 LiveAudio(PluginServer *server);
82 PLUGIN_CLASS_MEMBERS(LiveAudioConfig);
84 int process_buffer(int64_t size,
86 int64_t start_position,
89 int is_multichannel();
91 void save_data(KeyFrame *keyframe);
92 void read_data(KeyFrame *keyframe);
100 int history_channels;
101 int64_t history_position;
103 // Fragment size from the EDL session
118 LiveAudioConfig::LiveAudioConfig()
129 LiveAudioWindow::LiveAudioWindow(LiveAudio *plugin)
130 : PluginClientWindow(plugin,
137 this->plugin = plugin;
140 LiveAudioWindow::~LiveAudioWindow()
144 void LiveAudioWindow::create_objects()
149 add_subwindow(title = new BC_Title(x, y, _("Live audio")));
175 REGISTER_PLUGIN(LiveAudio)
182 LiveAudio::LiveAudio(PluginServer *server)
183 : PluginAClient(server)
187 history_channels = 0;
189 history_position = 0;
196 LiveAudio::~LiveAudio()
200 adevice->interrupt_crash();
201 adevice->close_all();
206 for(int i = 0; i < history_channels; i++)
214 int LiveAudio::process_buffer(int64_t size,
216 int64_t start_position,
219 load_configuration();
221 // printf("LiveAudio::process_buffer 10 size=%lld buffer_size=%d channels=%d size=%d\n",
222 // size, get_buffer_size(), get_total_buffers(), size);
224 int first_buffer = 0;
228 EDLSession *session = get_edl()->session;
231 fragment_size = session->record_fragment_size;
232 history_channels = session->aconfig_in->channels;
234 adevice = new AudioDevice(server ? server->mwindow : 0);
235 // Take fragment size & channels from the recording config
236 adevice->open_input(session->aconfig_in,
238 get_project_samplerate(),
240 session->aconfig_in->channels,
241 session->real_time_record);
242 adevice->start_recording();
244 history_position = start_position;
248 if(!history || history[0]->get_allocated() < size)
250 // compute new history which is a multiple of our fragment size
251 int new_history_size = fragment_size;
252 while(new_history_size < size)
253 new_history_size += fragment_size;
258 history = new Samples*[history_channels];
259 for(int i = 0; i < history_channels; i++)
263 for(int i = 0; i < history_channels; i++)
266 history[i] = new Samples(new_history_size);
267 bzero(history[i]->get_data(), sizeof(double) * new_history_size);
273 // if(get_direction() == PLAY_FORWARD)
275 // Reset history buffer to current position if before maximum history
276 if(start_position < history_position - history[0]->get_allocated())
277 history_position = start_position;
281 // Extend history buffer
282 int64_t end_position = start_position + size;
283 // printf("LiveAudio::process_buffer %lld %lld %lld\n",
286 // end_position - history_position);
287 if(end_position > history_position)
289 // Reset history buffer to current position if after maximum history
290 if(start_position >= history_position + history[0]->get_allocated())
291 history_position = start_position;
292 // A delay seems required because ALSA playback may get ahead of
293 // ALSA recording and never recover.
294 if(first_buffer) end_position += sample_rate;
296 while(!done && history_position < end_position)
298 if(history_ptr + fragment_size >= history[0]->get_allocated())
303 // Read rest of buffer from sound driver
306 int over[history_channels];
307 double max[history_channels];
308 int result = adevice->read_buffer(history,
314 if( result && adevice->config_updated() )
315 adevice->config_update();
318 history_ptr += fragment_size;
319 // wrap around buffer
320 if(history_ptr >= history[0]->get_allocated())
322 history_position += fragment_size;
326 // Copy data from history buffer
327 int buffer_position = 0;
328 int history_buffer_ptr = history_ptr - history_position + start_position;
329 while(history_buffer_ptr < 0)
330 history_buffer_ptr += history[0]->get_allocated();
331 while(buffer_position < size)
334 if(history_buffer_ptr + fragment > history[0]->get_allocated())
335 fragment = history[0]->get_allocated() - history_buffer_ptr;
336 if(buffer_position + fragment > size)
337 fragment = size - buffer_position;
339 for(int i = 0; i < get_total_buffers(); i++)
341 int input_channel = i;
342 // Clamp channel to total history channels
343 if(input_channel >= history_channels)
344 input_channel = history_channels - 1;
345 memcpy(buffer[i]->get_data() + buffer_position,
346 history[input_channel]->get_data() + history_buffer_ptr,
347 sizeof(double) * fragment);
350 history_buffer_ptr += fragment;
351 if(history_buffer_ptr >= history[0]->get_allocated())
352 history_buffer_ptr = 0;
353 buffer_position += fragment;
363 void LiveAudio::render_stop()
367 adevice->interrupt_crash();
368 adevice->close_all();
373 history_position = 0;
378 const char* LiveAudio::plugin_title() { return N_("Live Audio"); }
379 int LiveAudio::is_realtime() { return 1; }
380 int LiveAudio::is_multichannel() { return 1; }
381 int LiveAudio::is_synthesis() { return 1; }
385 NEW_WINDOW_MACRO(LiveAudio, LiveAudioWindow)
389 int LiveAudio::load_configuration()
395 void LiveAudio::save_data(KeyFrame *keyframe)
399 void LiveAudio::read_data(KeyFrame *keyframe)
403 void LiveAudio::update_gui()