From 489c3771e648d412cab283c660996c880149315d Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 30 Aug 2022 22:25:43 -0500 Subject: refactor: use stereo panning model --- src/channel.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/channel.c') diff --git a/src/channel.c b/src/channel.c index 603a980..2fd0324 100644 --- a/src/channel.c +++ b/src/channel.c @@ -9,7 +9,8 @@ void channel_init(struct channel_t *chan) chan->active = false; chan->paused = false; chan->volume = 255; - chan->pan = 0; + chan->pan_left = -128; + chan->pan_right = 128; chan->sound_mutex = p_mutex_new(); chan->sound.left = NULL; @@ -22,7 +23,8 @@ 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); + p_atomic_int_set(&(chan->pan_left), -128); + p_atomic_int_set(&(chan->pan_right), 128); chan->pos = 0; p_atomic_int_set(&(chan->active), false); } @@ -49,12 +51,16 @@ void channel_set_volume(struct channel_t *chan, float volume) } -void channel_set_pan(struct channel_t *chan, float pan) +void channel_set_pan(struct channel_t *chan, float pan_left, float pan_right) { - if (pan > 1.0f) pan = 1.0f; - if (pan < -1.0f) pan = -1.0f; + if (pan_left > 1.0f) pan_left = 1.0f; + if (pan_left < -1.0f) pan_left = -1.0f; - p_atomic_int_set(&(chan->pan), 128*pan); + if (pan_right > 1.0f) pan_right = 1.0f; + if (pan_right < -1.0f) pan_right = -1.0f; + + p_atomic_int_set(&(chan->pan_left), 128*pan_left); + p_atomic_int_set(&(chan->pan_right), 128*pan_right); } @@ -76,8 +82,17 @@ void channel_get_next_sample(float *left, float *right, struct channel_t *chan) return; } - *left = chan->sound.left[chan->pos]; - *right = chan->sound.right[chan->pos]; + float l, r; + + l = chan->sound.left[chan->pos]; + if (chan->sound.mono) + r = l; + else + r = chan->sound.right[chan->pos]; + + float volume = ((float)p_atomic_int_get(&(chan->active)))/255; + *left = volume * l; + *right = volume * r; chan->pos += 1; if (chan->pos >= chan->sound.len) { -- cgit v1.2.1