aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2020-10-07 19:22:53 +0200
committerAndre Weissflog <floooh@gmail.com>2020-10-07 19:22:53 +0200
commit8611fae87cb48cdce9efbf866fbee1ff7b624c39 (patch)
treeaee36ce230edf2ddb3cbf42c97bcc06b6982149d
parentd5e619093996c5e11336b507753ca297625a82ea (diff)
sokol_audio.h Linux: better ALSA initialization
- log messages for failed functions - use set_buffer_size_near() call because the exactly requested buffer size might not be supported by the runtime hardware config - init fails softly if the actual buffer size isn't a multiple of the requested packet size
-rw-r--r--sokol_audio.h23
1 files changed, 21 insertions, 2 deletions
diff --git a/sokol_audio.h b/sokol_audio.h
index baf74eaf..1ca13791 100644
--- a/sokol_audio.h
+++ b/sokol_audio.h
@@ -1057,8 +1057,10 @@ _SOKOL_PRIVATE void* _saudio_alsa_cb(void* param) {
_SOKOL_PRIVATE bool _saudio_backend_init(void) {
int dir; unsigned int val;
+ snd_pcm_uframes_t buf_frames;
int rc = snd_pcm_open(&_saudio.backend.device, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
+ SOKOL_LOG("sokol_audio.h: snd_pcm_open() failed");
return false;
}
snd_pcm_hw_params_t* params = 0;
@@ -1066,8 +1068,8 @@ _SOKOL_PRIVATE bool _saudio_backend_init(void) {
snd_pcm_hw_params_any(_saudio.backend.device, params);
snd_pcm_hw_params_set_access(_saudio.backend.device, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_channels(_saudio.backend.device, params, _saudio.num_channels);
- snd_pcm_hw_params_set_buffer_size(_saudio.backend.device, params, _saudio.buffer_frames);
if (0 > snd_pcm_hw_params_test_format(_saudio.backend.device, params, SND_PCM_FORMAT_FLOAT_LE)) {
+ SOKOL_LOG("sokol_audio.h: snd_pcm_hw_params_test_format() failed");
goto error;
}
else {
@@ -1076,9 +1078,17 @@ _SOKOL_PRIVATE bool _saudio_backend_init(void) {
val = _saudio.sample_rate;
dir = 0;
if (0 > snd_pcm_hw_params_set_rate_near(_saudio.backend.device, params, &val, &dir)) {
+ SOKOL_LOG("sokol_audio.h: snd_pcm_hw_params_set_rate_near() failed");
goto error;
}
+ buf_frames = _saudio.buffer_frames;
+ if (0 > snd_pcm_hw_params_set_buffer_size_near(_saudio.backend.device, params, &buf_frames)) {
+ SOKOL_LOG("sokol_audio.h: snd_pcm_hw_params_set_buffer_size_near() failed");
+ goto error;
+ }
+ _saudio.buffer_frames = buf_frames;
if (0 > snd_pcm_hw_params(_saudio.backend.device, params)) {
+ SOKOL_LOG("sokol_audio.h: snd_pcm_hw_params() failed");
goto error;
}
@@ -1097,6 +1107,7 @@ _SOKOL_PRIVATE bool _saudio_backend_init(void) {
/* create the buffer-streaming start thread */
if (0 != pthread_create(&_saudio.backend.thread, 0, _saudio_alsa_cb, 0)) {
+ SOKOL_LOG("sokol_audio.h: pthread_create() failed");
goto error;
}
@@ -1822,7 +1833,15 @@ SOKOL_API_IMPL void saudio_setup(const saudio_desc* desc) {
_saudio.num_channels = _saudio_def(_saudio.desc.num_channels, 1);
_saudio_fifo_init_mutex(&_saudio.fifo);
if (_saudio_backend_init()) {
- SOKOL_ASSERT(0 == (_saudio.buffer_frames % _saudio.packet_frames));
+ /* the backend might not support the requested exact buffer size,
+ make sure the actual buffer size is still a multiple of
+ the requested packet size
+ */
+ if (0 != (_saudio.buffer_frames % _saudio.packet_frames)) {
+ SOKOL_LOG("sokol_audio.h: actual backend buffer size isn't multiple of requested packet size");
+ _saudio_backend_shutdown();
+ return;
+ }
SOKOL_ASSERT(_saudio.bytes_per_frame > 0);
_saudio_fifo_init(&_saudio.fifo, _saudio.packet_frames * _saudio.bytes_per_frame, _saudio.num_packets);
_saudio.valid = true;