-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdhalf.c
79 lines (67 loc) · 1.92 KB
/
pdhalf.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
/*
* PDHalf
*
* This code has been extracted from the Csound opcode "pdhalf".
* It has been modified to work as a Soundpipe module.
*
* Original Author(s): Anthony M. Kozar Jr.
* Year: 2004
* Location: Opcodes/shape.c
*/
#include <stdlib.h>
#include "soundpipe.h"
int sp_pdhalf_create(sp_pdhalf **p)
{
*p = malloc(sizeof(sp_pdhalf));
return SP_OK;
}
int sp_pdhalf_destroy(sp_pdhalf **p)
{
free(*p);
return SP_OK;
}
int sp_pdhalf_init(sp_data *sp, sp_pdhalf *p)
{
p->ibipolar = 0;
p->ifullscale = 1.0;
p->amount = 0;
return SP_OK;
}
int sp_pdhalf_compute(sp_data *sp, sp_pdhalf *p, SPFLOAT *in, SPFLOAT *out)
{
SPFLOAT cur, maxampl, midpoint, leftslope, rightslope;
maxampl = p->ifullscale;
if (maxampl == 0.0) maxampl = 1.0;
if (p->ibipolar != 0.0) {
midpoint = (p->amount >= 1.0 ? maxampl :
(p->amount <= -1.0 ? -maxampl :
(p->amount * maxampl)));
if (midpoint != -maxampl)
leftslope = maxampl / (midpoint + maxampl);
else leftslope = 0.0;
if (midpoint != maxampl)
rightslope = maxampl / (maxampl - midpoint);
else rightslope = 0.0;
cur = *in;
if (cur < midpoint) *out = leftslope * (cur - midpoint);
else *out = rightslope * (cur - midpoint);
} else {
SPFLOAT halfmaxampl = 0.5 * maxampl;
midpoint = (p->amount >= 1.0 ? maxampl :
(p->amount <= -1.0 ? 0.0 :
((p->amount + 1.0) * halfmaxampl)));
if (midpoint != 0.0)
leftslope = halfmaxampl / midpoint;
else leftslope = 0.0;
if (midpoint != maxampl)
rightslope = halfmaxampl / (maxampl - midpoint);
else rightslope = 0.0;
cur = *in;
if (cur < midpoint) {
*out = leftslope * cur;
} else {
*out = rightslope * (cur - midpoint) + halfmaxampl;
}
}
return SP_OK;
}