Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / audiodevice.C
blobaa1fd3d98b72b6aa60ddc36bc4699d18ca2c82bb
1 #ifdef HAVE_FIREWIRE
2 #include "audio1394.h"
3 #endif
4 #include "audioalsa.h"
5 #include "audiocine.h"
6 #include "audiodevice.h"
7 #include "audiodvb.h"
8 #include "audioesound.h"
9 #include "audiooss.h"
10 #include "bctimer.h"
11 #include "condition.h"
12 #include "dcoffset.h"
13 #include "mutex.h"
14 #include "playbackconfig.h"
15 #include "preferences.h"
16 #include "recordconfig.h"
17 #include "sema.h"
20 AudioLowLevel::AudioLowLevel(AudioDevice *device)
22         this->device = device;
25 AudioLowLevel::~AudioLowLevel()
34 AudioDevice::AudioDevice(MWindow *mwindow)
35  : Thread(1, 0, 0)
37         initialize();
38         this->mwindow = mwindow;
39         this->out_config = new AudioOutConfig(0);
40         this->in_config = new AudioInConfig;
41         this->vconfig = new VideoInConfig;
42         startup_lock = new Condition(0, "AudioDevice::startup_lock");
43         duplex_lock = new Condition(0, "AudioDevice::duplex_lock");
44         timer_lock = new Mutex("AudioDevice::timer_lock");
45         buffer_lock = new Mutex("AudioDevice::buffer_lock");
46         polling_lock = new Condition(0, "AudioDevice::polling_lock");
47         playback_timer = new Timer;
48         record_timer = new Timer;
49         for(int i = 0; i < TOTAL_BUFFERS; i++)
50         {
51                 play_lock[i] = new Sema(0, "AudioDevice::play_lock");
52                 arm_lock[i] = new Sema(1, "AudioDevice::arm_lock");
53         }
56 AudioDevice::~AudioDevice()
58         delete out_config;
59         delete in_config;
60         delete vconfig;
61         delete startup_lock;
62         delete duplex_lock;
63         delete timer_lock;
64         for(int i = 0; i < TOTAL_BUFFERS; i++)
65         {
66                 delete play_lock[i];
67                 delete arm_lock[i];
68         }
69         delete playback_timer;
70         delete record_timer;
71         delete buffer_lock;
72         delete polling_lock;
75 int AudioDevice::initialize()
77         record_before_play = 0;
78         r = w = d = 0;
80         for(int i = 0; i < TOTAL_BUFFERS; i++)
81         {
82                 output_buffer[i] = 0;
83                 input_buffer[i] = 0;
84                 buffer_size[i] = 0;
85                 last_buffer[i] = 0;
86         }
88         duplex_init = 0;
89         rec_dither = play_dither = 0;
90         software_position_info = 0;
91         arm_buffer_num = 0;
92         is_playing_back = 0;
93         is_recording = 0;
94         last_buffer_size = 0;
95         total_samples = 0;
96         position_correction = 0;
97         last_position = 0;
98         interrupt = 0;
99         lowlevel_in = lowlevel_out = lowlevel_duplex = 0;
100         vdevice = 0;
101         sharing = 0;
102         total_samples_read = 0;
103         out_realtime = 0;
104         duplex_realtime = 0;
105         in_realtime = 0;
106         read_waiting = 0; 
107         return 0;
110 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver)
112         this->driver = driver;
115         if(!lowlevel)
116         {
117                 switch(driver)
118                 {
119 #ifdef HAVE_OSS
120                         case AUDIO_OSS:
121                         case AUDIO_OSS_ENVY24:
122                                 lowlevel = new AudioOSS(this);
123                                 break;
124 #endif
126 #ifdef HAVE_ESOUND
127                         case AUDIO_ESOUND:
128                                 lowlevel = new AudioESound(this);
129                                 break;
130 #endif
131                         case AUDIO_NAS:
132                                 break;
134 #ifdef HAVE_ALSA
135                         case AUDIO_ALSA:
136                                 lowlevel = new AudioALSA(this);
137                                 break;
138 #endif
140 #ifdef HAVE_FIREWIRE    
141                         case AUDIO_1394:
142                         case AUDIO_DV1394:
143                         case AUDIO_IEC61883:
144                                 lowlevel = new Audio1394(this);
145                                 break;
146 #endif
150                         case AUDIO_DVB:
151                                 lowlevel = new AudioDVB(this);
152                                 break;
156                         case AUDIO_CINE:
157                                 lowlevel = new AudioCine(this);
158                                 break;
159                 }
160         }
161         return 0;
164 int AudioDevice::open_input(AudioInConfig *config, 
165         VideoInConfig *vconfig, 
166         int rate, 
167         int samples,
168         int channels,
169         int realtime)
171         r = 1;
172         duplex_init = 0;
173         this->in_config->copy_from(config);
174         this->vconfig->copy_from(vconfig);
175         in_samplerate = rate;
176         in_samples = samples;
177         in_realtime = realtime;
178         in_channels = channels;
179         create_lowlevel(lowlevel_in, config->driver);
180         lowlevel_in->open_input();
181         record_timer->update();
182         return 0;
185 int AudioDevice::open_output(AudioOutConfig *config, 
186         int rate, 
187         int samples, 
188         int channels,
189         int realtime)
191         w = 1;
192         duplex_init = 0;
193         *this->out_config = *config;
194         out_samplerate = rate;
195         out_samples = samples;
196         out_channels = channels;
197         out_realtime = realtime;
198         create_lowlevel(lowlevel_out, config->driver);
199         return lowlevel_out ? lowlevel_out->open_output() : 0;
203 int AudioDevice::interrupt_crash()
205         if(lowlevel_in) return lowlevel_in->interrupt_crash();
206         return 0;
210 int AudioDevice::close_all()
212         if(is_recording)
213         {
214                 is_recording = 0;
215                 read_waiting = 1;
216                 Thread::join();
217         }
218         
220         if(lowlevel_in) lowlevel_in->close_all();
221         if(lowlevel_out) lowlevel_out->close_all();
222         if(lowlevel_duplex) lowlevel_duplex->close_all();
224         reset_output();
225         for(int i = 0; i < TOTAL_BUFFERS; i++)
226         {
227                 delete [] input_buffer[i]; 
228                 input_buffer[i] = 0;
229         }
230         
231         is_recording = 0;
232         rec_dither = play_dither = 0;
233         software_position_info = position_correction = last_buffer_size = 0;
234         r = w = d = 0;
235         duplex_init = 0;
236         vdevice = 0;
237         sharing = 0;
239         if(lowlevel_in)
240         {
241                 delete lowlevel_in;
242                 lowlevel_in = 0;
243         }
245         if(lowlevel_out)
246         {
247                 delete lowlevel_out;
248                 lowlevel_out = 0;
249         }
251         if(lowlevel_duplex)
252         {
253                 delete lowlevel_duplex;
254                 lowlevel_duplex = 0;
255         }
257         return 0;
260 int AudioDevice::set_vdevice(VideoDevice *vdevice)
262         this->vdevice = vdevice;
263         return 0;
267 int AudioDevice::get_ichannels()
269         if(r) return in_channels;
270         else if(d) return duplex_channels;
271         else return 0;
274 int AudioDevice::get_ibits()
276         if(r) return in_bits;
277         else if(d) return duplex_bits;
278         return 0;
282 int AudioDevice::get_obits()
284         if(w) return out_bits;
285         else if(d) return duplex_bits;
286         return 0;
289 int AudioDevice::get_ochannels()
291         if(w) return out_channels;
292         else if(d) return duplex_channels;
293         return 0;
296 AudioLowLevel* AudioDevice::get_lowlevel_out()
298         if(w) return lowlevel_out;
299         else if(d) return lowlevel_duplex;
300         return 0;
303 AudioLowLevel* AudioDevice::get_lowlevel_in()
305         if(r) return lowlevel_in;
306         else if(d) return lowlevel_duplex;
307         return 0;
310 int AudioDevice::get_irate()
312         if(r) return in_samplerate;
313         else
314         if(d) return duplex_samplerate;
317 int AudioDevice::get_orealtime()
319         if(w) return out_realtime;
320         else 
321         if(d) return duplex_realtime;
322         return 0;
325 int AudioDevice::get_irealtime()
327         if(r) return in_realtime;
328         else 
329         if(d) return duplex_realtime;
330         return 0;
334 int AudioDevice::get_orate()
336         if(w) return out_samplerate;
337         else if(d) return duplex_samplerate;
338         return 0;
341 int AudioDevice::get_interrupted()
343         return interrupt;
346 int AudioDevice::get_device_buffer()
348         return device_buffer;
361 void AudioDevice::run()
363         if(w)
364                 run_output();
365         else
366         if(r)
367                 run_input();
371 //      Local Variables:
372 //      mode: C++
373 //      c-file-style: "linux"
374 //      End: