Line data Source code
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>
24 0 : void cubeb_pan_stereo_buffer(T * buf, uint32_t frames, float pan)
25 : {
26 0 : if (pan == 0.0) {
27 0 : return;
28 : }
29 : /* rescale in [0; 1] */
30 0 : pan += 1;
31 0 : pan /= 2;
32 0 : float left_gain = float(cos(pan * M_PI * 0.5));
33 0 : 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 0 : if (pan < 0.5) {
38 0 : for (uint32_t i = 0; i < frames * 2; i+=2) {
39 0 : buf[i] = T(buf[i] + buf[i + 1] * left_gain);
40 0 : buf[i + 1] = T(buf[i + 1] * right_gain);
41 : }
42 : } else {
43 0 : for (uint32_t i = 0; i < frames * 2; i+=2) {
44 0 : buf[i] = T(buf[i] * left_gain);
45 0 : buf[i + 1] = T(buf[i + 1] + buf[i] * right_gain);
46 : }
47 : }
48 : }
49 : }
50 :
51 0 : void cubeb_pan_stereo_buffer_float(float * buf, uint32_t frames, float pan)
52 : {
53 0 : cubeb_pan_stereo_buffer(buf, frames, pan);
54 0 : }
55 :
56 0 : void cubeb_pan_stereo_buffer_int(short * buf, uint32_t frames, float pan)
57 : {
58 0 : cubeb_pan_stereo_buffer(buf, frames, pan);
59 0 : }
60 :
|