#include #include #include #include "channel.h" void channel_init(struct channel_t *chan) { chan->active = false; chan->paused = false; chan->volume = 255; chan->pan = 0; chan->sound_mutex = p_mutex_new(); chan->sound.left = NULL; chan->sound.right = NULL; chan->pos = 0; } void channel_reset(struct channel_t *chan) { p_atomic_int_set(&(chan->paused), false); p_atomic_int_set(&(chan->volume), 255); p_atomic_int_set(&(chan->pan), 0); chan->pos = 0; p_atomic_int_set(&(chan->active), false); } void channel_get_next_sample(float *left, float *right, struct channel_t *chan) { bool active = p_atomic_int_get(&(chan->active)); bool paused = p_atomic_int_get(&(chan->paused)); if (!active || paused) { /* skip this channel */ *left = 0; *right = 0; return; } if (!p_mutex_trylock(chan->sound_mutex)) { /* can't lock mutex, skip */ *left = 0; *right = 0; return; } *left = chan->sound.left[chan->pos]; *right = chan->sound.right[chan->pos]; chan->pos += 1; if (chan->pos >= chan->sound.len) { channel_reset(chan); } p_mutex_unlock(chan->sound_mutex); }