4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
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.
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.
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
23 #include "audiodevice.h"
25 #include "audio1394.h"
27 #include "audioalsa.h"
29 #include "audioesound.h"
31 #include "audiopulse.h"
32 #include "audiov4l2mpeg.h"
35 #include "condition.h"
38 #include "edlsession.h"
42 #include "mwindowgui.h"
43 #include "playbackconfig.h"
44 #include "preferences.h"
45 #include "recordconfig.h"
46 #include "recordgui.h"
51 AudioLowLevel::AudioLowLevel(AudioDevice *device)
53 this->device = device;
56 AudioLowLevel::~AudioLowLevel()
61 AudioDevice::AudioDevice(MWindow *mwindow)
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");
80 total_samples_read = 0;
81 total_samples_input = 0;
83 total_samples_written = 0;
84 total_samples_output = 0;
87 AudioDevice::~AudioDevice()
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;
99 delete playback_timer;
106 int AudioDevice::initialize()
108 for(int i = 0; i < TOTAL_AUDIO_BUFFERS; i++) {
110 output[i].buffer = 0;
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.;
124 software_position_info = 0;
131 void AudioThread::initialize()
142 AudioThread(AudioDevice *device,
143 void (AudioDevice::*arun)(), void (AudioDevice::*aend)())
147 startup_lock = new Condition(0, "AudioThread::startup_lock");
148 this->device = device;
154 AudioThread::~AudioThread()
160 void AudioThread::stop(int wait)
162 if( !wait ) (device->*aend)();
167 void AudioThread::run()
169 startup_lock->lock();
173 void AudioThread::startup()
177 startup_lock->unlock();
182 void AudioDevice::stop_input()
184 device_lock->lock("AudioDevice::stop_input");
191 device_lock->unlock();
194 void AudioDevice::stop_output(int wait)
196 if( !wait ) playback_interrupted = 1;
197 device_lock->lock("AudioDevice::stop_output");
199 if( is_playing_back )
200 audio_out->stop(wait);
205 device_lock->unlock();
208 int AudioDevice::stop_audio(int wait)
210 if( audio_in ) stop_input();
211 if( audio_out ) stop_output(wait);
216 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver,int in)
218 *(in ? &this->idriver : &this->odriver) = driver;
224 case AUDIO_OSS_ENVY24:
225 lowlevel = new AudioOSS(this);
231 lowlevel = new AudioESound(this);
239 lowlevel = new AudioALSA(this);
247 lowlevel = new Audio1394(this);
253 lowlevel = new AudioDVB(this);
257 #ifdef HAVE_VIDEO4LINUX2
259 lowlevel = new AudioV4L2MPEG(this);
265 lowlevel = new AudioPulse(this);
273 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
274 int rate, int samples, int channels, int realtime)
276 device_lock->lock("AudioDevice::open_input");
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();
295 int AudioDevice::open_output(AudioOutConfig *config,
296 int rate, int samples, int channels, int realtime)
298 device_lock->lock("AudioDevice::open_output");
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();
314 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
316 device_lock->lock("AudioDevice::open_output");
318 *this->out_config = *config;
321 device_lock->unlock();
327 int AudioDevice::interrupt_crash()
330 lowlevel_in->interrupt_crash();
335 double AudioDevice::get_itimestamp()
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();
349 double AudioDevice::get_otimestamp()
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();
362 double AudioDevice::device_timestamp()
364 return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
367 int AudioDevice::close_all()
369 device_lock->lock("AudioDevice::close_all");
372 lowlevel_in->close_all();
378 lowlevel_out->close_all();
383 device_lock->unlock();
387 void AudioDevice::auto_update(int channels, int samplerate, int bits)
389 if( !in_config || !in_config->follow_audio ) return;
390 in_channels = channels;
391 in_samplerate = samplerate;
393 in_config_updated = 1;
394 polling_lock->unlock();
397 int AudioDevice::config_updated()
399 if( !in_config || !in_config->follow_audio ) return 0;
400 return in_config_updated;
403 void AudioDevice::config_update()
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;
420 int AudioDevice::start_toc(const char *path, const char *toc_path)
422 return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
425 int AudioDevice::start_record(int fd, int bsz)
427 return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
430 int AudioDevice::stop_record()
432 return lowlevel_in ? lowlevel_in->stop_record() : -1;
435 int AudioDevice::total_audio_channels()
437 return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
440 DeviceMPEGInput *AudioDevice::mpeg_device()
442 return lowlevel_in ? lowlevel_in->mpeg_device() : 0;