audio.c: sanity checking with warnings

This commit is contained in:
veclavtalica 2024-07-08 18:22:40 +03:00
parent 6cb8736fc1
commit eba3f0a2d5
2 changed files with 12 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
};