-
Notifications
You must be signed in to change notification settings - Fork 3
/
waveform.c
210 lines (193 loc) · 6.71 KB
/
waveform.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*!
* Polyphonic synthesizer for microcontrollers. Voice waveform generator.
* (C) 2017 Stuart Longland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "waveform.h"
#include "synth.h"
#include "debug.h"
#include <stdlib.h>
/* Amplitude scaling */
#define VOICE_WF_AMP_SCALE (8)
/*!
* Number of fractional bits for `period` and `period_remain`.
* This allows tuned notes even in lower sampling frequencies.
* The integer part (12 bits) is wide enough to render a 20Hz
* note on the higher 48kHz sampling frequency.
*/
#define PERIOD_FP_SCALE (4)
int8_t voice_wf_next(struct voice_wf_gen_t* const wf_gen) {
switch(wf_gen->mode) {
case VOICE_MODE_DC:
_DPRINTF("wf=%p mode=DC amp=%d\n",
wf_gen, wf_gen->amplitude);
return wf_gen->amplitude;
case VOICE_MODE_NOISE:
wf_gen->sample = (rand() /
(RAND_MAX/512)) - 256;
wf_gen->sample *= wf_gen->amplitude;
_DPRINTF("wf=%p mode=NOISE amp=%d → sample=%d\n",
wf_gen, wf_gen->amplitude,
wf_gen->sample);
break;
case VOICE_MODE_SQUARE:
if ((wf_gen->period_remain >> PERIOD_FP_SCALE) == 0) {
/* Swap value */
wf_gen->sample = -wf_gen->sample;
wf_gen->period_remain += wf_gen->period;
}
wf_gen->period_remain -= (1 << PERIOD_FP_SCALE);
_DPRINTF("wf=%p mode=SQUARE amp=%d rem=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude,
wf_gen->period_remain,
wf_gen->sample);
break;
case VOICE_MODE_SAWTOOTH:
if ((wf_gen->period_remain >> PERIOD_FP_SCALE) == 0) {
/* Back to -amplitude */
wf_gen->sample = -wf_gen->amplitude;
wf_gen->period_remain += wf_gen->period;
} else {
wf_gen->sample += wf_gen->step;
}
wf_gen->period_remain -= (1 << PERIOD_FP_SCALE);
_DPRINTF("wf=%p mode=SAWTOOTH amp=%d rem=%d step=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude,
wf_gen->period_remain, wf_gen->step,
wf_gen->sample);
break;
case VOICE_MODE_TRIANGLE:
if ((wf_gen->period_remain >> PERIOD_FP_SCALE) == 0) {
/* Switch direction */
if (wf_gen->step > 0)
wf_gen->sample = wf_gen->amplitude;
else
wf_gen->sample = -wf_gen->amplitude;
wf_gen->step = -wf_gen->step;
wf_gen->period_remain += wf_gen->period;
} else {
wf_gen->sample += wf_gen->step;
}
wf_gen->period_remain -= (1 << PERIOD_FP_SCALE);
_DPRINTF("wf=%p mode=TRIANGLE amp=%d rem=%d step=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude,
wf_gen->period_remain, wf_gen->step,
wf_gen->sample);
break;
}
return wf_gen->sample >> VOICE_WF_AMP_SCALE;
}
/* Compute frequency period (sawtooth wave) */
uint16_t voice_wf_freq_to_period(uint16_t freq) {
/* Use 16-bit 12.4 fixed point */
return (((uint32_t)synth_freq << PERIOD_FP_SCALE) / freq);
}
/* Compute frequency period (square/triangle wave) */
static uint16_t voice_wf_to_square_period(uint16_t period) {
return period >> 1;
}
void voice_wf_set_dc(struct voice_wf_gen_t* const wf_gen,
int8_t amplitude) {
wf_gen->mode = VOICE_MODE_DC;
wf_gen->amplitude = amplitude;
}
static void voice_wf_set_square_p(struct voice_wf_gen_t* const wf_gen,
uint16_t period, int8_t amplitude) {
wf_gen->mode = VOICE_MODE_SQUARE;
wf_gen->amplitude = (int16_t)amplitude << VOICE_WF_AMP_SCALE;
wf_gen->period = voice_wf_to_square_period(period);
wf_gen->period_remain = wf_gen->period;
wf_gen->sample = wf_gen->amplitude;
_DPRINTF("wf=%p INIT mode=SQUARE amp=%d per=%d rem=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude, wf_gen->period,
wf_gen->period_remain,
wf_gen->sample);
}
void voice_wf_set_square(struct voice_wf_gen_t* const wf_gen,
uint16_t freq, int8_t amplitude) {
uint16_t period = voice_wf_freq_to_period(freq);
voice_wf_set_square_p(wf_gen, period, amplitude);
}
static void voice_wf_set_sawtooth_p(struct voice_wf_gen_t* const wf_gen,
uint16_t period, int8_t amplitude) {
wf_gen->mode = VOICE_MODE_SAWTOOTH;
wf_gen->sample = -(int16_t)amplitude << VOICE_WF_AMP_SCALE;
wf_gen->period = period;
wf_gen->period_remain = wf_gen->period;
wf_gen->amplitude = -wf_gen->sample;
wf_gen->step = (wf_gen->amplitude / (wf_gen->period >> PERIOD_FP_SCALE)) << 1;
_DPRINTF("wf=%p INIT mode=SAWTOOTH amp=%d per=%d step=%d rem=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude, wf_gen->period,
wf_gen->step, wf_gen->period_remain,
wf_gen->sample);
}
void voice_wf_set_sawtooth(struct voice_wf_gen_t* const wf_gen,
uint16_t freq, int8_t amplitude) {
uint16_t period = voice_wf_freq_to_period(freq);
voice_wf_set_sawtooth_p(wf_gen, period, amplitude);
}
static void voice_wf_set_triangle_p(struct voice_wf_gen_t* const wf_gen,
uint16_t period, int8_t amplitude) {
wf_gen->mode = VOICE_MODE_TRIANGLE;
wf_gen->sample = -(int16_t)amplitude << VOICE_WF_AMP_SCALE;
wf_gen->period = voice_wf_to_square_period(period);
wf_gen->period_remain = wf_gen->period;
wf_gen->amplitude = -wf_gen->sample;
wf_gen->step = (wf_gen->amplitude / (wf_gen->period >> PERIOD_FP_SCALE)) << 1;
_DPRINTF("wf=%p INIT mode=TRIANGLE amp=%d per=%d step=%d rem=%d "
"→ sample=%d\n",
wf_gen, wf_gen->amplitude, wf_gen->period,
wf_gen->step, wf_gen->period_remain,
wf_gen->sample);
}
void voice_wf_set_triangle(struct voice_wf_gen_t* const wf_gen,
uint16_t freq, int8_t amplitude) {
uint16_t period = voice_wf_freq_to_period(freq);
voice_wf_set_triangle_p(wf_gen, period, amplitude);
}
void voice_wf_set_noise(struct voice_wf_gen_t* const wf_gen,
int8_t amplitude) {
wf_gen->mode = VOICE_MODE_NOISE;
wf_gen->amplitude = amplitude;
}
void voice_wf_set(struct voice_wf_gen_t* const wf_gen, struct voice_wf_def_t* const wf_def) {
switch (wf_def->mode) {
case VOICE_MODE_DC:
voice_wf_set_dc(wf_gen, wf_def->amplitude);
break;
case VOICE_MODE_SQUARE:
voice_wf_set_square_p(wf_gen, wf_def->period, wf_def->amplitude);
break;
case VOICE_MODE_SAWTOOTH:
voice_wf_set_sawtooth_p(wf_gen, wf_def->period, wf_def->amplitude);
break;
case VOICE_MODE_TRIANGLE:
voice_wf_set_triangle_p(wf_gen, wf_def->period, wf_def->amplitude);
break;
case VOICE_MODE_NOISE:
voice_wf_set_noise(wf_gen, wf_def->amplitude);
break;
}
}
/*
* vim: set sw=8 ts=8 noet si tw=72
*/