r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / audiodevice.C
blob556b71ed1a5c83839a66212edeab4ad94962e51b
1 #include "audio1394.h"
2 #include "audioconfig.h"
3 #include "audiodevice.h"
4 #include "audioalsa.h"
5 #include "audioesound.h"
6 #include "audiooss.h"
7 #include "dcoffset.h"
8 #include "playbackconfig.h"
9 #include "preferences.h"
10 #include "recordconfig.h"
15 AudioLowLevel::AudioLowLevel(AudioDevice *device)
17         this->device = device;
20 AudioLowLevel::~AudioLowLevel()
29 AudioDevice::AudioDevice()
30  : Thread(1, 0, 0)
32         initialize();
33         this->out_config = new AudioOutConfig(0, 0, 0);
34         this->in_config = new AudioInConfig;
37 AudioDevice::~AudioDevice()
39         delete out_config;
40         delete in_config;
43 int AudioDevice::initialize()
45         record_before_play = 0;
46         r = w = d = 0;
48         for(int i = 0; i < TOTAL_BUFFERS; i++)
49         {
50                 buffer[i] = 0;
51                 buffer_size[i] = 0;
52                 play_mutex[i].lock();
53                 last_buffer[i] = 0;
54         }
56         input_buffer = 0;
57         duplex_init = 0;
58         rec_dither = play_dither = 0;
59         software_position_info = 0;
60         arm_buffer_num = 0;
61         is_playing_back = 0;
62         is_recording = 0;
63         last_buffer_size = total_samples = position_correction = 0;
64         last_position = 0;
65         dc_offset_thread = new DC_Offset;
66         interrupt = 0;
67         lowlevel_in = lowlevel_out = lowlevel_duplex = 0;
68         vdevice = 0;
69         sharing = 0;
70         total_samples_read = 0;
71         out_realtime = 0;
72         duplex_realtime = 0;
73         return 0;
76 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver)
78         this->driver = driver;
80         if(!lowlevel)
81         {
82                 switch(driver)
83                 {
84 #ifdef HAVE_OSS
85                         case AUDIO_OSS:
86                         case AUDIO_OSS_ENVY24:
87                                 lowlevel = new AudioOSS(this);
88                                 break;
89 #endif
91 #ifdef HAVE_ESOUND
92                         case AUDIO_ESOUND:
93                                 lowlevel = new AudioESound(this);
94                                 break;
95 #endif
96                         case AUDIO_NAS:
97                                 break;
99 #ifdef HAVE_ALSA
100                         case AUDIO_ALSA:
101                                 lowlevel = new AudioALSA(this);
102                                 break;
103 #endif
105 #ifdef HAVE_FIREWIRE    
106                         case AUDIO_1394:
107                                 lowlevel = new Audio1394(this);
108                                 break;
109 #endif
110                 }
111         }
112         return 0;
115 int AudioDevice::open_input(AudioInConfig *config, int rate, int samples)
117         r = 1;
118         duplex_init = 0;
119         *this->in_config = *config;
120 //printf("AudioDevice::open_input %s\n", this->in_config->oss_in_device[0]);
121         in_samplerate = rate;
122         in_samples = samples;
123         create_lowlevel(lowlevel_in, config->driver);
124         lowlevel_in->open_input();
125         record_timer.update();
126         return 0;
129 int AudioDevice::open_output(AudioOutConfig *config, int rate, int samples, int realtime)
131         w = 1;
132         duplex_init = 0;
133         *this->out_config = *config;
134         out_samplerate = rate;
135         out_samples = samples;
136         out_realtime = realtime;
137         create_lowlevel(lowlevel_out, config->driver);
138         lowlevel_out->open_output();
139         return 0;
142 int AudioDevice::open_duplex(AudioOutConfig *out_config, int rate, int samples, int realtime)
144         d = 1;
145         duplex_init = 1;        // notify playback routines to test the duplex lock
146         *this->out_config = *out_config;
147         duplex_samplerate = rate;
148         duplex_samples = samples;
149         duplex_channels = 0;
150         duplex_realtime = realtime;
151         for(int i = 0; i < MAXDEVICES; i++)
152         {
153                 duplex_channels += out_config->oss_out_channels[i];
154         }
155         duplex_lock.lock();     // prevent playback until recording starts
156         create_lowlevel(lowlevel_duplex, out_config->driver);
157         lowlevel_duplex->open_duplex();
158         playback_timer.update();
159         record_timer.update();
160         return 0;
164 int AudioDevice::interrupt_crash()
166         if(lowlevel_in) return lowlevel_in->interrupt_crash();
167         return 0;
171 int AudioDevice::close_all()
173         if(lowlevel_in) lowlevel_in->close_all();
174         if(lowlevel_out) lowlevel_out->close_all();
175         if(lowlevel_duplex) lowlevel_duplex->close_all();
177         reset_output();
178         if(input_buffer) 
179         {
180                 delete [] input_buffer; 
181                 input_buffer = 0; 
182         }
183         
184         is_recording = 0;
185         rec_dither = play_dither = 0;
186         software_position_info = position_correction = last_buffer_size = 0;
187         r = w = d = 0;
188         duplex_init = 0;
189         vdevice = 0;
190         sharing = 0;
192         if(lowlevel_in)
193         {
194                 delete lowlevel_in;
195                 lowlevel_in = 0;
196         }
197         if(lowlevel_out)
198         {
199                 delete lowlevel_out;
200                 lowlevel_out = 0;
201         }
202         if(lowlevel_duplex)
203         {
204                 delete lowlevel_duplex;
205                 lowlevel_duplex = 0;
206         }
207         return 0;
210 int AudioDevice::set_vdevice(VideoDevice *vdevice)
212         this->vdevice = vdevice;
213         return 0;
217 int AudioDevice::get_ichannels()
219         if(r) return in_channels;
220         else if(d) return duplex_channels;
221         else return 0;
224 int AudioDevice::get_ibits()
226         if(r) return in_bits;
227         else if(d) return duplex_bits;
228         return 0;
232 int AudioDevice::get_obits()
234         if(w) return out_bits;
235         else if(d) return duplex_bits;
236         return 0;
239 int AudioDevice::get_ochannels()
241         if(w) return out_channels;
242         else if(d) return duplex_channels;
243         return 0;
246 AudioLowLevel* AudioDevice::get_lowlevel_out()
248         if(w) return lowlevel_out;
249         else if(d) return lowlevel_duplex;
250         return 0;
253 AudioLowLevel* AudioDevice::get_lowlevel_in()
255         if(r) return lowlevel_in;
256         else if(d) return lowlevel_duplex;
257         return 0;
260 int AudioDevice::get_irate()
262         if(r) return in_samplerate;
263         else
264         if(d) return duplex_samplerate;
267 int AudioDevice::get_orealtime()
269         if(w) return out_realtime;
270         else if(d) return duplex_realtime;
271         return 0;
275 int AudioDevice::get_orate()
277         if(w) return out_samplerate;
278         else if(d) return duplex_samplerate;
279         return 0;
282 int AudioDevice::get_interrupted()
284         return interrupt;
287 int AudioDevice::get_device_buffer()
289         return device_buffer;