-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
466 lines (367 loc) · 12.2 KB
/
output.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include "midi.h"
#include "output.h"
#include "input.h"
//typedef OSStatus (*AURenderCallback)(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
const SAMPLETYPE SHORT_MAX = 32767.0;
const SAMPLETYPE SHORT_MAX_I = 1.0/SHORT_MAX;
const double eqtemp_12 = 1.0594630943592953;
double eqtemp_factor = eqtemp_12;
const double TWOPI = 6.2831853071795865;
static double eqtemp_hz[128];
void init_eqtemp_hztable() {
for (int i = 21; i < 128; ++i) {
eqtemp_hz[i] = 27.5*pow(eqtemp_factor, (i-21));
keys[i].hz = eqtemp_hz[i];
}
}
static const double GLOBAL_DT = 1.0/44100.0;
static inline double waveform_sine(double freq, double t, double phi) {
return sin(freq * TWOPI * t + phi);
}
static double waveform_triangle(double freq, double t, double phi) {
float p = (TWOPI*freq*t + phi)/TWOPI;
float phase = p - floor(p);
return phase < 0.5 ? (-4*phase + 1) : (4*phase - 3);
}
static inline double limit(double d, double limit_abs) {
if (d > limit_abs) {
d = limit_abs;
}
if (d < -limit_abs) {
d = -limit_abs;
}
return d;
}
static inline double waveform_sine_limit(double freq, double t, double phi, double limit_abs) {
double h = (27.5/freq);
double h2 = 0.5*h;
double d = sin(freq * TWOPI * t + phi)
- h * sin(2*freq*TWOPI*t + phi)
+ h * sin(3*freq*TWOPI*t + phi)
- h2 * waveform_triangle(4*freq, t, phi);
return limit(d, limit_abs);
}
static double *sine_precalculated;
#define PRECALC_SINE_RESOLUTION (2*1024) // this is plenty. sounds pretty ok with 1024 :D
static double *precalculate_sinusoid() {
double* w = malloc(PRECALC_SINE_RESOLUTION*sizeof(double));
double dp = TWOPI / (double)PRECALC_SINE_RESOLUTION;
for (int i = 0; i < PRECALC_SINE_RESOLUTION; ++i) {
w[i] = sin(dp*i);
}
return w;
}
static double pcsin(double s) {
double m = fmod(s, TWOPI)/TWOPI;
int index = m * (double)(PRECALC_SINE_RESOLUTION-1); // will get 'floored', but doesn't matter probably
return sine_precalculated[index];
}
static double pcwaveform_sine_limit(double freq, double t, double phi, double limit_abs) {
double h = (27.5/freq);
double h2 = 0.5*h;
double d = pcsin(freq * TWOPI * t + phi)
- h * pcsin(2*freq*TWOPI*t + phi)
+ h * pcsin(3*freq*TWOPI*t + phi)
- h2 * waveform_triangle(4*freq, t, phi);
return limit(d, limit_abs)/limit_abs;
}
static double pcwaveform_synthpiano(double freq, double t, double phi) {
double T = freq*TWOPI*t;
double d = pcsin(1*T + phi) +
0.75*pcsin(2*T + phi) -
0.20*pcsin(3*T + phi) +
0.23*pcsin(4*T + phi) -
0.02*pcsin(5*T + phi) +
0.03*pcsin(6*T + phi) -
0.003*pcsin(7*T + phi) +
0.003*pcsin(8*T + phi) -
0.006*pcsin(9*T + phi);
return d;
}
double midikey_to_hz(int index) {
// key 21 maps to lowest A (27.5 Hz @ A=440Hz)
// key 108 maps to highest C
// return 27.5*pow(eqtemp_factor, (index-21));
return eqtemp_hz[index];
}
double frand(double fMin, double fMax) {
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
float midikey_to_hz_random(int index) {
static float hz[128];
static int initialized = 0;
if (!initialized) {
hz[0] = 27.5;
for (int i = 1; i < 128; ++i) {
hz[i] = hz[i-1] * frand(0.94, 1.12);
}
initialized = 1;
}
return hz[index];
}
static short *dumpdata;
static int dumpoffset = 0;
static int dumping = 0;
#define DUMPSAMPLES (44100 * 30)
static int start_dumping() {
if (dumping == 0) {
printf("starting dump!\n");
dumpdata = malloc(DUMPSAMPLES*sizeof(short));
dumping = 1;
}
return 0;
}
static int stop_dumping() {
if (dumping == 1) {
printf("stopping dump\n");
FILE *fp = fopen("dumpfile.dat", "wb");
fwrite(dumpdata, sizeof(short), DUMPSAMPLES, fp);
fclose(fp);
free(dumpdata);
dumping = 2;
}
return 1;
}
static int append_to_dump(short *buffer, int num_samples) {
if (dumping != 1) return 0;
memcpy(&dumpdata[dumpoffset], buffer, num_samples*sizeof(short));
dumpoffset += num_samples;
return dumpoffset;
}
static int get_current_dumpoffset() {
return dumpoffset;
}
static inline SAMPLETYPE clamp(SAMPLETYPE in, SAMPLETYPE min, SAMPLETYPE max) {
if (in > max) return max;
if (in < min) return min;
return in;
}
static OSStatus rcallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) {
//printf("%f\n", inTimeStamp->mSampleTime);
AudioBuffer *b = &ioData->mBuffers[0];
short *samples = (short*)b->mData;
// memset(samples, 0, b->mDataByteSize);
int num_keys = 0;
static SAMPLETYPE fbuf[1024*2]; // the buffer is most likely never going to be over 32 samples (because of delay) so this should be fine
memset(fbuf, 0, sizeof(fbuf));
static short delaybuf[1024*2];
// float *input = get_input_data();
//
// for (int m = 20; m < 109; ++m) {
// struct MIDIkey_t *k = &keys[m];
// if (k->A > 0.001) {
//#ifdef DUMP
// start_dumping();
//#endif
// for (int i = 0; i < PREFERRED_FRAMESIZE; ++i) {
// //double tv = 0.0003*sin(5*TWOPI*t);
// //samples[i] += A * k->A * waveform_sine_limit(k->hz, k->t, 0, 0.5);
//// samples[i] += A * k->A * pcwaveform_sine_limit(k->hz, k->t, 0, 0.5);
// samples[i] += 0.3 * A * k->A * pcwaveform_synthpiano(k->hz, k->t, 0);
// // samples[i] += 0.3 * A * k->A * input[i];
// k->t += GLOBAL_DT;
// }
//
// k->phase = k->hz * k->t * TWOPI + 0;
//
// if (k->pressed || sustain_pedal_down) {
// k->A *= 0.9995;
// } else {
// k->A *= 0.99;
// }
// ++num_keys;
// }
// else {
// k->t = 0;
// k->phase = 0;
// }
//
// }
int NUM_FRAMES = b->mDataByteSize/sizeof(short)/b->mNumberChannels;
if (b->mNumberChannels == 1) {
for (int m = 0; m < mqueue.num_events; ++m) {
mevent_t *e = &mqueue.events[m];
for (int i = 0; i < NUM_FRAMES; ++i) {
samples[i] += 0.3 * SHORT_MAX * e->A *
pcwaveform_sine_limit(e->hz, e->t, 0, modulation);
//pcwaveform_synthpiano(e->hz, e->t, 0);
e->t += GLOBAL_DT;
}
e->phase = e->hz * e->t * TWOPI;
}
}
else if (b->mNumberChannels == 2) {
// the data is expected to be in an interleaved arrangement
for (int m = 0; m < mqueue.num_events; ++m) {
mevent_t *e = &mqueue.events[m];
if (e->sample) {
if (e->sample_index >= e->sample->num_frames - 1) {
e->A = 0;
}
else {
for (int i = 0; i < NUM_FRAMES; ++i) {
// e->A for samples has a separate block :)
SAMPLETYPE fA = 0.33 * e->A;
SAMPLETYPE Lval = fA * e->sample->samples[e->sample_index];
SAMPLETYPE Rval = fA * e->sample->samples[e->sample_index+1];
fbuf[2*i] += Lval;
fbuf[2*i + 1] += Rval;
e->t += GLOBAL_DT;
e->sample_index += 2;
if (!keys[e->keyindex].pressed) {
//e->A *= exp(-0.0008*e->t);
e->A *= 0.9995;
}
}
}
}
else {
for (int i = 0; i < NUM_FRAMES; ++i) {
SAMPLETYPE val = 0.5 * e->A *
pcwaveform_sine_limit(e->hz, e->t, 0, modulation);
fbuf[2*i] += val;
fbuf[2*i+1] += val;
e->t += GLOBAL_DT;
if (keys[e->keyindex].pressed) {
e->A *= 0.99998;
}
else {
e->A *= 0.999;
}
}
}
e->phase = e->hz * e->t * TWOPI;
}
for (int i = 0; i < b->mDataByteSize/sizeof(short)/b->mNumberChannels; ++i) {
samples[2*i] = SHORT_MAX * (clamp(fbuf[2*i], -1.0, 1.0));
samples[2*i + 1] = SHORT_MAX * (clamp(fbuf[2*i + 1], -1.0, 1.0));
}
// memcpy(delaybuf, samples, b->mDataByteSize);
}
mqueue_purge(&mqueue);
#ifdef DUMP
if (get_current_dumpoffset() + PREFERRED_FRAMESIZE >= DUMPSAMPLES) stop_dumping();
if (dumping == 1) append_to_dump(samples, PREFERRED_FRAMESIZE);
#endif
return noErr;
}
enum format_type {
FMT_NULL,
FMT_S16_LE,
FMT_S16_BE,
FMT_S32_LE,
FMT_S32_BE,
FMT_FLOAT
};
struct CoreAudioFormatDescription{
enum format_type type;
int bits_per_sample;
int bytes_per_sample;
unsigned int flags;
};
static struct CoreAudioFormatDescription format_map[] = {
{FMT_S16_LE, 16, sizeof (int16_t), kAudioFormatFlagIsSignedInteger},
{FMT_S16_BE, 16, sizeof (int16_t), kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsBigEndian},
{FMT_S32_LE, 32, sizeof (int32_t), kAudioFormatFlagIsSignedInteger},
{FMT_S32_BE, 32, sizeof (int32_t), kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsBigEndian},
{FMT_FLOAT, 32, sizeof (float), kAudioFormatFlagIsFloat},
{FMT_NULL, 0, 0, 0},
};
static AudioComponent output_comp;
static AudioComponentInstance output_instance;
static AudioComponent input_comp;
static AudioComponentInstance input_instance;
static bool init (void) {
/* open the default audio device */
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
output_comp = AudioComponentFindNext (NULL, &desc);
if (!output_comp) {
fprintf (stderr, "Failed to open default audio device.\n");
return false;
}
if (AudioComponentInstanceNew(output_comp, &output_instance)) {
fprintf (stderr, "Failed to open default audio device.\n");
return false;
}
return true;
}
static void cleanup (void) {
AudioUnitUninitialize(output_instance);
}
static bool open_audio (int format, int rate, int channels, AURenderCallbackStruct *callback) {
struct CoreAudioFormatDescription *it = &format_map[0];
struct CoreAudioFormatDescription *m = NULL;
while (it->type != FMT_NULL) {
if (it->type == format) {
m = it;
break;
}
++it;
}
if (!m) {
fprintf (stderr, "The requested audio format %d is unsupported.\n", format);
return false;
}
if (AudioUnitInitialize (output_instance)) {
fprintf (stderr, "Unable to initialize audio unit instance\n");
return false;
}
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = rate;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags = m->flags;
streamFormat.mFramesPerPacket = 1;
streamFormat.mChannelsPerFrame = channels;
streamFormat.mBitsPerChannel = m->bits_per_sample;
streamFormat.mBytesPerPacket = channels * m->bytes_per_sample;
streamFormat.mBytesPerFrame = channels * m->bytes_per_sample;
printf ("Stream format:\n");
printf (" Channels: %d\n", streamFormat.mChannelsPerFrame);
printf (" Sample rate: %f\n", streamFormat.mSampleRate);
printf (" Bits per channel: %d\n", streamFormat.mBitsPerChannel);
printf (" Bytes per frame: %d\n", streamFormat.mBytesPerFrame);
if (AudioUnitSetProperty (output_instance, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, sizeof(streamFormat)))
{
fprintf (stderr, "Failed to set audio unit input property.\n");
return false;
}
if (AudioUnitSetProperty (output_instance, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, callback, sizeof (AURenderCallbackStruct))) {
fprintf (stderr, "Unable to attach an IOProc to the selected audio unit.\n");
return false;
}
UInt32 framesize = PREFERRED_FRAMESIZE;
if (AudioUnitSetProperty(output_instance, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &framesize, sizeof(UInt32))) {
fprintf(stderr, "SetProperty for kAudioDevicePropertyBufferFrameSize failed :(\n");
return false;
}
if (AudioOutputUnitStart (output_instance)) {
fprintf (stderr, "Unable to start audio unit.\n");
return false;
}
return true;
}
static void close_audio (void) {
AudioOutputUnitStop (output_instance);
}
int init_output() {
if (!init()) return 0;
sine_precalculated = precalculate_sinusoid();
AURenderCallbackStruct cb;
memset(&cb, 0, sizeof(cb));
cb.inputProc = rcallback;
cb.inputProcRefCon = NULL;
if (!open_audio(FMT_S16_LE, 44100, 2, &cb)) return 0;
return 1;
}