From 63db9380f84cb3eb35d2de430b0783afa5773e85 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 4 Sep 2022 00:39:24 -0500 Subject: implement callbacks --- src/channel.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/channel.c') diff --git a/src/channel.c b/src/channel.c index 8615265..ac955a1 100644 --- a/src/channel.c +++ b/src/channel.c @@ -15,16 +15,24 @@ void channel_init(struct channel_t *chan) chan->shared.volume = 255; chan->shared.pan_left = -128; chan->shared.pan_right = 128; + chan->shared.trigger_callback = 0; chan->sound_mutex = p_mutex_new(); chan->sound.left = NULL; chan->sound.right = NULL; chan->pos = 0; + + chan->callback = NULL; + chan->userdata = NULL; } void channel_reset(struct channel_t *chan) { + p_atomic_int_inc(&(chan->shared.trigger_callback)); + if (p_atomic_int_get(&(chan->shared.trigger_callback)) > 32000) + /* prevent integer overflow */ + p_atomic_int_set(&(chan->shared.trigger_callback), 1); chan->pos = 0; int loops = channel_atomic_get(chan->shared.loops); if (loops) { @@ -68,6 +76,25 @@ void channel_set_pan(struct channel_t *chan, float pan_left, float pan_right) } +void channel_set_callback(struct channel_t *chan, channel_callback_t callback, void *userdata) +{ + channel_atomic_set(chan->shared.trigger_callback, 0); + chan->callback = callback; + chan->userdata = userdata; +} + + +void channel_poll_callbacks(struct channel_t *chan, int index) +{ + int trigger_callback = channel_atomic_get(chan->shared.trigger_callback); + while (trigger_callback) { + if (chan->callback != NULL) chan->callback(index, chan->userdata); + trigger_callback -= 1; + } + channel_atomic_set(chan->shared.trigger_callback, 0); +} + + int channel_sound_load(struct channel_t *chan, struct mossrose_sound_t *sound, bool force, int loops) { if (!force && channel_atomic_get(chan->shared.active)) -- cgit v1.2.1