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 strcpy(result, DEFAULT_DEVICE);
47 while(snd_card_next(&card) >= 0)
51 sprintf(name, "hw:%i", card);
53 if((err = snd_ctl_open(&handle, name, 0)) < 0)
55 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
59 if((err = snd_ctl_card_info(handle, info)) < 0)
61 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
62 snd_ctl_close(handle);
71 if(snd_ctl_pcm_next_device(handle, &dev) < 0)
72 printf("AudioALSA::list_devices: snd_ctl_pcm_next_device\n");
77 snd_pcm_info_set_device(pcminfo, dev);
78 snd_pcm_info_set_subdevice(pcminfo, 0);
79 snd_pcm_info_set_stream(pcminfo, stream);
81 if((err = snd_ctl_pcm_info(handle, pcminfo)) < 0)
84 printf("AudioALSA::list_devices (%i): %s\n", card, snd_strerror(err));
90 sprintf(string, "plughw:%d,%d", card, dev);
91 // strcpy(string, "cards.pcm.front");
95 sprintf(string, "%s #%d",
96 snd_ctl_card_info_get_name(info),
100 char *result = devices->append(new char[strlen(string) + 1]);
101 strcpy(result, string);
104 snd_ctl_close(handle);
107 // snd_ctl_card_info_free(info);
108 // snd_pcm_info_free(pcminfo);
111 void AudioALSA::translate_name(char *output, char *input)
113 ArrayList<char*> titles;
114 ArrayList<char*> pcm_titles;
116 list_devices(&titles, 0);
117 list_devices(&pcm_titles, 1);
119 sprintf(output, "default");
120 for(int i = 0; i < titles.total; i++)
122 //printf("AudioALSA::translate_name %s %s\n", titles.values[i], pcm_titles.values[i]);
123 if(!strcasecmp(titles.values[i], input))
125 strcpy(output, pcm_titles.values[i]);
130 titles.remove_all_objects();
131 pcm_titles.remove_all_objects();
134 snd_pcm_format_t AudioALSA::translate_format(int format)
139 return SND_PCM_FORMAT_S8;
142 return SND_PCM_FORMAT_S16_LE;
145 return SND_PCM_FORMAT_S24_LE;
148 return SND_PCM_FORMAT_S32_LE;
153 void AudioALSA::set_params(snd_pcm_t *dsp,
159 snd_pcm_hw_params_t *params;
160 snd_pcm_sw_params_t *swparams;
163 snd_pcm_hw_params_alloca(¶ms);
164 snd_pcm_sw_params_alloca(&swparams);
165 err = snd_pcm_hw_params_any(dsp, params);
169 printf("AudioALSA::set_params: no PCM configurations available\n");
173 snd_pcm_hw_params_set_access(dsp,
175 SND_PCM_ACCESS_RW_INTERLEAVED);
176 snd_pcm_hw_params_set_format(dsp,
178 translate_format(bits));
179 snd_pcm_hw_params_set_channels(dsp,
182 snd_pcm_hw_params_set_rate_near(dsp,
184 (unsigned int*)&samplerate,
187 // Buffers written must be equal to period_time
192 buffer_time = 10000000;
193 period_time = (int)((int64_t)samples * 1000000 / samplerate);
197 buffer_time = (int)((int64_t)samples * 1000000 * 2 / samplerate + 0.5);
198 period_time = samples * samplerate / 1000000;
202 //printf("AudioALSA::set_params 1 %d %d %d\n", samples, buffer_time, period_time);
203 snd_pcm_hw_params_set_buffer_time_near(dsp,
205 (unsigned int*)&buffer_time,
207 snd_pcm_hw_params_set_period_time_near(dsp,
209 (unsigned int*)&period_time,
211 //printf("AudioALSA::set_params 5 %d %d\n", buffer_time, period_time);
212 err = snd_pcm_hw_params(dsp, params);
215 printf("AudioALSA::set_params: hw_params failed\n");
219 snd_pcm_uframes_t chunk_size = 1024;
220 snd_pcm_uframes_t buffer_size = 262144;
221 snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
222 snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
223 //printf("AudioALSA::set_params 10 %d %d\n", chunk_size, buffer_size);
225 snd_pcm_sw_params_current(dsp, swparams);
226 size_t xfer_align = 1 /* snd_pcm_sw_params_get_xfer_align(swparams) */;
227 unsigned int sleep_min = 0;
228 err = snd_pcm_sw_params_set_sleep_min(dsp, swparams, sleep_min);
230 err = snd_pcm_sw_params_set_avail_min(dsp, swparams, n);
231 err = snd_pcm_sw_params_set_xfer_align(dsp, swparams, xfer_align);
232 if(snd_pcm_sw_params(dsp, swparams) < 0)
234 printf("AudioALSA::set_params: snd_pcm_sw_params failed\n");
237 device->device_buffer = samples * bits / 8 * channels;
239 //printf("AudioALSA::set_params 100 %d %d\n", samples, device->device_buffer);
241 // snd_pcm_hw_params_free(params);
242 // snd_pcm_sw_params_free(swparams);
245 int AudioALSA::open_input()
247 char pcm_name[BCTEXTLEN];
248 snd_pcm_stream_t stream = SND_PCM_STREAM_CAPTURE;
252 device->in_channels = device->in_config->alsa_in_channels;
253 device->in_bits = device->in_config->alsa_in_bits;
255 translate_name(pcm_name, device->in_config->alsa_in_device);
257 err = snd_pcm_open(&dsp_in, pcm_name, stream, open_mode);
261 printf("AudioALSA::open_input: %s\n", snd_strerror(err));
266 device->in_config->alsa_in_channels,
267 device->in_config->alsa_in_bits,
268 device->in_samplerate,
274 int AudioALSA::open_output()
276 char pcm_name[BCTEXTLEN];
277 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
281 device->out_channels = device->out_config->alsa_out_channels;
282 device->out_bits = device->out_config->alsa_out_bits;
284 translate_name(pcm_name, device->out_config->alsa_out_device);
286 err = snd_pcm_open(&dsp_out, pcm_name, stream, open_mode);
290 printf("AudioALSA::open_output %s: %s\n", pcm_name, snd_strerror(err));
295 device->out_config->alsa_out_channels,
296 device->out_config->alsa_out_bits,
297 device->out_samplerate,
298 device->out_samples);
303 int AudioALSA::open_duplex()
305 // ALSA always opens 2 devices
309 int AudioALSA::close_output()
313 snd_pcm_close(dsp_out);
318 int AudioALSA::close_input()
322 // snd_pcm_reset(dsp_in);
323 snd_pcm_drop(dsp_in);
324 snd_pcm_drain(dsp_in);
325 snd_pcm_close(dsp_in);
330 int AudioALSA::close_all()
336 snd_pcm_close(dsp_duplex);
344 int64_t AudioALSA::device_position()
346 timer_lock->lock("AudioALSA::device_position");
347 int64_t result = samples_written +
348 timer->get_scaled_difference(device->out_samplerate) -
350 // printf("AudioALSA::device_position 1 %lld %lld %d %lld\n",
352 // timer->get_scaled_difference(device->out_samplerate),
354 // samples_written + timer->get_scaled_difference(device->out_samplerate) - delay);
355 timer_lock->unlock();
359 int AudioALSA::read_buffer(char *buffer, int size)
361 //printf("AudioALSA::read_buffer 1\n");
364 while(attempts < 1 && !done)
366 if(snd_pcm_readi(get_input(),
368 size / (device->in_bits / 8) / device->in_channels) < 0)
370 printf("AudioALSA::read_buffer overrun at sample %lld\n",
371 device->total_samples_read);
372 // snd_pcm_resume(get_input());
383 int AudioALSA::write_buffer(char *buffer, int size)
385 // Don't give up and drop the buffer on the first error.
388 int samples = size / (device->out_bits / 8) / device->out_channels;
389 while(attempts < 2 && !done && !interrupted)
391 // Buffers written must be equal to period_time
393 snd_pcm_sframes_t delay;
394 snd_pcm_delay(get_output(), &delay);
395 snd_pcm_avail_update(get_output());
397 device->Thread::enable_cancel();
398 if(snd_pcm_writei(get_output(),
402 device->Thread::disable_cancel();
403 printf("AudioALSA::write_buffer underrun at sample %lld\n",
404 device->current_position());
405 // snd_pcm_resume(get_output());
412 device->Thread::disable_cancel();
419 timer_lock->lock("AudioALSA::write_buffer");
422 samples_written += samples;
423 timer_lock->unlock();
428 int AudioALSA::flush_device()
430 if(get_output()) snd_pcm_drain(get_output());
434 int AudioALSA::interrupt_playback()
439 // Interrupts the playback but may not have caused snd_pcm_writei to exit.
440 // With some soundcards it causes snd_pcm_writei to freeze for a few seconds.
441 if(!device->out_config->interrupt_workaround)
442 snd_pcm_drop(get_output());
444 // Makes sure the current buffer finishes before stopping.
445 // snd_pcm_drain(get_output());
447 // The only way to ensure snd_pcm_writei exits, but
448 // got a lot of crashes when doing this.
449 // device->Thread::cancel();
455 snd_pcm_t* AudioALSA::get_output()
457 if(device->w) return dsp_out;
459 if(device->d) return dsp_duplex;
463 snd_pcm_t* AudioALSA::get_input()
465 if(device->r) return dsp_in;
467 if(device->d) return dsp_duplex;