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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "synth_engine.h"
#include "lowpass.h"
#include "filter.h"
float
adsr_amplitude(void *synthData, unsigned long long elapsed)
{
synth_t *synth = (synth_t*)synthData;
float dAmplitude = 0.0;
float dReleaseAmplitude = 0.0;
float dStartAmplitude = 1.0;
float dLifeTime = (elapsed * (1.0 / (float)SAMPLE_RATE));
if (synth->n.noteOn != 0 && synth->n.noteOff == 0) {
if (dLifeTime <= synth->adsr.a)
dAmplitude = (dLifeTime / synth->adsr.a)*(dLifeTime / synth->adsr.a) * dStartAmplitude;
if (dLifeTime > synth->adsr.a && dLifeTime <= ( synth->adsr.a + synth->adsr.d))
dAmplitude = ((dLifeTime - synth->adsr.a) / synth->adsr.d) * (synth->adsr.s - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (synth->adsr.a + synth->adsr.d))
dAmplitude = synth->adsr.s;
}
else { // Note is off
if (dLifeTime <= synth->adsr.a)
dReleaseAmplitude = (dLifeTime / synth->adsr.a)*(dLifeTime / synth->adsr.a) * dStartAmplitude;
if (dLifeTime > synth->adsr.a && dLifeTime <= (synth->adsr.a + synth->adsr.d))
dReleaseAmplitude = ((dLifeTime - synth->adsr.a) / synth->adsr.d) * (synth->adsr.s - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (synth->adsr.a + synth->adsr.d))
dReleaseAmplitude = synth->adsr.s;
dAmplitude = (((synth->n.noteOn + dLifeTime) - synth->n.noteOff) / synth->adsr.r) * (0.0 - dReleaseAmplitude) + dReleaseAmplitude;
if (synth->adsr.r < 0) {
dAmplitude = synth->adsr.s;
}
}
// Amplitude should not be negative
if (dAmplitude <= 0.000)
dAmplitude = 0.0;
return dAmplitude;
}
float
sin_sample(float amp, float freq, unsigned long long phase, unsigned int sample_rate)
{
return amp * sinf(2.0 * M_PI * freq * ((float)phase / (float)sample_rate));
}
float
saw_sample(float amp, float freq, unsigned long long phase, unsigned int sample_rate)
{
return amp * (0.17 * (1.0 - (2.0 * M_PI * freq * fmod((float)phase, (float)(sample_rate / (freq)))) / (float)sample_rate));
}
float
sawX_sample(float amp, float freq, float sm, unsigned long long phase, unsigned int sample_rate)
{
float dOutput = 0.0;
for (float n = 1.0; n < sm; n++)
dOutput += (sinf(n * 2.0 * M_PI * freq * ((float)phase / (float)sample_rate))) / n;
return 0.5 * amp * dOutput;
}
float
sqr_sample(float amp, float freq, float duty_cycle, unsigned long long phase, unsigned int sample_rate)
{
if (duty_cycle < 0.0001 || duty_cycle > 0.9999) {
duty_cycle = 0.5;
}
return (fmod((float)phase / (float)sample_rate, 1.0 / freq) < duty_cycle * (1.0 / freq)) ? amp : -amp;
}
float
gen0(float f, unsigned long long phase, float x, unsigned int sample_rate)
{
return sqr_sample(0.1, f, 0.3, phase, sample_rate)
+ sqr_sample(0.1, f * 3.0 / 2.0 , 0.5, phase, sample_rate)
+ saw_sample(0.3, f, phase, sample_rate)
+ sin_sample(0.1, f, phase, sample_rate)
+ sin_sample(0.1, f * 5, phase, sample_rate)
/* + sin_sample(0.1, freq * 50 * 1021, phase, sample_rate) */
/* + sin_sample(0.1, freq * 50 * 3531021, phase, sample_rate) */
+ sin_sample(0.1, f * 7, phase, sample_rate);
}
float
gen1(float f, unsigned long long phase, float x, unsigned int sample_rate)
{
return sawX_sample(0.5, f, 5, phase, sample_rate)
+ saw_sample(0.3, 2 * f / 5, phase, sample_rate)
+ sin_sample(0.2, f * 5.0 / 7.0 , phase, sample_rate);
}
float
gen2(float f, unsigned long long phase, float x, unsigned int sample_rate)
{
return sin_sample(0.5, f * sqrt(2) , phase, sample_rate)
+ sin_sample(0.5, f, phase, sample_rate);
/* sawX_sample(1, synth->freq, 5, phase, sample_rate); */
}
float
gen3(float f, unsigned long long phase, float x, unsigned int sample_rate)
{
return sawX_sample(0.7, f, 5, phase, sample_rate)
+ sin_sample(0.3, 4.0/17.0*f, phase, sample_rate);
/* return sawX_sample(0.5, f * (1 + sqrt(5)) / 2, 5, phase, sample_rate) */
/* + sin_sample(0.3, f * x, phase, sample_rate) */
/* + sqr_sample(0.2, f * x, 0.2 * x * x, phase, sample_rate); */
}
/* 1d convolution */
void
convole(float *signal, float *filter, size_t signal_size, size_t filter_size, float *out) {
for (size_t i = 0; i < filter_size + signal_size; i++) {
size_t kmin, kmax, k;
out[i] = 0;
/* find overlap */
kmin = (i >= filter_size - 1) ? i - (filter_size - 1) : 0;
kmax = (i < signal_size - 1) ? i : signal_size - 1;
/* Add the overlaping values */
for (k = kmin; k <= kmax; k++) {
out[i] += signal[k] * filter[i - k];
}
}
}
void
low_pass_filter(float* signal, int length, float cutoff, float resonance, float* out) {
float c = 1.0f / tanf(M_PI * cutoff); // calculate filter constant
float a1 = 1.0f / (1.0f + resonance * c + c * c); // calculate filter coefficients
float a2 = 2.0f * a1;
float a3 = a1;
float b1 = 2.0f * (1.0f - c * c) * a1;
float b2 = (1.0f - resonance * c + c * c) * a1;
float prev_input = 0.0f, prev_output = 0.0f; // initialize previous input and output to zero
for (int i = 0; i < length; i++) {
float input = signal[i];
float output = a1 * input + a2 * prev_input + a3 * prev_output - (i >= 1 ? b1 * out[i-1] : 0.0f) - (i >= 2 ? b2 * out[i-2] : 0.0f);
out[i] = output;
prev_input = input;
prev_output = output;
}
}
float
make_sample(unsigned long long phase, void *synthData, unsigned int sample_rate, int viz)
{
synth_t *synth = (synth_t*)synthData;
float sample = 0;
int n = 1;
if (1 /* !synth->filter */) {
for (int i = 0; i < n; i++) {
sample += (1.0 / n) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x, sample_rate);
}
if (!viz && synth->filter) {
// ALLL THE FILTERS
LowPass_Update(synth->resonance, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * synth->cutoff + 1, sample_rate);
sample = LowPass_Filter(sample);
update_bw_low_pass_filter(synth->fff, SAMPLE_RATE, (adsr_amplitude(synth, synth->adsr.elapsed) + 0.1) * synth->cutoff, synth->resonance);
sample = bw_low_pass(synth->fff, sample);
}
sample = synth->gain * adsr_amplitude(synth, synth->adsr.elapsed) *
sample; //synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase, synth->x);
if (synth->clamp && sample >= 1) sample = 0.99;
if (synth->clamp && sample <= -1) sample = -0.99;
} else {
// get sample array s[]
int samples = 30;
float s[samples];
if (synth->adsr.elapsed < samples) {
for (int i = 0; i < samples; i++) {
s[i] = synth->gain * adsr_amplitude(synth, i) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, i, synth->x, sample_rate);
}
} else {
for (int i = 0; i < samples; i++) {
s[i] = synth->gain * adsr_amplitude(synth, synth->adsr.elapsed - 50 + i) * synth->gen[synth->geni](synth->n.freq + synth->freq_offset, phase - 50 + i, synth->x, sample_rate);
}
}
// process s[]
// return s[50]
}
return sample;
}
int
sound_gen(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *synthData)
{
synth_t *synth = (synth_t*)synthData;
float *out = (float*)outputBuffer;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
if (1) {
float s;
for( unsigned long i=0; i<framesPerBuffer; i++ ) {
//get_portaudio_frame(outputBuffer, synth);
s = make_sample(synth->n.elapsed, synth, SAMPLE_RATE, 0);
*out++ = s;
*out++ = s;
synth->adsr.elapsed++;
synth->n.elapsed++;
if (!synth->multi) {
if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
} else {
}
}
} else {
float s[FRAMES_PER_BUFFER * 5];
if (synth->adsr.elapsed < framesPerBuffer * 5) {
for (unsigned long long i = 0; i < framesPerBuffer * 5; i++) {
s[i] = make_sample(i, synth, SAMPLE_RATE, 0);
synth->adsr.elapsed++;
synth->n.elapsed++;
if (!synth->multi) {
if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
} else {
}
}
} else {
for (unsigned long long i = 0; i < framesPerBuffer * 5; i++) {
s[i] = make_sample(synth->n.elapsed - framesPerBuffer * 2 + i, synth, SAMPLE_RATE, 0);
synth->adsr.elapsed++;
synth->n.elapsed++;
if (!synth->multi) {
if (synth->n.elapsed >= (1.0 / synth->n.freq) * SAMPLE_RATE) synth->n.elapsed = 0;
} else {
}
}
}
// filter
// output
if (synth->adsr.elapsed < framesPerBuffer * 5) {
for( unsigned long i=0; i<framesPerBuffer; i++ ) {
*out++ = s[i];
*out++ = s[i];
}
} else {
for( unsigned long i=0; i<framesPerBuffer; i++ ) {
*out++ = s[i + (2 * framesPerBuffer)];
*out++ = s[i + (2 * framesPerBuffer)];
}
}
synth->adsr.elapsed-= 4*framesPerBuffer;
if (!synth->multi) {
for (int i = 0; i < framesPerBuffer * 4; i++) {
synth->n.elapsed--;
if (synth->n.elapsed == 0) synth->n.elapsed = (int)(SAMPLE_RATE / synth->n.freq);
}
} else {
synth->n.elapsed-= 4*framesPerBuffer;
}
}
return paContinue;
}
void
init_synth(synth_t * synth)
{
synth->freq_offset = 0;
synth->gain = 1;
synth->x = 1;
synth->n.freq = 0;
synth->n.noteOn = 0;
synth->n.noteOff = 1;
synth->n.key = 0;
synth->n.elapsed = 0;
synth->adsr.a = 0.001;
synth->adsr.d = 0.3;
synth->adsr.s = 0.7;
synth->adsr.r = 0.4;
synth->adsr.elapsed = 0;
synth->octave = 3;
synth->multi = 0;
synth->filter = 0;
synth->cutoff = 22000.0f;
synth->resonance = 1.0f;
synth->clamp = 0;
synth->gen[0] = gen0;
synth->gen[1] = gen1;
synth->gen[2] = gen2;
synth->gen[3] = gen3;
synth->geni = 0;
synth->viz.sample_rate_divider = 1;
LowPass_Init();
synth->fff = create_bw_low_pass_filter(2, SAMPLE_RATE, 400);
}
|