-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpan2.c
85 lines (70 loc) · 1.57 KB
/
pan2.c
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
81
82
83
84
85
/*
* Pan2
*
* This code has been extracted from the Csound opcode "pan2"
* It has been modified to work as a Soundpipe module.
*
* Original Author(s): John ffitch
* Year: 2007
* Location: Opcodes/pan2.c
*
*/
#include <stdlib.h>
#include <math.h>
#include "soundpipe.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define SQRT2 1.41421356237309504880
int sp_pan2_create(sp_pan2 **p)
{
*p = malloc(sizeof(sp_pan2));
return SP_OK;
}
int sp_pan2_destroy(sp_pan2 **p)
{
free(*p);
return SP_OK;
}
int sp_pan2_init(sp_data *sp, sp_pan2 *p)
{
p->type = 0;
p->pan = 0;
return SP_OK;
}
int sp_pan2_compute(sp_data *sp, sp_pan2 *p, SPFLOAT *in, SPFLOAT *out1, SPFLOAT *out2)
{
/* Send the signal's input to the output */
uint32_t type = p->type;
SPFLOAT pan = (1 + p->pan) * 0.5;
SPFLOAT cc, ss, l, r;
type %= 4;
switch (type) {
/* Equal power */
case 0:
pan = M_PI * 0.5 * pan;
*out1 = *in * cos(pan);
*out2 = *in * sin(pan);
break;
/* Square root */
case 1:
*out1 = *in * sqrt(pan);
*out2 = *in * sqrt(1.0 - pan);
break;
/* simple linear */
case 2:
*out1 = *in * (1.0 - pan);
*out2 = *in * pan;
break;
/* Equal power (alternative) */
case 3:
cc = cos(M_PI * pan * 0.5);
ss = sin(M_PI * pan * 0.5);
l = SQRT2 * (cc + ss) * 0.5;
r = SQRT2 * (cc - ss) * 0.5;
*out1 = *in * l;
*out2 = *in * r;
break;
}
return SP_OK;
}