summaryrefslogtreecommitdiff
path: root/media/libcubeb/src/cubeb_panner.cpp
blob: bd96ed6ef0a734f73451e380fec2a5ef04e8c11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright © 2014 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */

#define _USE_MATH_DEFINES
#include <math.h>
#include <stdint.h>

#include "cubeb_panner.h"

#ifndef M_PI
#define M_PI 3.14159263
#endif

/**
 * We use a cos/sin law.
 */

namespace {
template<typename T>
void cubeb_pan_stereo_buffer(T * buf, uint32_t frames, float pan)
{
  if (pan == 0.0) {
    return;
  }
  /* rescale in [0; 1] */
  pan += 1;
  pan /= 2;
  float left_gain  = float(cos(pan * M_PI * 0.5));
  float right_gain = float(sin(pan * M_PI * 0.5));

  /* In we are panning on the left, pan the right channel into the left one and
   * vice-versa. */
  if (pan < 0.5) {
    for (uint32_t i = 0; i < frames * 2; i+=2) {
      buf[i]     = T(buf[i] + buf[i + 1] * left_gain);
      buf[i + 1] = T(buf[i + 1] * right_gain);
    }
  } else {
    for (uint32_t i = 0; i < frames * 2; i+=2) {
      buf[i]     = T(buf[i] * left_gain);
      buf[i + 1] = T(buf[i + 1] + buf[i] * right_gain);
    }
  }
}
}

void cubeb_pan_stereo_buffer_float(float * buf, uint32_t frames, float pan)
{
  cubeb_pan_stereo_buffer(buf, frames, pan);
}

void cubeb_pan_stereo_buffer_int(short * buf, uint32_t frames, float pan)
{
  cubeb_pan_stereo_buffer(buf, frames, pan);
}