ALSA: let alsa lib choose period settings

Closes #46
This commit is contained in:
Andrew Kelley 2015-11-14 21:44:03 -07:00
parent d168a7d192
commit 8f1e1b8752
2 changed files with 8 additions and 29 deletions

View file

@ -1312,34 +1312,7 @@ static int outstream_open_alsa(struct SoundIoPrivate *si, struct SoundIoOutStrea
return SoundIoErrorOpeningDevice;
}
int period_count;
if (device->is_raw) {
period_count = 4;
unsigned int microseconds = 0.25 * outstream->software_latency * 1000000.0;
if ((err = snd_pcm_hw_params_set_period_time_near(osa->handle, hwparams, &microseconds, NULL)) < 0) {
outstream_destroy_alsa(si, os);
return SoundIoErrorOpeningDevice;
}
} else {
period_count = 2;
double period_duration = 0.5 * outstream->software_latency;
snd_pcm_uframes_t period_frames =
ceil_dbl_to_uframes(period_duration * (double)outstream->sample_rate);
if ((err = snd_pcm_hw_params_set_period_size_near(osa->handle, hwparams, &period_frames, NULL)) < 0) {
outstream_destroy_alsa(si, os);
return SoundIoErrorOpeningDevice;
}
}
snd_pcm_uframes_t period_size;
if ((snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL)) < 0) {
outstream_destroy_alsa(si, os);
return SoundIoErrorOpeningDevice;
}
osa->period_size = period_size;
osa->buffer_size_frames = osa->period_size * period_count;
osa->buffer_size_frames = outstream->software_latency * outstream->sample_rate;
if ((err = snd_pcm_hw_params_set_buffer_size_near(osa->handle, hwparams, &osa->buffer_size_frames)) < 0) {
outstream_destroy_alsa(si, os);
return SoundIoErrorOpeningDevice;
@ -1352,6 +1325,12 @@ static int outstream_open_alsa(struct SoundIoPrivate *si, struct SoundIoOutStrea
return (err == -EINVAL) ? SoundIoErrorIncompatibleDevice : SoundIoErrorOpeningDevice;
}
if ((snd_pcm_hw_params_get_period_size(hwparams, &osa->period_size, NULL)) < 0) {
outstream_destroy_alsa(si, os);
return SoundIoErrorOpeningDevice;
}
// set channel map
osa->chmap->channels = ch_count;
for (int i = 0; i < ch_count; i += 1) {

View file

@ -59,7 +59,7 @@ struct SoundIoOutStreamAlsa {
struct pollfd *poll_fds;
struct SoundIoOsThread *thread;
atomic_flag thread_exit_flag;
int period_size;
snd_pcm_uframes_t period_size;
int write_frame_count;
bool is_paused;
atomic_flag clear_buffer_flag;