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"
28 #include "edlsession.h"
33 #include "pluginaclient.h"
34 #include "pluginserver.h"
35 #include "recordconfig.h"
37 #include "transportque.inc"
44 class LiveAudioWindow;
58 class LiveAudioWindow : public PluginClientWindow
61 LiveAudioWindow(LiveAudio *plugin);
64 void create_objects();
74 class LiveAudio : public PluginAClient
77 LiveAudio(PluginServer *server);
81 PLUGIN_CLASS_MEMBERS(LiveAudioConfig);
83 int process_buffer(int64_t size,
85 int64_t start_position,
88 int is_multichannel();
90 void save_data(KeyFrame *keyframe);
91 void read_data(KeyFrame *keyframe);
100 int64_t history_position;
102 // Fragment size from the EDL session
117 LiveAudioConfig::LiveAudioConfig()
128 LiveAudioWindow::LiveAudioWindow(LiveAudio *plugin)
129 : PluginClientWindow(plugin,
136 this->plugin = plugin;
139 LiveAudioWindow::~LiveAudioWindow()
143 void LiveAudioWindow::create_objects()
148 add_subwindow(title = new BC_Title(x, y, _("Live audio")));
174 REGISTER_PLUGIN(LiveAudio)
181 LiveAudio::LiveAudio(PluginServer *server)
182 : PluginAClient(server)
186 history_channels = 0;
188 history_position = 0;
195 LiveAudio::~LiveAudio()
199 adevice->interrupt_crash();
200 adevice->close_all();
205 for(int i = 0; i < history_channels; i++)
213 int LiveAudio::process_buffer(int64_t size,
215 int64_t start_position,
218 load_configuration();
220 // printf("LiveAudio::process_buffer 10 size=%lld buffer_size=%d channels=%d size=%d\n",
221 // size, get_buffer_size(), get_total_buffers(), size);
223 int first_buffer = 0;
227 EDLSession *session = PluginClient::get_edlsession();
230 fragment_size = session->record_fragment_size;
231 history_channels = session->aconfig_in->channels;
233 adevice = new AudioDevice(server ? server->mwindow : 0);
234 // Take fragment size & channels from the recording config
235 adevice->open_input(session->aconfig_in,
237 get_project_samplerate(),
239 session->aconfig_in->channels,
240 session->real_time_record);
241 adevice->start_recording();
243 history_position = start_position;
247 if(!history || history[0]->get_allocated() < size)
249 // compute new history which is a multiple of our fragment size
250 int new_history_size = fragment_size;
251 while(new_history_size < size)
252 new_history_size += fragment_size;
257 history = new Samples*[history_channels];
258 for(int i = 0; i < history_channels; i++)
262 for(int i = 0; i < history_channels; i++)
265 history[i] = new Samples(new_history_size);
266 bzero(history[i]->get_data(), sizeof(double) * new_history_size);
272 // if(get_direction() == PLAY_FORWARD)
274 // Reset history buffer to current position if before maximum history
275 if(start_position < history_position - history[0]->get_allocated())
276 history_position = start_position;
280 // Extend history buffer
281 int64_t end_position = start_position + size;
282 // printf("LiveAudio::process_buffer %lld %lld %lld\n",
285 // end_position - history_position);
286 if(end_position > history_position)
288 // Reset history buffer to current position if after maximum history
289 if(start_position >= history_position + history[0]->get_allocated())
290 history_position = start_position;
291 // A delay seems required because ALSA playback may get ahead of
292 // ALSA recording and never recover.
293 if(first_buffer) end_position += sample_rate;
295 while(!done && history_position < end_position)
297 if(history_ptr + fragment_size >= history[0]->get_allocated())
302 // Read rest of buffer from sound driver
305 int over[history_channels];
306 double max[history_channels];
307 int result = adevice->read_buffer(history,
313 if( result && adevice->config_updated() )
314 adevice->config_update();
317 history_ptr += fragment_size;
318 // wrap around buffer
319 if(history_ptr >= history[0]->get_allocated())
321 history_position += fragment_size;
325 // Copy data from history buffer
326 int buffer_position = 0;
327 int history_buffer_ptr = history_ptr - history_position + start_position;
328 while(history_buffer_ptr < 0)
329 history_buffer_ptr += history[0]->get_allocated();
330 while(buffer_position < size)
333 if(history_buffer_ptr + fragment > history[0]->get_allocated())
334 fragment = history[0]->get_allocated() - history_buffer_ptr;
335 if(buffer_position + fragment > size)
336 fragment = size - buffer_position;
338 for(int i = 0; i < get_total_buffers(); i++)
340 int input_channel = i;
341 // Clamp channel to total history channels
342 if(input_channel >= history_channels)
343 input_channel = history_channels - 1;
344 memcpy(buffer[i]->get_data() + buffer_position,
345 history[input_channel]->get_data() + history_buffer_ptr,
346 sizeof(double) * fragment);
349 history_buffer_ptr += fragment;
350 if(history_buffer_ptr >= history[0]->get_allocated())
351 history_buffer_ptr = 0;
352 buffer_position += fragment;
362 void LiveAudio::render_stop()
366 adevice->interrupt_crash();
367 adevice->close_all();
372 history_position = 0;
377 const char* LiveAudio::plugin_title() { return N_("Live Audio"); }
378 int LiveAudio::is_realtime() { return 1; }
379 int LiveAudio::is_multichannel() { return 1; }
380 int LiveAudio::is_synthesis() { return 1; }
384 NEW_WINDOW_MACRO(LiveAudio, LiveAudioWindow)
388 int LiveAudio::load_configuration()
394 void LiveAudio::save_data(KeyFrame *keyframe)
398 void LiveAudio::read_data(KeyFrame *keyframe)
402 void LiveAudio::update_gui()