2 #include "audiodevice.h"
3 #include "bcdisplayinfo.h"
7 #include "edlsession.h"
11 #include "picon_png.h"
12 #include "pluginaclient.h"
13 #include "transportque.inc"
19 #define HISTORY_SAMPLES 0x100000
21 class LiveAudioWindow;
35 class LiveAudioWindow : public BC_Window
38 LiveAudioWindow(LiveAudio *plugin, int x, int y);
41 void create_objects();
48 PLUGIN_THREAD_HEADER(LiveAudio, LiveAudioThread, LiveAudioWindow)
52 class LiveAudio : public PluginAClient
55 LiveAudio(PluginServer *server);
59 PLUGIN_CLASS_MEMBERS(LiveAudioConfig, LiveAudioThread);
61 int process_buffer(int64_t size,
63 int64_t start_position,
66 int is_multichannel();
70 void save_data(KeyFrame *keyframe);
71 void read_data(KeyFrame *keyframe);
79 int64_t history_position;
94 LiveAudioConfig::LiveAudioConfig()
105 LiveAudioWindow::LiveAudioWindow(LiveAudio *plugin, int x, int y)
106 : BC_Window(plugin->gui_string,
117 this->plugin = plugin;
120 LiveAudioWindow::~LiveAudioWindow()
124 void LiveAudioWindow::create_objects()
129 add_subwindow(title = new BC_Title(x, y, "Live audio"));
134 WINDOW_CLOSE_EVENT(LiveAudioWindow)
144 PLUGIN_THREAD_OBJECT(LiveAudio, LiveAudioThread, LiveAudioWindow)
155 REGISTER_PLUGIN(LiveAudio)
162 LiveAudio::LiveAudio(PluginServer *server)
163 : PluginAClient(server)
167 history_channels = 0;
169 history_position = 0;
171 PLUGIN_CONSTRUCTOR_MACRO
175 LiveAudio::~LiveAudio()
179 adevice->interrupt_crash();
180 adevice->close_all();
185 for(int i = 0; i < history_channels; i++)
186 delete [] history[i];
189 PLUGIN_DESTRUCTOR_MACRO
194 int LiveAudio::process_buffer(int64_t size,
196 int64_t start_position,
199 load_configuration();
200 //printf("LiveAudio::process_buffer 10 start_position=%lld buffer_size=%d size=%d\n",
201 //start_position, get_buffer_size(), size);
202 int first_buffer = 0;
206 EDLSession *session = PluginClient::get_edlsession();
209 adevice = new AudioDevice;
210 adevice->open_input(session->aconfig_in,
212 get_project_samplerate(),
215 session->real_time_record);
216 adevice->start_recording();
218 history_position = start_position;
224 history_channels = get_total_buffers();
225 history = new double*[history_channels];
226 for(int i = 0; i < history_channels; i++)
228 history[i] = new double[HISTORY_SAMPLES];
229 bzero(history[i], sizeof(double) * HISTORY_SAMPLES);
234 // if(get_direction() == PLAY_FORWARD)
236 // Reset history buffer to current position if before maximum history
237 if(start_position < history_position - HISTORY_SAMPLES)
238 history_position = start_position;
242 // Extend history buffer
243 int64_t end_position = start_position + size;
244 // printf("LiveAudio::process_buffer %lld %lld %lld\n",
247 // end_position - history_position);
248 if(end_position > history_position)
250 // Reset history buffer to current position if after maximum history
251 if(start_position >= history_position + HISTORY_SAMPLES)
252 history_position = start_position;
253 // A delay seems required because ALSA playback may get ahead of
254 // ALSA recording and never recover.
255 if(first_buffer) end_position += sample_rate;
257 while(!done && history_position < end_position)
259 // Reading in playback buffer sized fragments seems to help
260 // even though the sound driver abstracts this size. Larger
261 // fragments probably block unnecessarily long.
263 if(history_ptr + fragment > HISTORY_SAMPLES)
265 fragment = HISTORY_SAMPLES - history_ptr;
269 // Read rest of buffer from sound driver
272 int over[get_total_buffers()];
273 double max[get_total_buffers()];
274 adevice->read_buffer(history,
280 history_ptr += fragment;
281 // wrap around buffer
282 if(history_ptr >= HISTORY_SAMPLES)
284 history_position += fragment;
288 // Copy data from history buffer
289 int buffer_position = 0;
290 int history_buffer_ptr = history_ptr - history_position + start_position;
291 while(history_buffer_ptr < 0)
292 history_buffer_ptr += HISTORY_SAMPLES;
293 while(buffer_position < size)
296 if(history_buffer_ptr + fragment > HISTORY_SAMPLES)
297 fragment = HISTORY_SAMPLES - history_buffer_ptr;
298 if(buffer_position + fragment > size)
299 fragment = size - buffer_position;
300 for(int i = 0; i < get_total_buffers(); i++)
301 memcpy(buffer[i] + buffer_position,
302 history[i] + history_buffer_ptr,
303 sizeof(double) * fragment);
304 history_buffer_ptr += fragment;
305 if(history_buffer_ptr >= HISTORY_SAMPLES)
306 history_buffer_ptr = 0;
307 buffer_position += fragment;
317 void LiveAudio::render_stop()
321 adevice->interrupt_crash();
322 adevice->close_all();
327 history_position = 0;
332 char* LiveAudio::plugin_title() { return N_("Live Audio"); }
333 int LiveAudio::is_realtime() { return 1; }
334 int LiveAudio::is_multichannel() { return 1; }
335 int LiveAudio::is_synthesis() { return 1; }
338 NEW_PICON_MACRO(LiveAudio)
340 SHOW_GUI_MACRO(LiveAudio, LiveAudioThread)
342 RAISE_WINDOW_MACRO(LiveAudio)
344 SET_STRING_MACRO(LiveAudio);
346 int LiveAudio::load_configuration()
351 int LiveAudio::load_defaults()
356 int LiveAudio::save_defaults()
361 void LiveAudio::save_data(KeyFrame *keyframe)
365 void LiveAudio::read_data(KeyFrame *keyframe)
369 void LiveAudio::update_gui()