summaryrefslogtreecommitdiff
path: root/src/channel.c
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-30 23:46:40 -0500
committersanine <sanine.not@pm.me>2022-08-30 23:46:40 -0500
commiteb4e4cab2d3f24ee7f8d0be7149162084fe98392 (patch)
tree37affdd321ee4f14965eb98000ae82a051efba40 /src/channel.c
parent489c3771e648d412cab283c660996c880149315d (diff)
add mono panning
Diffstat (limited to 'src/channel.c')
-rw-r--r--src/channel.c33
1 files changed, 25 insertions, 8 deletions
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 <stdlib.h>
#include <string.h>
+#include <math.h>
#include <plibsys.h>
#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) {