-
Notifications
You must be signed in to change notification settings - Fork 0
/
synth.c
634 lines (559 loc) · 17.3 KB
/
synth.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "synth.h"
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#define PCM_DEVICE "default"
#ifndef M_PI
#define M_PI 3.14159365359879323846
#define M_2PI 6.28318530718
#define M_PI2 1.57079632679
#endif
#ifndef alloca
#define alloca(x) __builtin_alloca(x)
#endif
#ifdef DEBUG
#define DEBUG_VAL 1
#else
#define DEBUG_VAL 0
#endif
#define debug_print(fmt, ...) \
do { if(DEBUG_VAL) printf(fmt, __VA_ARGS__); } while(0)
#define LINE_MAX_LEN 255
#define NANO 1000000000
unsigned int rate = 44100; // samples per second
struct timespec start_time;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
long long int timespec_to_nsecs(struct timespec *t) {
return (long long int)t->tv_sec * (long long int)NANO +
(long long int)t->tv_nsec;
}
void nsecs_to_timespec(long long int nsecs, struct timespec *t) {
t->tv_sec = nsecs / NANO;
t->tv_nsec = nsecs % NANO;
}
// When b > a both the seconds and nanoseconds part will be -ve
static inline void timespec_diff(struct timespec *a, struct timespec *b, struct timespec *result) {
long long int result_nsecs = timespec_to_nsecs(a) - timespec_to_nsecs(b);
nsecs_to_timespec(result_nsecs, result);
}
/*************************/
typedef struct mod {
char type[3];
struct mod **inputs;
int *input_idxs;
float *outputs;
void (*tick)(struct mod*);
void *data;
} mod;
int nmods = 0;
mod *mods = NULL;
void init_mods(int n) {
nmods = n;
mods = malloc(n * sizeof(mod));
}
/*************************/
float get_input(mod *m, int i) { return m->inputs[i]->outputs[m->input_idxs[i]]; }
/* CONSTANT OUT VALUE */
typedef struct cst_data {
char label[LINE_MAX_LEN];
char type[3];
float init_val;
float val;
} cst_data;
#define CST_OUT_VAL 0
void cst_tick(mod *m) {
m->outputs[CST_OUT_VAL] = ((cst_data*)m->data)->val;
}
int make_cst(mod *m) {
memcpy(m->type, "CST", 3);
m->outputs = malloc(sizeof(float));
m->tick = &cst_tick;
m->data = malloc(sizeof(cst_data));
memset(m->data, 0, sizeof(cst_data));
return 0;
}
void cst_set_val(mod *m, float val) {
((cst_data*)m->data)->val = val;
}
void cst_set_init_val(mod *m, float val) {
((cst_data*)m->data)->init_val = val;
((cst_data*)m->data)->val = val;
}
void cst_set_type(mod *m, char *type) {
strncpy(((cst_data*)m->data)->type, type, 3);
}
void cst_set_label(mod *m, char *label) {
int pos = -1;
while('\n' != label[++pos])
((cst_data*)m->data)->label[pos] = label[pos];
}
void cst_print(mod *m) {
cst_data *data =((cst_data*)m->data);
printf("CST %f %c%c%c %s\n",
data->init_val,
data->type[0], data->type[1], data->type[2],
data->label);
}
void set_mod_cst_value(int mod_id, float val) {
cst_set_val(&mods[mod_id], val);
}
char* get_mod_cst_label(int mod_id) {
return ((cst_data*)mods[mod_id].data)->label;
}
char* get_mod_cst_type(int mod_id) {
return ((cst_data*)mods[mod_id].data)->type;
}
float get_mod_cst_init_value(int mod_id) {
return ((cst_data*)mods[mod_id].data)->init_val;
}
char *get_mod_type(int mod_id) {
return mods[mod_id].type;
}
int get_nmods() { return nmods; }
#define FAD_IN_SIG1 0
#define FAD_IN_SIG2 1
#define FAD_IN_MIX 2
#define FAD_OUT_VAL 0
void fad_tick(mod *m) {
float s1 = get_input(m, FAD_IN_SIG1);
float s2 = get_input(m, FAD_IN_SIG2);
float mix = get_input(m, FAD_IN_MIX);
float out = s1 * (1 - mix) + s2 * mix;
debug_print("FAD %p - %f, %f @ %f%% = %f\n", (void*)m, s1, s2, mix, out);
m->outputs[FAD_OUT_VAL] = out;
}
int make_fad(mod *m) {
memcpy(m->type, "FAD", 3);
m->inputs = malloc(3 * sizeof(mod*));
m->input_idxs = malloc(3 * sizeof(int));
m->outputs = malloc(sizeof(float));
m->tick = &fad_tick;
return 0;
}
#define ADD_IN1 0
#define ADD_IN2 1
#define ADD_OUT_VAL 0
void add_tick(mod *m) {
float a1 = get_input(m, ADD_IN1);
float a2 = get_input(m, ADD_IN2);
debug_print("ADD %p - %f + %f = %f\n", (void*)m, a1, a2, a1 + a2);
m->outputs[ADD_OUT_VAL] = a1 + a2;
}
int make_add(mod *m) {
memcpy(m->type, "ADD", 3);
m->inputs = malloc(2 * sizeof(mod*));
m->input_idxs = malloc(2 * sizeof(int));
m->outputs = malloc(sizeof(float));
m->tick = &add_tick;
return 0;
}
#define OCC_IN_FREQ 0
#define OCC_OUT_SIN 0
#define OCC_OUT_TRI 1
#define OCC_OUT_SAW 2
#define OCC_OUT_SQU 3
void occ_tick(mod *m) {
float freq_in = get_input(m, OCC_IN_FREQ);
float phase = *(float*)m->data;
phase += freq_in * M_2PI / (float)rate;
phase += ((phase >= M_2PI) * -M_2PI) + ((phase < 0.0) * M_2PI);
//printf("%f %f %f\n", freq_in, freq_in * M_2PI / (float)rate, phase);
*(float*)m->data = phase;
float sample_sin = 0.5 + 0.5 * sin(phase);
float sample_tri = ((phase < M_PI) * phase / M_PI) +
((phase >= M_PI) * (2. - phase / M_PI));
float sample_saw = phase / M_2PI;
float sample_squ = (phase >= M_PI2 && phase < 3 * M_PI2);
//float sample = 0.5 + 0.5 * sin(theta * freq_in);
debug_print("OCC %p - %f @ %f = sin %f, tri %f, saw %f, squ %f\n",
(void*)m, phase, freq_in, sample_sin, sample_tri, sample_saw, sample_squ);
m->outputs[OCC_OUT_SIN] = sample_sin;
m->outputs[OCC_OUT_TRI] = sample_tri;
m->outputs[OCC_OUT_SAW] = sample_saw;
m->outputs[OCC_OUT_SQU] = sample_squ;
}
int make_occ(mod *m) {
memcpy(m->type, "OCC", 3);
m->inputs = malloc(sizeof(mod*));
m->input_idxs = malloc(sizeof(int));
m->outputs = malloc(4 * sizeof(float));
m->data = malloc(sizeof(float));
memset(m->data, 0, sizeof(float));
m->tick = &occ_tick;
return 0;
}
#define VCA_IN_CV 0
#define VCA_IN_SIG 1
#define VCA_OUT_SIG 0
void vca_tick(mod *m) {
float in_cv = get_input(m, VCA_IN_CV);
float in_sig = get_input(m, VCA_IN_SIG);
debug_print("VCA %p - %f * %f = %f\n", (void*)m, in_cv, in_sig, in_cv * in_sig);
m->outputs[VCA_OUT_SIG] = in_cv * in_sig;
}
int make_vca(mod *m) {
memcpy(m->type, "VCA", 3);
m->inputs = malloc(2 * sizeof(mod*));
m->input_idxs = malloc(2 * sizeof(int));
m->outputs = malloc(sizeof(float));
m->tick = &vca_tick;
return 0;
}
#define VCF_IN_CUT 0
#define VCF_IN_RES 1
#define VCF_IN_SIG 2
#define VCF_OUT_SIG 0
#define vcf_stages 4
typedef struct vcf_data {
float sn[vcf_stages]; // s(n)
float snm1[vcf_stages]; // s(n-1)
} vcf_data;
void vcf_tick(mod *m) {
float cut = get_input(m, VCF_IN_CUT);
float res = get_input(m, VCF_IN_RES);
float sig = get_input(m, VCF_IN_SIG);
vcf_data *data = (vcf_data*)m->data;
float tmp;
// Pull data from the last stage
for(int i = vcf_stages -1; i > 0; i--) {
tmp = data->sn[i];
data->sn[i] = data->sn[i-1] * cut + data->snm1[i] * (1.0f - cut);
data->snm1[i] = tmp;
}
tmp = data->sn[0];
data->sn[0] = sig * cut + data->snm1[0] * (1.0f - cut) +
data->snm1[vcf_stages-1] * res * -1. * cut;
data->snm1[0] = tmp;
debug_print("VCF cut %f, res %f, sig %f = %f\n", cut, res, sig,
data->sn[vcf_stages -1]);
m->outputs[VCF_OUT_SIG] = data->sn[vcf_stages -1];
}
int make_vcf(mod *m) {
memcpy(m->type, "VCF", 3);
m->inputs = malloc(3 * sizeof(mod*));
m->input_idxs = malloc(3 * sizeof(int));
m->outputs = malloc(sizeof(float));
m->tick = &vcf_tick;
m->data = (void*)malloc(sizeof(vcf_data));
memset(m->data, 0, sizeof(vcf_data));
return 0;
}
#define ENV_IN_A 0
#define ENV_IN_D 1
#define ENV_IN_S 2
#define ENV_IN_R 3
#define ENV_IN_GATE 4
#define ENV_OUT 0
typedef struct env_data {
int ticks_since_gate_high;
int ticks_since_gate_low;
} env_data;
void env_tick(mod *m) {
float in_a = get_input(m, ENV_IN_A) + 0.00001; // prevent x/0
float in_d = get_input(m, ENV_IN_D) + 0.00001;
float in_s = get_input(m, ENV_IN_S);
float in_r = get_input(m, ENV_IN_R) + 0.00001;
float gate = get_input(m, ENV_IN_GATE);
// Start with just linear AD
env_data *data = (env_data*)m->data;
// last edge was falling
if(data->ticks_since_gate_low < data->ticks_since_gate_high) {
if(gate >= 0.9) { // Just got a rising edge
data->ticks_since_gate_high = 0;
m->outputs[ENV_OUT] = 0.0;
} else { // Falling output
float t = (float)data->ticks_since_gate_low / (float)rate;
m->outputs[ENV_OUT] = (t < in_r) * (1. - (t / in_r));
}
} else { // last edge was rising
if(gate <= 0.1) { // Just got a falling edge
data->ticks_since_gate_low = 0;
} else { // Rising output
float t = (float)data->ticks_since_gate_high / (float)rate;
m->outputs[ENV_OUT] = (t < in_a) * (t / in_a) + (t > in_a) * 1.;
}
}
data->ticks_since_gate_high++;
data->ticks_since_gate_low++;
debug_print("ENV %p - ->0 %i ->1 %i, gate = %f, ADSR = [%f %f %f %f] -> %f\n",
(void*)m,
data->ticks_since_gate_low,
data->ticks_since_gate_high,
gate,
in_a, in_d, in_s, in_r,
m->outputs[ENV_OUT]
);
}
int make_env(mod *m) {
memcpy(m->type, "ENV", 3);
m->inputs = malloc(5 * sizeof(mod*));
m->input_idxs = malloc(5 * sizeof(int));
m->outputs = malloc(sizeof(float));
m->data = malloc(sizeof(env_data));
m->tick = &env_tick;
return 0;
}
#define OTP_IN 0
typedef struct otp_data {
float *fbuf;
int16_t *ibuf;
int i;
} otp_data;
struct timespec last_dump;
void otp_tick(mod *m) {
unsigned int pcm;
float in = get_input(m, OTP_IN);
otp_data *data = (otp_data*)m->data;
data->fbuf[data->i++] = in;
//struct timespec now, elapsed;
// Time to dump the data into the audio buffer?
if(data->i == frames) {
//clock_gettime(CLOCK_REALTIME, &now);
//timespec_diff(&now, &start_time, &elapsed);
//printf("Dump at %fsecs\n", (float)elapsed.tv_sec + (float)elapsed.tv_nsec / (float)NANO);
//timespec_diff(&now, &last_dump, &elapsed);
//printf("Last dump %fsecs ago\n", (float)elapsed.tv_sec + (float)elapsed.tv_nsec / (float)NANO);
//memcpy(&last_dump, &now, sizeof(struct timespec));
for(int i = 0; i < frames; i++) {
data->ibuf[i] = (int16_t)(data->fbuf[i] * 32767.0f);
//printf("%f -> %i\n", data->fbuf[i], data->ibuf[i]);
}
if ((pcm = snd_pcm_writei(pcm_handle, data->ibuf, frames)) == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
data->i = 0;
}
}
int make_otp(mod *m) {
memcpy(m->type, "OTP", 3);
m->inputs = malloc(sizeof(mod*));
m->input_idxs = malloc(sizeof(int));
m->outputs = NULL;
m->tick = &otp_tick;
otp_data *data = (otp_data*)malloc(sizeof(otp_data));
data->fbuf = (float*)malloc(frames * sizeof(float));
data->ibuf = (int16_t*)malloc(frames * sizeof(int));
data->i = 0;
m->data = (void*)data;
return 0;
}
/*************************/
void freadline(char line[LINE_MAX_LEN], FILE *f) {
while(isspace(fgetc(f)));
if(feof(f)) return;
fseek(f, -1, SEEK_CUR);
int i = 0;
line[i] = fgetc(f);
while(line[i] != '\n')
line[++i] = fgetc(f);
}
void parse_input(mod *mods, int m, int input, char *line, int *pos) {
mods[m].inputs[input] = &mods[atoi(&line[*pos])];
while('/' != line[(*pos)++]);
mods[m].input_idxs[input] = atoi(&line[*pos]);
while(isdigit(line[(*pos)++]));
}
void parse_mod_line(mod *mods, char line[LINE_MAX_LEN]) {
int i = 0;
int n = atoi(line);
while(isdigit(line[i++]));
if(0 == strncmp("CST", &line[i], 3)) {
make_cst(&mods[n]);
i += 4;
cst_set_val(&mods[n], atof(&line[i]));
cst_set_init_val(&mods[n], atof(&line[i]));
while(!isspace(line[i++]));
if(0 == strncmp("HFO", &line[i], 3) ||
0 == strncmp("LFO", &line[i], 3) ||
0 == strncmp("PER", &line[i], 3) ||
0 == strncmp("NDS", &line[i], 3)) {
cst_set_type(&mods[n], &line[i]);
i += 4;
}
else
printf("Bad CST UI Type in: %s\n", line);
cst_set_label(&mods[n], &line[i]);
cst_print(&mods[n]);
}
else if(0 == strncmp("ADD", &line[i], 3)) {
make_add(&mods[n]);
i += 4;
parse_input(mods, n, ADD_IN1, line, &i);
parse_input(mods, n, ADD_IN2, line, &i);
}
else if(0 == strncmp("FAD", &line[i], 3)) {
make_fad(&mods[n]);
i += 4;
parse_input(mods, n, FAD_IN_SIG1, line, &i);
parse_input(mods, n, FAD_IN_SIG2, line, &i);
parse_input(mods, n, FAD_IN_MIX, line, &i);
}
else if(0 == strncmp("OCC", &line[i], 3)) {
make_occ(&mods[n]);
i += 4;
parse_input(mods, n, OCC_IN_FREQ, line, &i);
}
else if(0 == strncmp("VCA", &line[i], 3)) {
make_vca(&mods[n]);
i += 4;
parse_input(mods, n, VCA_IN_CV, line, &i);
parse_input(mods, n, VCA_IN_SIG, line, &i);
}
else if(0 == strncmp("VCF", &line[i], 3)) {
make_vcf(&mods[n]);
i += 4;
parse_input(mods, n, VCF_IN_CUT, line, &i);
parse_input(mods, n, VCF_IN_RES, line, &i);
parse_input(mods, n, VCF_IN_SIG, line, &i);
}
else if(0 == strncmp("ENV", &line[i], 3)) {
make_env(&mods[n]);
i += 4;
parse_input(mods, n, ENV_IN_A, line, &i);
parse_input(mods, n, ENV_IN_D, line, &i);
parse_input(mods, n, ENV_IN_S, line, &i);
parse_input(mods, n, ENV_IN_R, line, &i);
parse_input(mods, n, ENV_IN_GATE, line, &i);
}
else if(0 == strncmp("OUT", &line[i], 3)) {
make_otp(&mods[n]);
i += 4;
parse_input(mods, n, OTP_IN, line, &i);
}
else
printf("Bad module type in: %s\n", line);
}
int load_network(char *filename) {
FILE * f = fopen(filename, "r");
// Go to the last line and get the highest mod number.
fseek(f, 1, SEEK_END);
// Ignore whitespace at the end
while(isspace(fgetc(f)))
fseek(f, -2, SEEK_CUR);
// Move back past the \n
fseek(f, -1, SEEK_CUR);
// Find the next carrige return
while(fgetc(f) != '\n')
fseek(f, -2, SEEK_CUR);
// Read the last line
char line[LINE_MAX_LEN] = {0,};
fread(line, 1, LINE_MAX_LEN, f);
// Get the number
init_mods(atoi(line) + 1); // Number from 0
printf("nmods : %i\n", nmods);
rewind(f);
while(!feof(f)) {
memset(line, 0, LINE_MAX_LEN);
freadline(line, f);
printf("%s--\n", line);
parse_mod_line(mods, line);
}
return 0;
}
/*************************/
/* ^^^^ 0.0 -> 1.0+ ^^^^ vvvv -32767 -> 32767 vvvv */
void init_pcm() {
unsigned int pcm, tmp;
/* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
SND_PCM_STREAM_PLAYBACK, 0)) < 0)
printf("ERROR: Can't open \"%s\" PCM device. %s\n",
PCM_DEVICE, snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
SND_PCM_FORMAT_S16_LE)) < 0)
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
/* mono only */
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, 1)) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0)) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
/* Resume information */
printf("PCM Sample size %i bits\n", snd_pcm_hw_params_get_sbits(params));
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
snd_pcm_hw_params_get_period_size(params, &frames, 0);
unsigned int period_time;
int dir;
snd_pcm_hw_params_get_period_time(params, &period_time, &dir);
printf("Need %lu frames in %uus\n", frames, period_time);
}
void *synth_main_loop(void *synth_data) {
synth_thread_data *thread_data = (synth_thread_data*)synth_data;
init_pcm();
unsigned int period_time; // microseconds
int dir;
snd_pcm_hw_params_get_period_time(params, &period_time, &dir);
load_network("layout.dat");
//setup_network();
// Init complete, signal the UI thread
char alive = 1;
pthread_mutex_lock(&thread_data->alive_mtx);
thread_data->alive = alive;
pthread_mutex_unlock(&thread_data->alive_mtx);
uint32_t frames_calced = 0;
float sound_secs;
struct timespec now, elapsed, pause, sound;
long int max_sync_diff = 3 * period_time * 1000; // convert to nanoseconds
clock_gettime(CLOCK_REALTIME, &start_time);
while(alive) {
for(int i = 0; i < nmods; i++)
mods[i].tick(&mods[i]);
frames_calced++;
clock_gettime(CLOCK_REALTIME, &now);
timespec_diff(&now, &start_time, &elapsed);
sound_secs = (float)frames_calced / (float)rate;
sound.tv_sec = (time_t)sound_secs;
sound.tv_nsec = (long)((sound_secs - floor(sound_secs)) * (float)NANO);
timespec_diff(&sound, &elapsed, &pause);
//printf("calced %fsecs of sound in %fsecs\n",
// sound_secs, (float)elapsed.tv_sec + (float)elapsed.tv_nsec / (float)NANO);
if(pause.tv_sec > 3) {
printf("Clock too far out of sync\n");
abort();
}
if(pause.tv_nsec > max_sync_diff) {
//printf("calced %fsecs of sound in %fsecs\n",
// sound_secs, (float)elapsed.tv_sec + (float)elapsed.tv_nsec / (float)NANO);
// Always leave 2 periods in the output buffer
// This avoids the possibility of sleeping through the buffer interupt
pause.tv_nsec -= 2 * max_sync_diff / 3;
printf("Audio calculation too far ahead sleep for %li = %fs\n",
pause.tv_nsec,
(float)pause.tv_nsec / (float)NANO);
nanosleep(&pause, NULL);
}
pthread_mutex_lock(&thread_data->alive_mtx);
alive = thread_data->alive;
pthread_mutex_unlock(&thread_data->alive_mtx);
}
printf("Closing synth\n");
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
printf("Exiting synth\n");
return NULL;
}