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