-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanning.cpp
80 lines (70 loc) · 2.2 KB
/
panning.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <math.h>
#include "panning.h"
#ifndef PI
#define PI 3.14159265359
#endif
#ifndef SQRT2
#define SQRT2 1.414213562
#endif
#define RANGE 254
//-----------------------------------------------------------------
// Set the panning values for the two stereo channels (L,R)
// for a position 0..127..254 L..C..R
//-----------------------------------------------------------------
void calc_panning(float channels[2], int position)
{
// Equal power law: equation is
// right = sin( position / range * pi / 2) * sqrt( 2 )
// left is equivalent to right with position = range - position
// position is in the range 0 .. RANGE
// RANGE / 2 = centre, result = 1.0f
if ( position > RANGE )
position = RANGE;
else if ( position < 0 )
position = 0;
channels[1] = (float)( sin( (double)position / RANGE * PI / 2 ) * SQRT2 );
position = RANGE - position;
channels[0] = (float)( sin( (double)position / RANGE * PI / 2 ) * SQRT2 );
}
//-----------------------------------------------------------------
// Reset the panning values to the centre position
//-----------------------------------------------------------------
void centre_panning(float *channels)
{
channels[0] = channels[1] = 1.0f;
}
//-----------------------------------------------------------------
// Generate a stereo position in the range 0..RANGE
// with Gaussian distribution, mean RANGE/2, S.D. RANGE/5
//-----------------------------------------------------------------
int random_stereo()
{
int n = (int)(RANGE/2 + gauss_rand() * (RANGE * 0.2) );
if ( n > RANGE ) n = RANGE;
if ( n < 0 ) n = 0;
return n;
}
//-----------------------------------------------------------------
// Generate a Gaussian random number with mean 0, variance 1
// Copied from an ancient C newsgroup FAQ
//-----------------------------------------------------------------
double gauss_rand()
{
static double V1, V2, S;
static int phase = 0;
double X;
if(phase == 0) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return X;
}