1 /*
2  * Copyright © 2014 Mozilla Foundation
3  *
4  * This program is made available under an ISC-style license.  See the
5  * accompanying file LICENSE for details.
6  */
7 
8 #define _USE_MATH_DEFINES
9 #include <math.h>
10 #include <stdint.h>
11 
12 #include "cubeb_panner.h"
13 
14 #ifndef M_PI
15 #define M_PI 3.14159263
16 #endif
17 
18 /**
19  * We use a cos/sin law.
20  */
21 
22 namespace {
23 template<typename T>
cubeb_pan_stereo_buffer(T * buf,uint32_t frames,float pan)24 void cubeb_pan_stereo_buffer(T * buf, uint32_t frames, float pan)
25 {
26   if (pan == 0.0) {
27     return;
28   }
29   /* rescale in [0; 1] */
30   pan += 1;
31   pan /= 2;
32   float left_gain  = float(cos(pan * M_PI * 0.5));
33   float right_gain = float(sin(pan * M_PI * 0.5));
34 
35   /* In we are panning on the left, pan the right channel into the left one and
36    * vice-versa. */
37   if (pan < 0.5) {
38     for (uint32_t i = 0; i < frames * 2; i+=2) {
39       buf[i]     = T(buf[i] + buf[i + 1] * left_gain);
40       buf[i + 1] = T(buf[i + 1] * right_gain);
41     }
42   } else {
43     for (uint32_t i = 0; i < frames * 2; i+=2) {
44       buf[i]     = T(buf[i] * left_gain);
45       buf[i + 1] = T(buf[i + 1] + buf[i] * right_gain);
46     }
47   }
48 }
49 }
50 
cubeb_pan_stereo_buffer_float(float * buf,uint32_t frames,float pan)51 void cubeb_pan_stereo_buffer_float(float * buf, uint32_t frames, float pan)
52 {
53   cubeb_pan_stereo_buffer(buf, frames, pan);
54 }
55 
cubeb_pan_stereo_buffer_int(short * buf,uint32_t frames,float pan)56 void cubeb_pan_stereo_buffer_int(short * buf, uint32_t frames, float pan)
57 {
58   cubeb_pan_stereo_buffer(buf, frames, pan);
59 }
60 
61