r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / cinelerra / audioalsa.C
blobfcec2b6c5eca967b0e0facbaf7cdb6af0c0631d2
1 #include "audiodevice.h"
2 #include "audioalsa.h"
3 #include "bcsignals.h"
4 #include "mutex.h"
5 #include "playbackconfig.h"
6 #include "preferences.h"
7 #include "recordconfig.h"
9 #include <errno.h>
11 #ifdef HAVE_ALSA
13 AudioALSA::AudioALSA(AudioDevice *device)
14  : AudioLowLevel(device)
16         samples_written = 0;
17         timer = new Timer;
18         delay = 0;
19         timer_lock = new Mutex("AudioALSA::timer_lock");
20         interrupted = 0;
21         dsp_out = 0;
24 AudioALSA::~AudioALSA()
26         delete timer_lock;
27         delete timer;
30 void AudioALSA::list_devices(ArrayList<char*> *devices, int pcm_title, int mode)
32         snd_ctl_t *handle;
33         int card, err, dev, idx;
34         snd_ctl_card_info_t *info;
35         snd_pcm_info_t *pcminfo;
36         char string[BCTEXTLEN];
37         snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
38         int error;
39         switch(mode)
40         {
41                 case MODERECORD:
42                         stream = SND_PCM_STREAM_CAPTURE;
43                         break;
44                 case MODEPLAY:
45                         stream = SND_PCM_STREAM_PLAYBACK;
46                         break;
47         }
49         snd_ctl_card_info_alloca(&info);
50         snd_pcm_info_alloca(&pcminfo);
52         card = -1;
53 #define DEFAULT_DEVICE "default"
54         char *result = new char[strlen(DEFAULT_DEVICE) + 1];
55         devices->append(result);
56         devices->set_array_delete();     // since we are allocating by new[]
57         strcpy(result, DEFAULT_DEVICE);
59         while(snd_card_next(&card) >= 0)
60         {
61                 char name[BCTEXTLEN];
62                 if(card < 0) break;
63                 sprintf(name, "hw:%i", card);
65                 if((err = snd_ctl_open(&handle, name, 0)) < 0)
66                 {
67                         printf("AudioALSA::list_devices card=%i: %s\n", card, snd_strerror(err));
68                         continue;
69                 }
71                 if((err = snd_ctl_card_info(handle, info)) < 0)
72                 {
73                         printf("AudioALSA::list_devices card=%i: %s\n", card, snd_strerror(err));
74                         snd_ctl_close(handle);
75                         continue;
76                 }
78                 dev = -1;
80                 while(1)
81                 {
82                         unsigned int count;
83                         if(snd_ctl_pcm_next_device(handle, &dev) < 0)
84                                 printf("AudioALSA::list_devices: snd_ctl_pcm_next_device\n");
86                         if (dev < 0)
87                                 break;
89                         snd_pcm_info_set_device(pcminfo, dev);
90                         snd_pcm_info_set_subdevice(pcminfo, 0);
91                         snd_pcm_info_set_stream(pcminfo, stream);
93                         if((err = snd_ctl_pcm_info(handle, pcminfo)) < 0) 
94                         {
95                                 if(err != -ENOENT)
96                                         printf("AudioALSA::list_devices card=%i: %s\n", card, snd_strerror(err));
97                                 continue;
98                         }
100                         if(pcm_title)
101                         {
102                                 sprintf(string, "plughw:%d,%d", card, dev);
103 //                              strcpy(string, "cards.pcm.front");
104                         }
105                         else
106                         {
107                                 sprintf(string, "%s #%d", 
108                                         snd_ctl_card_info_get_name(info), 
109                                         dev);
110                         }
112                         char *result = devices->append(new char[strlen(string) + 1]);
113                         strcpy(result, string);
114                 }
116                 snd_ctl_close(handle);
117         }
119 //      snd_ctl_card_info_free(info);
120 //      snd_pcm_info_free(pcminfo);
123 void AudioALSA::translate_name(char *output, char *input)
125         ArrayList<char*> titles;
126         ArrayList<char*> pcm_titles;
127         int mode;
128         if(device->r) mode = MODERECORD;
129         else
130         if(device->w) mode = MODEPLAY;
131         
132         list_devices(&titles, 0, mode);
133         list_devices(&pcm_titles, 1, mode);
135         sprintf(output, "default");     
136         for(int i = 0; i < titles.total; i++)
137         {
138 //printf("AudioALSA::translate_name %s %s\n", titles.values[i], pcm_titles.values[i]);
139                 if(!strcasecmp(titles.values[i], input))
140                 {
141                         strcpy(output, pcm_titles.values[i]);
142                         break;
143                 }
144         }
145         
146         titles.remove_all_objects();
147         pcm_titles.remove_all_objects();
150 snd_pcm_format_t AudioALSA::translate_format(int format)
152         switch(format)
153         {
154                 case 8:
155                         return SND_PCM_FORMAT_S8;
156                         break;
157                 case 16:
158                         return SND_PCM_FORMAT_S16_LE;
159                         break;
160                 case 24:
161                         return SND_PCM_FORMAT_S24_LE;
162                         break;
163                 case 32:
164                         return SND_PCM_FORMAT_S32_LE;
165                         break;
166         }
169 void AudioALSA::set_params(snd_pcm_t *dsp, 
170         int channels, 
171         int bits,
172         int samplerate,
173         int samples)
175         snd_pcm_hw_params_t *params;
176         snd_pcm_sw_params_t *swparams;
177         int err;
179         snd_pcm_hw_params_alloca(&params);
180         snd_pcm_sw_params_alloca(&swparams);
181         err = snd_pcm_hw_params_any(dsp, params);
183         if (err < 0) 
184         {
185                 printf("AudioALSA::set_params: no PCM configurations available\n");
186                 return;
187         }
189         snd_pcm_hw_params_set_access(dsp, 
190                 params,
191                 SND_PCM_ACCESS_RW_INTERLEAVED);
192         snd_pcm_hw_params_set_format(dsp, 
193                 params, 
194                 translate_format(bits));
195         snd_pcm_hw_params_set_channels(dsp, 
196                 params, 
197                 channels);
198         snd_pcm_hw_params_set_rate_near(dsp, 
199                 params, 
200                 (unsigned int*)&samplerate, 
201                 (int*)0);
203 // Buffers written must be equal to period_time
204         int buffer_time;
205         int period_time;
206         if(device->r)
207         {
208                 buffer_time = 10000000;
209                 period_time = (int)((int64_t)samples * 1000000 / samplerate);
210         }
211         else
212         {
213                 buffer_time = (int)((int64_t)samples * 1000000 * 2 / samplerate + 0.5);
214                 period_time = samples * samplerate / 1000000;
215         }
218 //printf("AudioALSA::set_params 1 %d %d %d\n", samples, buffer_time, period_time);
219         snd_pcm_hw_params_set_buffer_time_near(dsp, 
220                 params,
221                 (unsigned int*)&buffer_time, 
222                 (int*)0);
223         snd_pcm_hw_params_set_period_time_near(dsp, 
224                 params,
225                 (unsigned int*)&period_time, 
226                 (int*)0);
227 //printf("AudioALSA::set_params 5 %d %d\n", buffer_time, period_time);
228         err = snd_pcm_hw_params(dsp, params);
229         if(err < 0)
230         {
231                 printf("AudioALSA::set_params: hw_params failed\n");
232                 return;
233         }
235         snd_pcm_uframes_t chunk_size = 1024;
236         snd_pcm_uframes_t buffer_size = 262144;
237         snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
238         snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
239 //printf("AudioALSA::set_params 10 %d %d\n", chunk_size, buffer_size);
241         snd_pcm_sw_params_current(dsp, swparams);
242         size_t xfer_align = 1 /* snd_pcm_sw_params_get_xfer_align(swparams) */;
243         unsigned int sleep_min = 0;
244         err = snd_pcm_sw_params_set_sleep_min(dsp, swparams, sleep_min);
245         int n = chunk_size;
246         err = snd_pcm_sw_params_set_avail_min(dsp, swparams, n);
247         err = snd_pcm_sw_params_set_xfer_align(dsp, swparams, xfer_align);
248         if(snd_pcm_sw_params(dsp, swparams) < 0)
249         {
250                 printf("AudioALSA::set_params: snd_pcm_sw_params failed\n");
251         }
253         device->device_buffer = samples * bits / 8 * channels;
255 //printf("AudioALSA::set_params 100 %d %d\n", samples,  device->device_buffer);
257 //      snd_pcm_hw_params_free(params);
258 //      snd_pcm_sw_params_free(swparams);
261 int AudioALSA::open_input()
263         char pcm_name[BCTEXTLEN];
264         snd_pcm_stream_t stream = SND_PCM_STREAM_CAPTURE;
265         int open_mode = 0;
266         int err;
268         device->in_channels = device->get_ichannels();
269         device->in_bits = device->in_config->alsa_in_bits;
271         translate_name(pcm_name, device->in_config->alsa_in_device);
272 //printf("AudioALSA::open_input %s\n", pcm_name);
274         err = snd_pcm_open(&dsp_in, pcm_name, stream, open_mode);
276         if(err < 0)
277         {
278                 printf("AudioALSA::open_input: %s\n", snd_strerror(err));
279                 return 1;
280         }
282         set_params(dsp_in, 
283                 device->get_ichannels(), 
284                 device->in_config->alsa_in_bits,
285                 device->in_samplerate,
286                 device->in_samples);
288         return 0;
291 int AudioALSA::open_output()
293         char pcm_name[BCTEXTLEN];
294         snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
295         int open_mode = 0;
296         int err;
298         device->out_channels = device->get_ochannels();
299         device->out_bits = device->out_config->alsa_out_bits;
301         translate_name(pcm_name, device->out_config->alsa_out_device);
303         err = snd_pcm_open(&dsp_out, pcm_name, stream, open_mode);
305         if(err < 0)
306         {
307                 dsp_out = 0;
308                 printf("AudioALSA::open_output %s: %s\n", pcm_name, snd_strerror(err));
309                 return 1;
310         }
312         set_params(dsp_out, 
313                 device->get_ochannels(), 
314                 device->out_config->alsa_out_bits,
315                 device->out_samplerate,
316                 device->out_samples);
317         timer->update();
318         return 0;
321 int AudioALSA::open_duplex()
323 // ALSA always opens 2 devices
324         return 0;
327 int AudioALSA::close_output()
329         if(device->w && dsp_out)
330         {
331                 snd_pcm_close(dsp_out);
332         }
333         return 0;
336 int AudioALSA::close_input()
338         if(device->r)
339         {
340 //              snd_pcm_reset(dsp_in);
341                 snd_pcm_drop(dsp_in);
342                 snd_pcm_drain(dsp_in);
343                 snd_pcm_close(dsp_in);
344         }
345         return 0;
348 int AudioALSA::close_all()
350         close_input();
351         close_output();
352         if(device->d)
353         {
354                 snd_pcm_close(dsp_duplex);
355         }
356         samples_written = 0;
357         delay = 0;
358         interrupted = 0;
361 // Undocumented
362 int64_t AudioALSA::device_position()
364         timer_lock->lock("AudioALSA::device_position");
365         int64_t result = samples_written + 
366                 timer->get_scaled_difference(device->out_samplerate) - 
367                 delay;
368 // printf("AudioALSA::device_position 1 %lld %lld %d %lld\n", 
369 // samples_written,
370 // timer->get_scaled_difference(device->out_samplerate),
371 // delay,
372 // samples_written + timer->get_scaled_difference(device->out_samplerate) - delay);
373         timer_lock->unlock();
374         return result;
377 int AudioALSA::read_buffer(char *buffer, int size)
379 //printf("AudioALSA::read_buffer 1\n");
380         int attempts = 0;
381         int done = 0;
383         if(!get_input())
384         {
385                 sleep(1);
386                 return 0;
387         }
389         while(attempts < 1 && !done)
390         {
391                 if(snd_pcm_readi(get_input(), 
392                         buffer, 
393                         size / (device->in_bits / 8) / device->get_ichannels()) < 0)
394                 {
395                         printf("AudioALSA::read_buffer overrun at sample %lld\n", 
396                                 device->total_samples_read);
397 //                      snd_pcm_resume(get_input());
398                         close_input();
399                         open_input();
400                         attempts++;
401                 }
402                 else
403                         done = 1;
404         }
405         return 0;
408 int AudioALSA::write_buffer(char *buffer, int size)
410 // Don't give up and drop the buffer on the first error.
411         int attempts = 0;
412         int done = 0;
413         int samples = size / (device->out_bits / 8) / device->get_ochannels();
415         if(!get_output()) return 0;
417         while(attempts < 2 && !done && !interrupted)
418         {
419 // Buffers written must be equal to period_time
420 // Update timing
421                 snd_pcm_sframes_t delay;
422                 snd_pcm_delay(get_output(), &delay);
423                 snd_pcm_avail_update(get_output());
425                 device->Thread::enable_cancel();
426                 if(snd_pcm_writei(get_output(), 
427                         buffer, 
428                         samples) < 0)
429                 {
430                         device->Thread::disable_cancel();
431                         printf("AudioALSA::write_buffer underrun at sample %lld\n",
432                                 device->current_position());
433 //                      snd_pcm_resume(get_output());
434                         close_output();
435                         open_output();
436                         attempts++;
437                 }
438                 else
439                 {
440                         device->Thread::disable_cancel();
441                         done = 1;
442                 }
443         }
445         if(done)
446         {
447                 timer_lock->lock("AudioALSA::write_buffer");
448                 this->delay = delay;
449                 timer->update();
450                 samples_written += samples;
451                 timer_lock->unlock();
452         }
453         return 0;
456 int AudioALSA::flush_device()
458         if(get_output()) snd_pcm_drain(get_output());
459         return 0;
462 int AudioALSA::interrupt_playback()
464         if(get_output()) 
465         {
466                 interrupted = 1;
467 // Interrupts the playback but may not have caused snd_pcm_writei to exit.
468 // With some soundcards it causes snd_pcm_writei to freeze for a few seconds.
469                 if(!device->out_config->interrupt_workaround)
470                         snd_pcm_drop(get_output());
472 // Makes sure the current buffer finishes before stopping.
473 //              snd_pcm_drain(get_output());
475 // The only way to ensure snd_pcm_writei exits, but
476 // got a lot of crashes when doing this.
477 //              device->Thread::cancel();
478         }
479         return 0;
483 snd_pcm_t* AudioALSA::get_output()
485         if(device->w) return dsp_out;
486         else
487         if(device->d) return dsp_duplex;
488         return 0;
491 snd_pcm_t* AudioALSA::get_input()
493         if(device->r) return dsp_in;
494         else
495         if(device->d) return dsp_duplex;
496         return 0;
499 #endif