00001 #include "asset.h"
00002 #include "audiodevice.h"
00003 #include "bcdisplayinfo.h"
00004 #include "bcsignals.h"
00005 #include "clip.h"
00006 #include "bchash.h"
00007 #include "edlsession.h"
00008 #include "filexml.h"
00009 #include "guicast.h"
00010 #include "language.h"
00011 #include "picon_png.h"
00012 #include "pluginaclient.h"
00013 #include "transportque.inc"
00014 #include "vframe.h"
00015
00016 #include <string.h>
00017 #include <stdint.h>
00018
00019 #define HISTORY_SAMPLES 0x100000
00020 class LiveAudio;
00021 class LiveAudioWindow;
00022
00023
00024 class LiveAudioConfig
00025 {
00026 public:
00027 LiveAudioConfig();
00028 };
00029
00030
00031
00032
00033
00034
00035 class LiveAudioWindow : public BC_Window
00036 {
00037 public:
00038 LiveAudioWindow(LiveAudio *plugin, int x, int y);
00039 ~LiveAudioWindow();
00040
00041 void create_objects();
00042 int close_event();
00043
00044 LiveAudio *plugin;
00045 };
00046
00047
00048 PLUGIN_THREAD_HEADER(LiveAudio, LiveAudioThread, LiveAudioWindow)
00049
00050
00051
00052 class LiveAudio : public PluginAClient
00053 {
00054 public:
00055 LiveAudio(PluginServer *server);
00056 ~LiveAudio();
00057
00058
00059 PLUGIN_CLASS_MEMBERS(LiveAudioConfig, LiveAudioThread);
00060
00061 int process_buffer(int64_t size,
00062 double **buffer,
00063 int64_t start_position,
00064 int sample_rate);
00065 int is_realtime();
00066 int is_multichannel();
00067 int is_synthesis();
00068 int load_defaults();
00069 int save_defaults();
00070 void save_data(KeyFrame *keyframe);
00071 void read_data(KeyFrame *keyframe);
00072 void update_gui();
00073 void render_stop();
00074
00075 AudioDevice *adevice;
00076 double **history;
00077 int history_ptr;
00078 int history_channels;
00079 int64_t history_position;
00080 int history_size;
00081 };
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 LiveAudioConfig::LiveAudioConfig()
00095 {
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105 LiveAudioWindow::LiveAudioWindow(LiveAudio *plugin, int x, int y)
00106 : BC_Window(plugin->gui_string,
00107 x,
00108 y,
00109 300,
00110 160,
00111 300,
00112 160,
00113 0,
00114 0,
00115 1)
00116 {
00117 this->plugin = plugin;
00118 }
00119
00120 LiveAudioWindow::~LiveAudioWindow()
00121 {
00122 }
00123
00124 void LiveAudioWindow::create_objects()
00125 {
00126 int x = 10, y = 10;
00127
00128 BC_Title *title;
00129 add_subwindow(title = new BC_Title(x, y, "Live audio"));
00130 show_window();
00131 flush();
00132 }
00133
00134 WINDOW_CLOSE_EVENT(LiveAudioWindow)
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 PLUGIN_THREAD_OBJECT(LiveAudio, LiveAudioThread, LiveAudioWindow)
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 REGISTER_PLUGIN(LiveAudio)
00156
00157
00158
00159
00160
00161
00162 LiveAudio::LiveAudio(PluginServer *server)
00163 : PluginAClient(server)
00164 {
00165 adevice = 0;
00166 history = 0;
00167 history_channels = 0;
00168 history_ptr = 0;
00169 history_position = 0;
00170 history_size = 0;
00171 PLUGIN_CONSTRUCTOR_MACRO
00172 }
00173
00174
00175 LiveAudio::~LiveAudio()
00176 {
00177 if(adevice)
00178 {
00179 adevice->interrupt_crash();
00180 adevice->close_all();
00181 }
00182 delete adevice;
00183 if(history)
00184 {
00185 for(int i = 0; i < history_channels; i++)
00186 delete [] history[i];
00187 delete [] history;
00188 }
00189 PLUGIN_DESTRUCTOR_MACRO
00190 }
00191
00192
00193
00194 int LiveAudio::process_buffer(int64_t size,
00195 double **buffer,
00196 int64_t start_position,
00197 int sample_rate)
00198 {
00199 load_configuration();
00200
00201
00202 int first_buffer = 0;
00203
00204 if(!adevice)
00205 {
00206 EDLSession *session = PluginClient::get_edlsession();
00207 if(session)
00208 {
00209 adevice = new AudioDevice;
00210 adevice->open_input(session->aconfig_in,
00211 session->vconfig_in,
00212 get_project_samplerate(),
00213 get_buffer_size(),
00214 get_total_buffers(),
00215 session->real_time_record);
00216 adevice->start_recording();
00217 first_buffer = 1;
00218 history_position = start_position;
00219 }
00220 }
00221
00222 if(!history)
00223 {
00224 history_channels = get_total_buffers();
00225 history = new double*[history_channels];
00226 for(int i = 0; i < history_channels; i++)
00227 {
00228 history[i] = new double[HISTORY_SAMPLES];
00229 bzero(history[i], sizeof(double) * HISTORY_SAMPLES);
00230 }
00231 }
00232
00233 SET_TRACE
00234
00235 {
00236
00237 if(start_position < history_position - HISTORY_SAMPLES)
00238 history_position = start_position;
00239
00240
00241
00242
00243 int64_t end_position = start_position + size;
00244
00245
00246
00247
00248 if(end_position > history_position)
00249 {
00250
00251 if(start_position >= history_position + HISTORY_SAMPLES)
00252 history_position = start_position;
00253
00254
00255 if(first_buffer) end_position += sample_rate;
00256 int done = 0;
00257 while(!done && history_position < end_position)
00258 {
00259
00260
00261
00262 int fragment = size;
00263 if(history_ptr + fragment > HISTORY_SAMPLES)
00264 {
00265 fragment = HISTORY_SAMPLES - history_ptr;
00266 done = 1;
00267 }
00268
00269
00270 if(adevice)
00271 {
00272 int over[get_total_buffers()];
00273 double max[get_total_buffers()];
00274 adevice->read_buffer(history,
00275 fragment,
00276 over,
00277 max,
00278 history_ptr);
00279 }
00280 history_ptr += fragment;
00281
00282 if(history_ptr >= HISTORY_SAMPLES)
00283 history_ptr = 0;
00284 history_position += fragment;
00285 }
00286 }
00287
00288
00289 int buffer_position = 0;
00290 int history_buffer_ptr = history_ptr - history_position + start_position;
00291 while(history_buffer_ptr < 0)
00292 history_buffer_ptr += HISTORY_SAMPLES;
00293 while(buffer_position < size)
00294 {
00295 int fragment = size;
00296 if(history_buffer_ptr + fragment > HISTORY_SAMPLES)
00297 fragment = HISTORY_SAMPLES - history_buffer_ptr;
00298 if(buffer_position + fragment > size)
00299 fragment = size - buffer_position;
00300 for(int i = 0; i < get_total_buffers(); i++)
00301 memcpy(buffer[i] + buffer_position,
00302 history[i] + history_buffer_ptr,
00303 sizeof(double) * fragment);
00304 history_buffer_ptr += fragment;
00305 if(history_buffer_ptr >= HISTORY_SAMPLES)
00306 history_buffer_ptr = 0;
00307 buffer_position += fragment;
00308 }
00309
00310 SET_TRACE
00311 }
00312
00313
00314 return 0;
00315 }
00316
00317 void LiveAudio::render_stop()
00318 {
00319 if(adevice)
00320 {
00321 adevice->interrupt_crash();
00322 adevice->close_all();
00323 }
00324 delete adevice;
00325 adevice = 0;
00326 history_ptr = 0;
00327 history_position = 0;
00328 history_size = 0;
00329 }
00330
00331
00332 char* LiveAudio::plugin_title() { return N_("Live Audio"); }
00333 int LiveAudio::is_realtime() { return 1; }
00334 int LiveAudio::is_multichannel() { return 1; }
00335 int LiveAudio::is_synthesis() { return 1; }
00336
00337
00338 NEW_PICON_MACRO(LiveAudio)
00339
00340 SHOW_GUI_MACRO(LiveAudio, LiveAudioThread)
00341
00342 RAISE_WINDOW_MACRO(LiveAudio)
00343
00344 SET_STRING_MACRO(LiveAudio);
00345
00346 int LiveAudio::load_configuration()
00347 {
00348 return 0;
00349 }
00350
00351 int LiveAudio::load_defaults()
00352 {
00353 return 0;
00354 }
00355
00356 int LiveAudio::save_defaults()
00357 {
00358 return 0;
00359 }
00360
00361 void LiveAudio::save_data(KeyFrame *keyframe)
00362 {
00363 }
00364
00365 void LiveAudio::read_data(KeyFrame *keyframe)
00366 {
00367 }
00368
00369 void LiveAudio::update_gui()
00370 {
00371 }
00372
00373
00374
00375
00376