rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[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                 case AUDIO_DVB:
250                         lowlevel = new AudioDVB(this);
251                         break;
252
253                 case AUDIO_V4L2MPEG:
254                         lowlevel = new AudioV4L2MPEG(this);
255                         break;
256                 }
257         }
258         return 0;
259 }
260
261 int AudioDevice::open_input(AudioInConfig *config, VideoInConfig *vconfig,
262         int rate, int samples, int channels, int realtime)
263 {
264         device_lock->lock("AudioDevice::open_input");
265         r = 1;
266         this->in_config->copy_from(config);
267         this->vconfig->copy_from(vconfig);
268         in_samplerate = rate;
269         in_samples = samples;
270         in_realtime = realtime;
271         in51_2 = config->map51_2 && channels == 2;
272         if( in51_2 ) channels = 6;
273         in_channels = channels;
274         rec_gain = config->rec_gain;
275         create_lowlevel(lowlevel_in, config->driver, 1);
276         lowlevel_in->open_input();
277         in_config_updated = 0;
278         record_timer->update();
279         device_lock->unlock();
280         return 0;
281 }
282
283 int AudioDevice::open_output(AudioOutConfig *config,
284         int rate, int samples, int channels, int realtime)
285 {
286         device_lock->lock("AudioDevice::open_output");
287         w = 1;
288         *this->out_config = *config;
289         out_samplerate = rate;
290         out_samples = samples;
291         out51_2 = config->map51_2 && channels == 6;
292         if( out51_2 ) channels = 2;
293         out_channels = channels;
294         out_realtime = realtime;
295         play_gain = config->play_gain;
296         create_lowlevel(lowlevel_out, config->driver,0);
297         int result = lowlevel_out ? lowlevel_out->open_output() : 0;
298         device_lock->unlock();
299         return result;
300 }
301
302 int AudioDevice::open_monitor(AudioOutConfig *config,int mode)
303 {
304         device_lock->lock("AudioDevice::open_output");
305         w = 1;
306         *this->out_config = *config;
307         monitoring = mode;
308         monitor_open = 0;
309         device_lock->unlock();
310         return 0;
311 }
312
313
314
315 int AudioDevice::interrupt_crash()
316 {
317         if( lowlevel_in )
318                 lowlevel_in->interrupt_crash();
319         stop_input();
320         return 0;
321 }
322
323 double AudioDevice::get_itimestamp()
324 {
325         buffer_lock->lock("AudioDevice::get_itimestamp");
326         timer_lock->lock("AudioDevice::get_otimestamp");
327         input_buffer_t *ibfr = &input[input_buffer_out];
328         int frame = get_ichannels() * get_ibits() / 8;
329         int64_t samples = frame ? input_buffer_offset / frame : 0;
330         double timestamp = !in_samplerate || ibfr->timestamp < 0. ? -1. :
331                 ibfr->timestamp + (double) samples / in_samplerate;
332         timer_lock->unlock();
333         buffer_lock->unlock();
334         return timestamp;
335 }
336
337 double AudioDevice::get_otimestamp()
338 {
339         buffer_lock->lock("AudioDevice::get_otimestamp");
340         timer_lock->lock("AudioDevice::get_otimestamp");
341         int64_t unplayed_samples = total_samples_output - current_position();
342         double unplayed_time = !out_samplerate || last_buffer_time < 0. ? -1. :
343                 (double)unplayed_samples / out_samplerate;
344         double timestamp = last_buffer_time - unplayed_time;
345         timer_lock->unlock();
346         buffer_lock->unlock();
347         return timestamp;
348 }
349
350 double AudioDevice::device_timestamp()
351 {
352         return lowlevel_in ? lowlevel_in->device_timestamp() : -1.;
353 }
354
355 int AudioDevice::close_all()
356 {
357         device_lock->lock("AudioDevice::close_all");
358         stop_audio(0);
359         if( lowlevel_in ) {
360                 lowlevel_in->close_all();
361                 delete lowlevel_in;
362                 lowlevel_in = 0;
363         }
364         if( lowlevel_out )
365         {
366                 lowlevel_out->close_all();
367                 delete lowlevel_out;
368                 lowlevel_out = 0;
369         }
370         initialize();
371         device_lock->unlock();
372         return 0;
373 }
374
375 void AudioDevice::auto_update(int channels, int samplerate, int bits)
376 {
377         if( !in_config || !in_config->follow_audio ) return;
378         in_channels = channels;
379         in_samplerate = samplerate;
380         in_bits = bits;
381         in_config_updated = 1;
382         polling_lock->unlock();
383 }
384
385 int AudioDevice::config_updated()
386 {
387         if( !in_config || !in_config->follow_audio ) return 0;
388         return in_config_updated;
389 }
390
391 void AudioDevice::config_update()
392 {
393         in_config_updated = 0;
394         AudioInConfig *aconfig_in = mwindow->edl->session->aconfig_in;
395         in51_2 = aconfig_in->map51_2 && in_channels == 6 ? 1 : 0;
396         int ichannels = in51_2 ? 2 : in_channels;
397         AudioOutConfig *aconfig_out = mwindow->edl->session->playback_config->aconfig;
398         out51_2 = aconfig_out->map51_2 && ichannels == 6 ? 1 : 0;
399         aconfig_in->in_samplerate = in_samplerate;
400         aconfig_in->channels = ichannels;
401         aconfig_in->oss_in_bits = in_bits;
402         aconfig_in->alsa_in_bits = in_bits;
403         total_samples_read = 0;
404         total_samples_input = 0;
405         monitor_open = 0;
406 }
407
408 int AudioDevice::start_toc(const char *path, const char *toc_path)
409 {
410         return lowlevel_in ? lowlevel_in->start_toc(path, toc_path) : -1;
411 }
412
413 int AudioDevice::start_record(int fd, int bsz)
414 {
415         return lowlevel_in ? lowlevel_in->start_record(fd, bsz) : -1;
416 }
417
418 int AudioDevice::stop_record()
419 {
420         return lowlevel_in ? lowlevel_in->stop_record() : -1;
421 }
422
423 int AudioDevice::total_audio_channels()
424 {
425         return lowlevel_in ? lowlevel_in->total_audio_channels() : 0;
426 }
427
428 DeviceMPEGInput *AudioDevice::mpeg_device()
429 {
430         return lowlevel_in ? lowlevel_in->mpeg_device() : 0;
431 }
432