Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / audiodevice.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * Copyright (C) 2003-2016 Cinelerra CV contributors
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "audiodevice.h"
24 #ifdef HAVE_FIREWIRE
25 #include "audio1394.h"
26 #endif
27 #include "audioalsa.h"
28 #include "audiodvb.h"
29 #include "audioesound.h"
30 #include "audiooss.h"
31 #include "audiopulse.h"
32 #include "audiov4l2mpeg.h"
33 #include "asset.h"
34 #include "bctimer.h"
35 #include "condition.h"
36 #include "dcoffset.h"
37 #include "edl.h"
38 #include "edlsession.h"
39 #include "mainmenu.h"
40 #include "mutex.h"
41 #include "mwindow.h"
42 #include "mwindowgui.h"
43 #include "playbackconfig.h"
44 #include "preferences.h"
45 #include "recordconfig.h"
46 #include "recordgui.h"
47 #include "record.h"
48 #include "sema.h"
49
50
51 AudioLowLevel::AudioLowLevel(AudioDevice *device)
52 {
53         this->device = device;
54 }
55
56 AudioLowLevel::~AudioLowLevel()
57 {
58 }
59
60
61 AudioDevice::AudioDevice(MWindow *mwindow)
62 {
63         initialize();
64         this->mwindow = mwindow;
65         this->out_config = new AudioOutConfig();
66         this->in_config = new AudioInConfig;
67         this->vconfig = new VideoInConfig;
68         device_lock = new Mutex("AudioDevice::device_lock",1);
69         timer_lock = new Mutex("AudioDevice::timer_lock");
70         buffer_lock = new Mutex("AudioDevice::buffer_lock");
71         polling_lock = new Condition(0, "AudioDevice::polling_lock");
72         playback_timer = new Timer;
73         record_timer = new Timer;
74         for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
75                 output_buffer_t *obfr = &output[i];
76                 obfr->play_lock = new Condition(0, "AudioDevice::play_lock");
77                 obfr->arm_lock = new Condition(1, "AudioDevice::arm_lock");
78         }
79         reset_input();
80         total_samples_read = 0;
81         total_samples_input = 0;
82         reset_output();
83         total_samples_written = 0;
84         total_samples_output = 0;
85 }
86
87 AudioDevice::~AudioDevice()
88 {
89         stop_audio(0);
90         delete out_config;
91         delete in_config;
92         delete vconfig;
93         delete timer_lock;
94         for( int i=0; i < TOTAL_AUDIO_BUFFERS; ++i ) {
95                 output_buffer_t *obfr = &output[i];
96                 delete obfr->play_lock;
97                 delete obfr->arm_lock;
98         }
99         delete playback_timer;
100         delete record_timer;
101         delete buffer_lock;
102         delete polling_lock;
103         delete device_lock;
104 }
105
106 int AudioDevice::initialize()
107 {
108         for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
109                 input[i].buffer = 0;
110                 output[i].buffer = 0;
111         }
112         audio_in = audio_out = 0;
113         in_bits = out_bits = 0;
114         in_channels = out_channels = 0;
115         in_samples = out_samples = 0;
116         in_samplerate = out_samplerate = 0;
117         in_realtime = out_realtime = 0;
118         lowlevel_out = lowlevel_in = 0;
119         in51_2 = out51_2 = 0;
120         in_config_updated = 0;
121         rec_dither = play_dither = 0;
122         rec_gain = play_gain = 1.;
123         r = w = 0;
124         software_position_info = 0;
125         vdevice = 0;
126         sharing = 0;
127         return 0;
128 }
129
130
131 void AudioThread::initialize()
132 {
133         device = 0;
134         arun = 0;
135         aend = 0;
136         startup_lock = 0;
137         started = 0;
138 }
139
140
141 AudioThread::
142 AudioThread(AudioDevice *device,
143         void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
144  : Thread(1, 0, 0)
145 {
146         initialize();
147         startup_lock = new Condition(0, "AudioThread::startup_lock");
148         this->device = device;
149         this->arun = arun;
150         this->aend = aend;
151         Thread::start();
152 }
153
154 AudioThread::~AudioThread()
155 {
156         stop(0);
157         delete startup_lock;
158 }
159
160 void AudioThread::stop(int wait)
161 {
162         if( !wait ) (device->*aend)();
163         startup();
164         Thread::join();
165 }
166
167 void AudioThread::run()
168 {
169         startup_lock->lock();
170         (device->*arun)();
171 }
172
173 void AudioThread::startup()
174 {
175         if( !started ) {
176                 started = 1;
177                 startup_lock->unlock();
178         }
179 }
180
181
182 void AudioDevice::stop_input()
183 {
184         device_lock->lock("AudioDevice::stop_input");
185         if( audio_in ) {
186                 audio_in->stop(0);
187                 delete audio_in;
188                 audio_in = 0;
189         }
190         reset_input();
191         device_lock->unlock();
192 }
193
194 void AudioDevice::stop_output(int wait)
195 {
196         if( !wait ) playback_interrupted = 1;
197         device_lock->lock("AudioDevice::stop_output");
198         if( audio_out ) {
199                 if( is_playing_back )
200                         audio_out->stop(wait);
201                 delete audio_out;
202                 audio_out = 0;
203         }
204         reset_output();
205         device_lock->unlock();
206 }
207
208 int AudioDevice::stop_audio(int wait)
209 {
210         if( audio_in ) stop_input();
211         if( audio_out ) stop_output(wait);
212         return 0;
213 }
214
215
216 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
217 {
218         *(in ? &this->idriver : &this->odriver) = driver;
219
220         if(!lowlevel) {
221                 switch(driver) {
222 #ifdef HAVE_OSS
223                 case AUDIO_OSS:
224                 case AUDIO_OSS_ENVY24:
225                         lowlevel = new AudioOSS(this);
226                         break;
227 #endif
228
229 #ifdef HAVE_ESOUND
230                 case AUDIO_ESOUND:
231                         lowlevel = new AudioESound(this);
232                         break;
233 #endif
234                 case AUDIO_NAS:
235                         break;
236
237 #ifdef HAVE_ALSA
238                 case AUDIO_ALSA:
239                         lowlevel = new AudioALSA(this);
240                         break;
241 #endif
242
243 #ifdef HAVE_FIREWIRE
244                 case AUDIO_1394:
245                 case AUDIO_DV1394:
246                 case AUDIO_IEC61883:
247                         lowlevel = new Audio1394(this);
248                         break;
249 #endif
250
251 #ifdef HAVE_DVB
252                 case AUDIO_DVB:
253                         lowlevel = new AudioDVB(this);
254                         break;
255 #endif
256
257 #ifdef HAVE_VIDEO4LINUX2
258                 case AUDIO_V4L2MPEG:
259                         lowlevel = new AudioV4L2MPEG(this);
260                         break;
261 #endif
262
263 #ifdef HAVE_PULSE
264                 case AUDIO_PULSE:
265                         lowlevel = new AudioPulse(this);
266                         break;
267 #endif
268                 }
269         }
270         return 0;
271 }
272
273 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
274         int rate, int samples, int channels, int realtime)
275 {
276         device_lock->lock("AudioDevice::open_input");
277         r = 1;
278         this->in_config->copy_from(config);
279         this->vconfig->copy_from(vconfig);
280         in_samplerate = rate;
281         in_samples = samples;
282         in_realtime = realtime;
283         in51_2 = config->map51_2 && channels == 2;
284         if( in51_2 ) channels = 6;
285         in_channels = channels;
286         rec_gain = config->rec_gain;
287         create_lowlevel(lowlevel_in, config->driver, 1);
288         lowlevel_in->open_input();
289         in_config_updated = 0;
290         record_timer->update();
291         device_lock->unlock();
292         return 0;
293 }
294
295 int AudioDevice::open_output(AudioOutConfig *config,
296         int rate, int samples, int channels, int realtime)
297 {
298         device_lock->lock("AudioDevice::open_output");
299         w = 1;
300         *this->out_config = *config;
301         out_samplerate = rate;
302         out_samples = samples;
303         out51_2 = config->map51_2 && channels == 6;
304         if( out51_2 ) channels = 2;
305         out_channels = channels;
306         out_realtime = realtime;
307         play_gain = config->play_gain;
308         create_lowlevel(lowlevel_out, config->driver,0);
309         int result = lowlevel_out ? lowlevel_out->open_output() : 0;
310         device_lock->unlock();
311         return result;
312 }
313
314 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
315 {
316         device_lock->lock("AudioDevice::open_output");
317         w = 1;
318         *this->out_config = *config;
319         monitoring = mode;
320         monitor_open = 0;
321         device_lock->unlock();
322         return 0;
323 }
324
325
326
327 int AudioDevice::interrupt_crash()
328 {
329         if( lowlevel_in )
330                 lowlevel_in->interrupt_crash();
331         stop_input();
332         return 0;
333 }
334
335 double AudioDevice::get_itimestamp()
336 {
337         buffer_lock->lock("AudioDevice::get_itimestamp");
338         timer_lock->lock("AudioDevice::get_otimestamp");
339         input_buffer_t *ibfr = &input[input_buffer_out];
340         int frame = get_ichannels() * get_ibits() / 8;
341         int64_t samples = frame ? input_buffer_offset / frame : 0;
342         double timestamp = !in_samplerate || ibfr->timestamp < 0. ? -1. :
343                 ibfr->timestamp + (double) samples / in_samplerate;
344         timer_lock->unlock();
345         buffer_lock->unlock();
346         return timestamp;
347 }
348
349 double AudioDevice::get_otimestamp()
350 {
351         buffer_lock->lock("AudioDevice::get_otimestamp");
352         timer_lock->lock("AudioDevice::get_otimestamp");
353         int64_t unplayed_samples = total_samples_output - current_position();
354         double unplayed_time = !out_samplerate || last_buffer_time < 0. ? -1. :
355                 (double)unplayed_samples / out_samplerate;
356         double timestamp = last_buffer_time - unplayed_time;
357         timer_lock->unlock();
358         buffer_lock->unlock();
359         return timestamp;
360 }
361
362 double AudioDevice::device_timestamp()
363 {
364         return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
365 }
366
367 int AudioDevice::close_all()
368 {
369         device_lock->lock("AudioDevice::close_all");
370         stop_audio(0);
371         if( lowlevel_in ) {
372                 lowlevel_in->close_all();
373                 delete lowlevel_in;
374                 lowlevel_in = 0;
375         }
376         if( lowlevel_out )
377         {
378                 lowlevel_out->close_all();
379                 delete lowlevel_out;
380                 lowlevel_out = 0;
381         }
382         initialize();
383         device_lock->unlock();
384         return 0;
385 }
386
387 void AudioDevice::auto_update(int channels, int samplerate, int bits)
388 {
389         if( !in_config || !in_config->follow_audio ) return;
390         in_channels = channels;
391         in_samplerate = samplerate;
392         in_bits = bits;
393         in_config_updated = 1;
394         polling_lock->unlock();
395 }
396
397 int AudioDevice::config_updated()
398 {
399         if( !in_config || !in_config->follow_audio ) return 0;
400         return in_config_updated;
401 }
402
403 void AudioDevice::config_update()
404 {
405         in_config_updated = 0;
406         AudioInConfig *aconfig_in = mwindow->edl->session->aconfig_in;
407         in51_2 = aconfig_in->map51_2 && in_channels == 6 ? 1 : 0;
408         int ichannels = in51_2 ? 2 : in_channels;
409         AudioOutConfig *aconfig_out = mwindow->edl->session->playback_config->aconfig;
410         out51_2 = aconfig_out->map51_2 && ichannels == 6 ? 1 : 0;
411         aconfig_in->in_samplerate = in_samplerate;
412         aconfig_in->channels = ichannels;
413         aconfig_in->oss_in_bits = in_bits;
414         aconfig_in->alsa_in_bits = in_bits;
415         total_samples_read = 0;
416         total_samples_input = 0;
417         monitor_open = 0;
418 }
419
420 int AudioDevice::start_toc(const char *path, const char *toc_path)
421 {
422         return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
423 }
424
425 int AudioDevice::start_record(int fd, int bsz)
426 {
427         return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
428 }
429
430 int AudioDevice::stop_record()
431 {
432         return lowlevel_in ? lowlevel_in->stop_record() : -1;
433 }
434
435 int AudioDevice::total_audio_channels()
436 {
437         return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
438 }
439
440 DeviceMPEGInput *AudioDevice::mpeg_device()
441 {
442         return lowlevel_in ? lowlevel_in->mpeg_device() : 0;
443 }
444