From eb4e4cab2d3f24ee7f8d0be7149162084fe98392 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 30 Aug 2022 23:46:40 -0500 Subject: add mono panning --- src/channel.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'src/channel.c') diff --git a/src/channel.c b/src/channel.c index 2fd0324..a29e7f7 100644 --- a/src/channel.c +++ b/src/channel.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "channel.h" @@ -64,6 +65,15 @@ void channel_set_pan(struct channel_t *chan, float pan_left, float pan_right) } +#define QUARTER_PI 0.785397 +static void pan_gain(float *gain_l, float *gain_r, float pan) +{ + float theta = (QUARTER_PI * pan) + QUARTER_PI; /* 0-PI/2 */ + *gain_l = cos(theta); + *gain_r = sin(theta); +} + + void channel_get_next_sample(float *left, float *right, struct channel_t *chan) { bool active = p_atomic_int_get(&(chan->active)); @@ -82,17 +92,24 @@ void channel_get_next_sample(float *left, float *right, struct channel_t *chan) return; } - float l, r; + float volume = ((float)p_atomic_int_get(&(chan->volume)))/255; - l = chan->sound.left[chan->pos]; - if (chan->sound.mono) - r = l; - else + if (chan->sound.mono) { + float x = chan->sound.left[chan->pos]; + float pan = ((float)p_atomic_int_get(&(chan->pan_left)))/128; + float gain_l, gain_r; + pan_gain(&gain_l, &gain_r, pan); + *left = volume * gain_l * x; + *right = volume * gain_r * x; + } + else { + float l, r; + l = chan->sound.left[chan->pos]; r = chan->sound.right[chan->pos]; + *left = volume * l; + *right = volume * r; + } - 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