1 #include "audiodevice.h"
4 #include "playbackconfig.h"
5 #include "preferences.h"
6 #include "recordconfig.h"
12 AudioALSA::AudioALSA(AudioDevice *device)
13 : AudioLowLevel(device)
18 timer_lock = new Mutex("AudioALSA::timer_lock");
22 AudioALSA::~AudioALSA()
28 void AudioALSA::list_devices(ArrayList<char*> *devices, int pcm_title)
31 int card, err, dev, idx;
32 snd_ctl_card_info_t *info;
33 snd_pcm_info_t *pcminfo;
34 char string[BCTEXTLEN];
35 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
38 snd_ctl_card_info_alloca(&info);
39 snd_pcm_info_alloca(&pcminfo);
42 #define DEFAULT_DEVICE "default"
43 char *result = new char[strlen(DEFAULT_DEVICE) + 1];
44 devices->append(result);
45 devices->set_array_delete(); // since we are allocating by new[]
46 strcpy(result, DEFAULT_DEVICE);
48 while(snd_card_next(&card) >= 0)
52 sprintf(name, "hw:%i", card);
54 if((err = snd_ctl_open(&handle, name, 0)) < 0)
56 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
60 if((err = snd_ctl_card_info(handle, info)) < 0)
62 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
63 snd_ctl_close(handle);
72 if(snd_ctl_pcm_next_device(handle, &dev) < 0)
73 printf("AudioALSA::list_devices: snd_ctl_pcm_next_device\n");
78 snd_pcm_info_set_device(pcminfo, dev);
79 snd_pcm_info_set_subdevice(pcminfo, 0);
80 snd_pcm_info_set_stream(pcminfo, stream);
82 if((err = snd_ctl_pcm_info(handle, pcminfo)) < 0)
85 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
91 sprintf(string, "plughw:%d,%d", card, dev);
92 // strcpy(string, "cards.pcm.front");
96 sprintf(string, "%s #%d",
97 snd_ctl_card_info_get_name(info),
101 char *result = devices->append(new char[strlen(string) + 1]);
102 strcpy(result, string);
105 snd_ctl_close(handle);
108 // snd_ctl_card_info_free(info);
109 // snd_pcm_info_free(pcminfo);
112 void AudioALSA::translate_name(char *output, char *input)
114 ArrayList<char*> titles;
115 ArrayList<char*> pcm_titles;
117 list_devices(&titles, 0);
118 list_devices(&pcm_titles, 1);
120 sprintf(output, "default");
121 for(int i = 0; i < titles.total; i++)
123 //printf("AudioALSA::translate_name %s %s\n", titles.values[i], pcm_titles.values[i]);
124 if(!strcasecmp(titles.values[i], input))
126 strcpy(output, pcm_titles.values[i]);
131 titles.remove_all_objects();
132 pcm_titles.remove_all_objects();
135 snd_pcm_format_t AudioALSA::translate_format(int format)
140 return SND_PCM_FORMAT_S8;
143 return SND_PCM_FORMAT_S16_LE;
146 return SND_PCM_FORMAT_S24_LE;
149 return SND_PCM_FORMAT_S32_LE;
154 void AudioALSA::set_params(snd_pcm_t *dsp,
160 snd_pcm_hw_params_t *params;
161 snd_pcm_sw_params_t *swparams;
164 snd_pcm_hw_params_alloca(¶ms);
165 snd_pcm_sw_params_alloca(&swparams);
166 err = snd_pcm_hw_params_any(dsp, params);
170 printf("AudioALSA::set_params: no PCM configurations available\n");
174 snd_pcm_hw_params_set_access(dsp,
176 SND_PCM_ACCESS_RW_INTERLEAVED);
177 snd_pcm_hw_params_set_format(dsp,
179 translate_format(bits));
180 snd_pcm_hw_params_set_channels(dsp,
183 snd_pcm_hw_params_set_rate_near(dsp,
185 (unsigned int*)&samplerate,
188 // Buffers written must be equal to period_time
193 buffer_time = 10000000;
194 period_time = (int)((int64_t)samples * 1000000 / samplerate);
198 buffer_time = (int)((int64_t)samples * 1000000 * 2 / samplerate + 0.5);
199 period_time = samples * samplerate / 1000000;
203 //printf("AudioALSA::set_params 1 %d %d %d\n", samples, buffer_time, period_time);
204 snd_pcm_hw_params_set_buffer_time_near(dsp,
206 (unsigned int*)&buffer_time,
208 snd_pcm_hw_params_set_period_time_near(dsp,
210 (unsigned int*)&period_time,
212 //printf("AudioALSA::set_params 5 %d %d\n", buffer_time, period_time);
213 err = snd_pcm_hw_params(dsp, params);
216 printf("AudioALSA::set_params: hw_params failed\n");
220 snd_pcm_uframes_t chunk_size = 1024;
221 snd_pcm_uframes_t buffer_size = 262144;
222 snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
223 snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
224 //printf("AudioALSA::set_params 10 %d %d\n", chunk_size, buffer_size);
226 snd_pcm_sw_params_current(dsp, swparams);
227 size_t xfer_align = 1 /* snd_pcm_sw_params_get_xfer_align(swparams) */;
228 unsigned int sleep_min = 0;
229 err = snd_pcm_sw_params_set_sleep_min(dsp, swparams, sleep_min);
231 err = snd_pcm_sw_params_set_avail_min(dsp, swparams, n);
232 err = snd_pcm_sw_params_set_xfer_align(dsp, swparams, xfer_align);
233 if(snd_pcm_sw_params(dsp, swparams) < 0)
235 printf("AudioALSA::set_params: snd_pcm_sw_params failed\n");
238 device->device_buffer = samples * bits / 8 * channels;
240 //printf("AudioALSA::set_params 100 %d %d\n", samples, device->device_buffer);
242 // snd_pcm_hw_params_free(params);
243 // snd_pcm_sw_params_free(swparams);
246 int AudioALSA::open_input()
248 char pcm_name[BCTEXTLEN];
249 snd_pcm_stream_t stream = SND_PCM_STREAM_CAPTURE;
253 device->in_channels = device->in_config->alsa_in_channels;
254 device->in_bits = device->in_config->alsa_in_bits;
256 translate_name(pcm_name, device->in_config->alsa_in_device);
258 err = snd_pcm_open(&dsp_in, pcm_name, stream, open_mode);
262 printf("AudioALSA::open_input: %s\n", snd_strerror(err));
267 device->in_config->alsa_in_channels,
268 device->in_config->alsa_in_bits,
269 device->in_samplerate,
275 int AudioALSA::open_output()
277 char pcm_name[BCTEXTLEN];
278 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
282 device->out_channels = device->out_config->alsa_out_channels;
283 device->out_bits = device->out_config->alsa_out_bits;
285 translate_name(pcm_name, device->out_config->alsa_out_device);
287 err = snd_pcm_open(&dsp_out, pcm_name, stream, open_mode);
291 printf("AudioALSA::open_output %s: %s\n", pcm_name, snd_strerror(err));
296 device->out_config->alsa_out_channels,
297 device->out_config->alsa_out_bits,
298 device->out_samplerate,
299 device->out_samples);
304 int AudioALSA::open_duplex()
306 // ALSA always opens 2 devices
310 int AudioALSA::close_output()
314 snd_pcm_close(dsp_out);
319 int AudioALSA::close_input()
323 // snd_pcm_reset(dsp_in);
324 snd_pcm_drop(dsp_in);
325 snd_pcm_drain(dsp_in);
326 snd_pcm_close(dsp_in);
331 int AudioALSA::close_all()
337 snd_pcm_close(dsp_duplex);
345 int64_t AudioALSA::device_position()
347 timer_lock->lock("AudioALSA::device_position");
348 int64_t result = samples_written +
349 timer->get_scaled_difference(device->out_samplerate) -
351 // printf("AudioALSA::device_position 1 %lld %lld %d %lld\n",
353 // timer->get_scaled_difference(device->out_samplerate),
355 // samples_written + timer->get_scaled_difference(device->out_samplerate) - delay);
356 timer_lock->unlock();
360 int AudioALSA::read_buffer(char *buffer, int size)
362 //printf("AudioALSA::read_buffer 1\n");
365 while(attempts < 1 && !done)
367 if(snd_pcm_readi(get_input(),
369 size / (device->in_bits / 8) / device->in_channels) < 0)
371 printf("AudioALSA::read_buffer overrun at sample %lld\n",
372 device->total_samples_read);
373 // snd_pcm_resume(get_input());
384 int AudioALSA::write_buffer(char *buffer, int size)
386 // Don't give up and drop the buffer on the first error.
389 int samples = size / (device->out_bits / 8) / device->out_channels;
390 while(attempts < 2 && !done && !interrupted)
392 // Buffers written must be equal to period_time
394 snd_pcm_sframes_t delay;
395 snd_pcm_delay(get_output(), &delay);
396 snd_pcm_avail_update(get_output());
398 device->Thread::enable_cancel();
399 if(snd_pcm_writei(get_output(),
403 device->Thread::disable_cancel();
404 printf("AudioALSA::write_buffer underrun at sample %lld\n",
405 device->current_position());
406 // snd_pcm_resume(get_output());
413 device->Thread::disable_cancel();
420 timer_lock->lock("AudioALSA::write_buffer");
423 samples_written += samples;
424 timer_lock->unlock();
429 int AudioALSA::flush_device()
431 if(get_output()) snd_pcm_drain(get_output());
435 int AudioALSA::interrupt_playback()
440 // Interrupts the playback but may not have caused snd_pcm_writei to exit.
441 // With some soundcards it causes snd_pcm_writei to freeze for a few seconds.
442 if(!device->out_config->interrupt_workaround)
443 snd_pcm_drop(get_output());
445 // Makes sure the current buffer finishes before stopping.
446 // snd_pcm_drain(get_output());
448 // The only way to ensure snd_pcm_writei exits, but
449 // got a lot of crashes when doing this.
450 // device->Thread::cancel();
456 snd_pcm_t* AudioALSA::get_output()
458 if(device->w) return dsp_out;
460 if(device->d) return dsp_duplex;
464 snd_pcm_t* AudioALSA::get_input()
466 if(device->r) return dsp_in;
468 if(device->d) return dsp_duplex;