From eba3f0a2d5fd4972e1c74aa29ddb6ba40a7b5a3a Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 8 Jul 2024 18:22:40 +0300 Subject: [PATCH] audio.c: sanity checking with warnings --- src/audio.c | 11 +++++++++++ src/private/audio.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/audio.c b/src/audio.c index 20a1720..41d0599 100644 --- a/src/audio.c +++ b/src/audio.c @@ -177,6 +177,7 @@ void play_audio_ex(const char *path, const char *channel, t_play_audio_args args .file_type = file_type, .context = init_audio_context(path, file_type), .path = path, + .name = channel, }; shput(ctx.audio_channels, channel, new_channel); pair = shgetp_null(ctx.audio_channels, channel); @@ -324,6 +325,15 @@ static void audio_sample_and_mixin_channel(const struct audio_channel *channel, } +static void sanity_check_channel(const struct audio_channel *channel) { + if (channel->args.volume < 0.0f || channel->args.volume > 1.0f) + log_warn("Volume argument is out of range for channel (%s)", channel->name); + + if (channel->args.panning < -1.0f || channel->args.panning > 1.0f) + log_warn("panning argument is out of range for channel (%s)", channel->name); +} + + void audio_callback(void *userdata, uint8_t *stream, int len) { (void)userdata; @@ -331,6 +341,7 @@ void audio_callback(void *userdata, uint8_t *stream, int len) { SDL_memset(stream, sizeof (uint16_t), len * ctx.audio_stream_channel_count); for (int i = 0; i < shlen(ctx.audio_channels); ++i) { + sanity_check_channel(&ctx.audio_channels[i].value); audio_sample_and_mixin_channel(&ctx.audio_channels[i].value, stream, len); } } diff --git a/src/private/audio.h b/src/private/audio.h index a484130..d94589b 100644 --- a/src/private/audio.h +++ b/src/private/audio.h @@ -40,6 +40,7 @@ struct audio_channel { enum audio_file_type file_type; union audio_context context; /* interpreted by `file_type` value */ const char *path; + const char *name; };