mirror of
https://github.com/Ryujinx/libsoundio.git
synced 2025-01-03 20:25:29 +00:00
ALSA: use period size to calculate buffer size
determining the buffer size before the period size sometimes results in EINVAL. closes #33
This commit is contained in:
parent
b202f6746b
commit
2f14aa569e
26
src/alsa.cpp
26
src/alsa.cpp
|
@ -1206,7 +1206,8 @@ static int outstream_open_alsa(SoundIoPrivate *si, SoundIoOutStreamPrivate *os)
|
||||||
osa->clear_buffer_flag.test_and_set();
|
osa->clear_buffer_flag.test_and_set();
|
||||||
|
|
||||||
if (outstream->software_latency == 0.0)
|
if (outstream->software_latency == 0.0)
|
||||||
outstream->software_latency = clamp(device->software_latency_min, 1.0, device->software_latency_max);
|
outstream->software_latency = 1.0;
|
||||||
|
outstream->software_latency = clamp(device->software_latency_min, outstream->software_latency, device->software_latency_max);
|
||||||
|
|
||||||
int ch_count = outstream->layout.channel_count;
|
int ch_count = outstream->layout.channel_count;
|
||||||
|
|
||||||
|
@ -1267,22 +1268,17 @@ static int outstream_open_alsa(SoundIoPrivate *si, SoundIoOutStreamPrivate *os)
|
||||||
return SoundIoErrorOpeningDevice;
|
return SoundIoErrorOpeningDevice;
|
||||||
}
|
}
|
||||||
|
|
||||||
osa->buffer_size_frames = ceil_dbl_to_int(outstream->software_latency * (double)outstream->sample_rate);
|
int period_count;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
outstream->software_latency = ((double)osa->buffer_size_frames) / (double)outstream->sample_rate;
|
|
||||||
|
|
||||||
if (device->is_raw) {
|
if (device->is_raw) {
|
||||||
|
period_count = 4;
|
||||||
unsigned int microseconds = 0.25 * outstream->software_latency * 1000000.0;
|
unsigned int microseconds = 0.25 * outstream->software_latency * 1000000.0;
|
||||||
if ((err = snd_pcm_hw_params_set_period_time_near(osa->handle, hwparams, µseconds, nullptr)) < 0) {
|
if ((err = snd_pcm_hw_params_set_period_time_near(osa->handle, hwparams, µseconds, nullptr)) < 0) {
|
||||||
outstream_destroy_alsa(si, os);
|
outstream_destroy_alsa(si, os);
|
||||||
return SoundIoErrorOpeningDevice;
|
return SoundIoErrorOpeningDevice;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
double period_duration = outstream->software_latency / 2.0;
|
period_count = 2;
|
||||||
|
double period_duration = 0.5 * outstream->software_latency;
|
||||||
snd_pcm_uframes_t period_frames =
|
snd_pcm_uframes_t period_frames =
|
||||||
ceil_dbl_to_uframes(period_duration * (double)outstream->sample_rate);
|
ceil_dbl_to_uframes(period_duration * (double)outstream->sample_rate);
|
||||||
|
|
||||||
|
@ -1299,6 +1295,13 @@ static int outstream_open_alsa(SoundIoPrivate *si, SoundIoOutStreamPrivate *os)
|
||||||
}
|
}
|
||||||
osa->period_size = period_size;
|
osa->period_size = period_size;
|
||||||
|
|
||||||
|
osa->buffer_size_frames = osa->period_size * period_count;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
outstream->software_latency = ((double)osa->buffer_size_frames) / (double)outstream->sample_rate;
|
||||||
|
|
||||||
// write the hardware parameters to device
|
// write the hardware parameters to device
|
||||||
if ((err = snd_pcm_hw_params(osa->handle, hwparams)) < 0) {
|
if ((err = snd_pcm_hw_params(osa->handle, hwparams)) < 0) {
|
||||||
outstream_destroy_alsa(si, os);
|
outstream_destroy_alsa(si, os);
|
||||||
|
@ -1534,7 +1537,8 @@ static int instream_open_alsa(SoundIoPrivate *si, SoundIoInStreamPrivate *is) {
|
||||||
SoundIoDevice *device = instream->device;
|
SoundIoDevice *device = instream->device;
|
||||||
|
|
||||||
if (instream->software_latency == 0.0)
|
if (instream->software_latency == 0.0)
|
||||||
instream->software_latency = clamp(device->software_latency_min, 1.0, device->software_latency_max);
|
instream->software_latency = 1.0;
|
||||||
|
instream->software_latency = clamp(device->software_latency_min, instream->software_latency, device->software_latency_max);
|
||||||
|
|
||||||
int ch_count = instream->layout.channel_count;
|
int ch_count = instream->layout.channel_count;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue