Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / liveaudio / liveaudio.C
blob09222366194e9fa0d4f42ee3266749ff5f6772a4
1 #include "asset.h"
2 #include "audiodevice.h"
3 #include "bcdisplayinfo.h"
4 #include "bcsignals.h"
5 #include "clip.h"
6 #include "bchash.h"
7 #include "edlsession.h"
8 #include "filexml.h"
9 #include "guicast.h"
10 #include "language.h"
11 #include "picon_png.h"
12 #include "pluginaclient.h"
13 #include "transportque.inc"
14 #include "vframe.h"
16 #include <string.h>
17 #include <stdint.h>
19 #define HISTORY_SAMPLES 0x100000
20 class LiveAudio;
21 class LiveAudioWindow;
24 class LiveAudioConfig
26 public:
27         LiveAudioConfig();
35 class LiveAudioWindow : public BC_Window
37 public:
38         LiveAudioWindow(LiveAudio *plugin, int x, int y);
39         ~LiveAudioWindow();
41         void create_objects();
42         int close_event();
44         LiveAudio *plugin;
48 PLUGIN_THREAD_HEADER(LiveAudio, LiveAudioThread, LiveAudioWindow)
52 class LiveAudio : public PluginAClient
54 public:
55         LiveAudio(PluginServer *server);
56         ~LiveAudio();
59         PLUGIN_CLASS_MEMBERS(LiveAudioConfig, LiveAudioThread);
61         int process_buffer(int64_t size, 
62                 double **buffer,
63                 int64_t start_position,
64                 int sample_rate);
65         int is_realtime();
66         int is_multichannel();
67         int is_synthesis();
68         int load_defaults();
69         int save_defaults();
70         void save_data(KeyFrame *keyframe);
71         void read_data(KeyFrame *keyframe);
72         void update_gui();
73         void render_stop();
75         AudioDevice *adevice;
76         double **history;
77         int history_ptr;
78         int history_channels;
79         int64_t history_position;
80         int history_size;
94 LiveAudioConfig::LiveAudioConfig()
105 LiveAudioWindow::LiveAudioWindow(LiveAudio *plugin, int x, int y)
106  : BC_Window(plugin->gui_string, 
107         x, 
108         y, 
109         300, 
110         160, 
111         300, 
112         160, 
113         0, 
114         0,
115         1)
117         this->plugin = plugin;
120 LiveAudioWindow::~LiveAudioWindow()
124 void LiveAudioWindow::create_objects()
126         int x = 10, y = 10;
128         BC_Title *title;
129         add_subwindow(title = new BC_Title(x, y, "Live audio"));
130         show_window();
131         flush();
134 WINDOW_CLOSE_EVENT(LiveAudioWindow)
144 PLUGIN_THREAD_OBJECT(LiveAudio, LiveAudioThread, LiveAudioWindow)
155 REGISTER_PLUGIN(LiveAudio)
162 LiveAudio::LiveAudio(PluginServer *server)
163  : PluginAClient(server)
165         adevice = 0;
166         history = 0;
167         history_channels = 0;
168         history_ptr = 0;
169         history_position = 0;
170         history_size = 0;
171         PLUGIN_CONSTRUCTOR_MACRO
175 LiveAudio::~LiveAudio()
177         if(adevice)
178         {
179                 adevice->interrupt_crash();
180                 adevice->close_all();
181         }
182         delete adevice;
183         if(history)
184         {
185                 for(int i = 0; i < history_channels; i++)
186                         delete [] history[i];
187                 delete [] history;
188         }
189         PLUGIN_DESTRUCTOR_MACRO
194 int LiveAudio::process_buffer(int64_t size, 
195         double **buffer,
196         int64_t start_position,
197         int sample_rate)
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;
204         if(!adevice)
205         {
206                 EDLSession *session = PluginClient::get_edlsession();
207                 if(session)
208                 {
209                         adevice = new AudioDevice;
210                         adevice->open_input(session->aconfig_in, 
211                                 session->vconfig_in, 
212                                 get_project_samplerate(), 
213                                 get_buffer_size(),
214                                 get_total_buffers(),
215                                 session->real_time_record);
216                         adevice->start_recording();
217                         first_buffer = 1;
218                         history_position = start_position;
219                 }
220         }
222         if(!history)
223         {
224                 history_channels = get_total_buffers();
225                 history = new double*[history_channels];
226                 for(int i = 0; i < history_channels; i++)
227                 {
228                         history[i] = new double[HISTORY_SAMPLES];
229                         bzero(history[i], sizeof(double) * HISTORY_SAMPLES);
230                 }
231         }
233 SET_TRACE
234 //      if(get_direction() == PLAY_FORWARD)
235         {
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", 
245 // end_position, 
246 // history_position,
247 // end_position - history_position);
248                 if(end_position > history_position)
249                 {
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;
256                         int done = 0;
257                         while(!done && history_position < end_position)
258                         {
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.
262                                 int fragment = size;
263                                 if(history_ptr + fragment  > HISTORY_SAMPLES)
264                                 {
265                                         fragment = HISTORY_SAMPLES - history_ptr;
266                                         done = 1;
267                                 }
269 // Read rest of buffer from sound driver
270                                 if(adevice)
271                                 {
272                                         int over[get_total_buffers()];
273                                         double max[get_total_buffers()];
274                                         adevice->read_buffer(history, 
275                                                 fragment, 
276                                                 over, 
277                                                 max, 
278                                                 history_ptr);
279                                 }
280                                 history_ptr += fragment;
281 // wrap around buffer
282                                 if(history_ptr >= HISTORY_SAMPLES)
283                                         history_ptr = 0;
284                                 history_position += fragment;
285                         }
286                 }
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)
294                 {
295                         int fragment = 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;
308                 }
310 SET_TRACE
311         }
314         return 0;
317 void LiveAudio::render_stop()
319         if(adevice)
320         {
321                 adevice->interrupt_crash();
322                 adevice->close_all();
323         }
324         delete adevice;
325         adevice = 0;
326         history_ptr = 0;
327         history_position = 0;
328         history_size = 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()
348         return 0;
351 int LiveAudio::load_defaults()
353         return 0;
356 int LiveAudio::save_defaults()
358         return 0;
361 void LiveAudio::save_data(KeyFrame *keyframe)
365 void LiveAudio::read_data(KeyFrame *keyframe)
369 void LiveAudio::update_gui()