-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.c
142 lines (111 loc) · 2.6 KB
/
wave.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <portsf.h>
#include <breakpoints.h>
#include <wave.h>
#ifndef M_PI
#define M_PI (3.1415926535897932)
#endif
#define TWOPI (2.0 * M_PI)
// double oscil(double frequency);
void oscil_init(OSCIL* osc, unsigned long srate)
{
osc->twopiovrsr = TWOPI / (double) srate;
osc->curfreq = 0.0;
osc->curphase = 0.0;
osc->incr = 0.0;
}
OSCIL* oscil(void)
{
OSCIL* osc = (OSCIL*) malloc(sizeof(OSCIL));
if (osc == NULL) return NULL;
return osc;
}
// local typing-saving macro for tick functions
#define OSC_WRAPPHASE if(p_osc->curphase > TWOPI) p_osc->curphase -= TWOPI; \
if(p_osc->curphase < 0.0) p_osc->curphase += TWOPI
OSCIL* new_oscil(double srate)
{
OSCIL* p_osc;
p_osc = (OSCIL*) malloc(sizeof(OSCIL));
if (p_osc == NULL) return NULL;
p_osc->twopiovrsr = TWOPI / (double) srate;
p_osc->curfreq = 0.0;
p_osc->curphase = 0.0;
p_osc->incr = 0.0;
return p_osc;
}
double sinetick(OSCIL* p_osc, double freq)
{
double val;
val = sin(p_osc->curphase);
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
p_osc->curphase += p_osc->incr;
if (p_osc->curphase >= TWOPI) p_osc->curphase -= TWOPI;
if (p_osc->curphase < 0.0) p_osc->curphase += TWOPI;
return val;
}
// SQUARE
double sqtick(OSCIL* p_osc, double freq)
{
double val;
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
if (p_osc->curphase <= M_PI) {
val = 1.0;
} else {
val = -1;
}
p_osc->curphase += p_osc->incr;
OSC_WRAPPHASE;
return val;
}
// SAWTOOTH DOWNWARDS
double sawdtick(OSCIL* p_osc, double freq)
{
double val;
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
val = 1.0 - 2.0 * (p_osc->curphase * (1.0 / TWOPI) );
p_osc->curphase += p_osc->incr;
OSC_WRAPPHASE;
return val;
}
// SAWTOOTH UPWARDS
double sawutick(OSCIL* p_osc, double freq)
{
double val;
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
val = (2.0 * (p_osc->curphase * (1.0 / TWOPI) )) - 1.0;
p_osc->curphase += p_osc->incr;
OSC_WRAPPHASE;
return val;
}
// TRIANGLE
double tritick(OSCIL* p_osc, double freq)
{
double val;
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
if (p_osc->curphase <= M_PI) {
val = (4.0 * (p_osc->curphase * (1.0 / TWOPI) )) - 1.0;
} else {
val = 3.0 - 4.0 * (p_osc->curphase * (1.0 / TWOPI) );
}
p_osc->curphase += p_osc->incr;
OSC_WRAPPHASE;
return val;
}