-
Notifications
You must be signed in to change notification settings - Fork 0
/
miditest.c
625 lines (499 loc) · 13.3 KB
/
miditest.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
#include <stdio.h>
#include <stdlib.h>
#include <CoreMIDI/CoreMIDI.h>
#include <pthread.h>
#include <curses.h>
#include "midi.h"
#include "output.h"
#include "input.h"
#include "harmony.h"
#include "samples.h"
static void notifyproc(const MIDINotification *message, void *refCon) {
}
mqueue_t mqueue;
sound_t *sound = NULL;
fsound_t *fsound = NULL;
static int sound_enabled = 1;
static int harmony_enabled = 0;
static int KB_INPUT_ENABLED = 0;
double modulation = 1.0;
mevent_t mevent_new(int keyindex, double hz, double A, fsample_t *sample) {
mevent_t e;
e.keyindex = keyindex;
e.hz = hz;
e.A = A;
e.t = 0;
e.phase = 0;
e.sample = sample;
e.sample_index = 0;
return e;
}
void mqueue_init(mqueue_t *q) {
memset(q->events, 0x0, sizeof(q->events));
}
void mqueue_add(mqueue_t *q, mevent_t *e) {
//printf("adding %f to queue at index %d, sample: %p\n", e->hz, q->num_events, e->sample);
q->events[q->num_events] = *e;
++q->num_events;
}
void mqueue_delete_at(mqueue_t *q, int i) {
// printf("num_events: %d, i: %d\n", q->num_events, i);
if (i >= q->num_events) {
return; // print a warning maybe
}
for (int j = i; j < q->num_events-1; ++j) {
q->events[j] = q->events[j+1];
}
--q->num_events;
}
void mqueue_purge(mqueue_t *q) {
for (int i = 0; i < q->num_events; ++i) {
if (q->events[i].A < 0.001) {
// this should work?
mqueue_delete_at(q, i);
mqueue_purge(q);
return;
}
}
}
MIDIkey_t keys[128];
int sustain_pedal_down = 0;
static int left_pedal_down = 0;
void printPacketInfo(const MIDIPacket* packet) {
double timeinsec = packet->timeStamp / (double)1e9;
int i;
if (packet->data[0] == 0xFE) return;
printf("[%.3f]: ", timeinsec);
for (i=0; i<packet->length; i++) {
if (packet->data[i] < 0x7f) {
printf("%d ", packet->data[i]);
} else {
printf("0x%X ", packet->data[i]);
}
}
puts("");
}
static void print_currently_pressed_keys() {
for (int m = MIDIKEY_MIN; m < MIDIKEY_MAX; ++m) {
struct MIDIkey_t *k = &keys[m];
if (k->pressed == 1) {
printf("%d ", m);
}
}
printf("\n");
}
enum {
I_UNISON = 0,
I_MINOR2,
I_MAJOR2,
I_MINOR3,
I_MAJOR3,
I_P4,
I_TRITONE,
I_P5,
I_MINOR6,
I_MAJOR6,
I_MINOR7,
I_MAJOR7
};
static inline int interval_to_imask(int interval) {
if (interval == 0) return 0;
else return (1 << interval);
}
static int get_interval_mask(int startindex) {
int mask = 0;
for (int i = startindex; i < 128; ++i) {
if (keys[i].pressed) {
int d = i - startindex;
mask |= (1 << (d % 12));
}
}
return mask;
}
static int get_lowest_pressed() {
for (int i = 0; i < 128; ++i) {
if (keys[i].pressed) return i;
}
return -1;
}
static int get_highest_pressed() {
for (int i = 127; i >= 0; --i) {
if (keys[i].pressed) return i;
}
return -1;
}
static void adjust_intonation(bool use_eqtemp) { // adJUST XDDDD
static double just[] = {
1.0,
25.0/24.0,
9.0/8.0,
6.0/5.0,
//19.0/16.0,
5.0/4.0,
4.0/3.0,
45.0/32.0,
3.0/2.0,
8.0/5.0,
5.0/3.0,
7.0/4.0,
15.0/8.0,
};
int lowest = get_lowest_pressed();
int minor7_adjust = 0;
int p4_adjust = 0;
int minor3_adjust = 0;
int major6_adjust = 0;
if (lowest == -1) { return; }
#define HAS_NOTE(interval) (imask & (interval_to_imask(interval)))
int imask = get_interval_mask(lowest);
if (HAS_NOTE(I_MINOR6)) {
if (HAS_NOTE(I_P4)) {
lowest -= I_P5;
}
else if (!HAS_NOTE(I_MAJOR3)) {
lowest -= I_MAJOR3;
}
}
else if (HAS_NOTE(I_P4) && HAS_NOTE(I_MAJOR6)) {
lowest -= I_P5;
}
else if (HAS_NOTE(I_MINOR3)) {
if (HAS_NOTE(I_MINOR7)) {
minor7_adjust = 1;
if (HAS_NOTE(I_P4)) { // minor11 sounds terrible with this p4
p4_adjust = 1;
}
}
else {
minor3_adjust = 1;
}
} else if (HAS_NOTE(I_MAJOR3) && HAS_NOTE(I_MAJOR6)) {
major6_adjust = 1;
}
else if (HAS_NOTE(I_MAJOR2) && HAS_NOTE(I_TRITONE)) {
lowest -= I_MINOR7;
}
while (lowest < 21) lowest += 12;
MIDIkey_t *lk = &keys[lowest];
double lkorig_hz = lk->hz;
lk->hz = midikey_to_hz(lowest);
if (lkorig_hz != 0) {
double newt = (lkorig_hz * lk->t)/lk->hz;
lk->t = newt;
}
for (int i = lowest + 1; i < 128; ++i) {
MIDIkey_t *k = &keys[i];
if (k->A > 0.001) {
double orig_hz = k->hz;
int interval = (i - lowest);
int times = interval / 12;
int modulo = interval % 12;
if (use_eqtemp) {
k->hz = midikey_to_hz(i);
}
else {
if (modulo == I_MINOR7 && minor7_adjust) {
k->hz = (1<<times) * lk->hz * just[I_MINOR3] * just[I_P5];
}
else if (modulo == I_P4 && p4_adjust) {
k->hz = (1<<times) * lk->hz * just[I_MINOR3]*just[I_MAJOR2];
}
else if (modulo == I_MINOR3 && minor3_adjust) {
k->hz = (1<<times) * lk->hz * (19.0/16.0);
}
else if (modulo == I_MAJOR6 && major6_adjust) {
k->hz = (1<<times) * lk->hz * ((just[I_MAJOR2]*just[I_P5] + just[I_MAJOR3]*just[I_P4]) / 2.0);
}
else {
k->hz = (1<<times) * lk->hz * just[modulo];
}
}
// adjust phase so no clicking occurs
double newt = (orig_hz * k->t)/k->hz;
k->t = newt;
}
else { memset(k, 0, sizeof(*k)); }
}
}
static void key_on(MIDIkey_t *k, UInt8 velocity) {
if (velocity == 0) {
k->pressed = 0;
return;
}
k->pressed = 1;
k->A = (double)velocity/(double)0x7F;
k->A = k->A * k->A; // exponential? ok
}
static void key_on2(UInt8 keyindex, UInt8 velocity) {
if (velocity == 0) {
return;
}
double A = (double)velocity/(double)0x7F;
A *= A; // exponential? ok
mevent_t e = mevent_new(keyindex, midikey_to_hz(keyindex), A,
sound_enabled ? get_fsample(fsound, keyindex) : NULL);
mqueue_add(&mqueue, &e);
if (!harmony_enabled) return;
const voicing_t *v = get_voicing(keyindex);
int i = 0;
while (v->members[i] != E_END) { ++i; }
int highest = v->pitches[i-1];
int bass = keyindex - highest;
// printf("keyindex: %d, highest: %d, bass %d (bass + highest = %d)\n", keyindex, highest, bass, bass + highest);
mevent_t ve = mevent_new(keyindex, midikey_to_hz(bass - 12), 0.8*A,
sound_enabled ? get_fsample(fsound, bass-12) : NULL);
// mqueue_add(&mqueue, &ve);
i = 0;
while (v->members[i] != E_END) {
ve = mevent_new(keyindex, midikey_to_hz(bass + v->pitches[i]), 0.75*A,
sound_enabled ? get_fsample(fsound, bass + v->pitches[i]) : NULL);
mqueue_add(&mqueue, &ve);
++i;
}
}
static void key_off_all() {
for (int i = KEY_MIN; i < KEY_MAX; ++i) {
struct MIDIkey_t *k = &keys[i];
k->pressed = 0;
}
}
void set_keyarray_state(const MIDIPacket *packet) {
UInt8 command = packet->data[0];
UInt8 keyindex = 0;
UInt8 param1 = 0;
UInt8 velocity = 0;
struct MIDIkey_t *k = NULL;
switch (command) {
case MIDI_KEYDOWN:
keyindex = packet->data[1];
velocity = packet->data[2];
k = &keys[keyindex];
key_on(k, velocity);
key_on2(keyindex, velocity);
break;
case MIDI_KEYUP:
// OK so apparently, not all midi devices use this; some of them use a KEYDOWN of velocity 0 instead (see key_on())
keyindex = packet->data[1];
keys[keyindex].pressed = 0;
break;
case MIDI_CONTROL:
#define PEDAL_UNACORDA 0x43
#define PEDAL_MIDDLE 0x42
#define PEDAL_SUSTAIN 0x40
#define MODULATION_WHEEL 0x1
#define VOLUME 0x7
keyindex = packet->data[1];
param1 = packet->data[2];
if (keyindex == PEDAL_MIDDLE) {
if (param1 == 0x7F) {
print_currently_pressed_keys();
}
}
else if (keyindex == PEDAL_SUSTAIN) {
if (param1 == 0x7F) {
sustain_pedal_down = 1;
}
else sustain_pedal_down = 0;
}
else if (keyindex == PEDAL_UNACORDA) {
if (param1 == 0x7F) {
left_pedal_down = 1;
}
else left_pedal_down = 0;
}
else if (keyindex == VOLUME) { // originally MODULATION_WHEEL
modulation = (double)param1 / (double)0x7F;
//printf("modulation: %f\n", modulation);
}
else if (keyindex == VOLUME) {
eqtemp_factor = eqtemp_12 + 0.05*(double)(param1 - 0x3F)/(double)0x7F;
}
break;
default:
break;
}
}
static void readproc(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon) {
MIDIPacket *packet = (MIDIPacket*)pktlist->packet;
int i;
int count = pktlist->numPackets;
for (i=0; i<count; i++) {
//printPacketInfo(packet);
set_keyarray_state(packet);
packet = MIDIPacketNext(packet); // this is quite necessary lol
}
adjust_intonation(!left_pedal_down);
}
char *cfstring_to_cstr(CFStringRef aString) {
if (aString == NULL) {
return NULL;
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize =
CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(aString, buffer, maxSize,
kCFStringEncodingUTF8)) {
return buffer;
}
free(buffer); // If we failed
return NULL;
}
static int select_MIDI_input() {
int device_index = -1;
MIDIClientRef cl;
MIDIClientCreate (CFSTR("MOII"), notifyproc, NULL, &cl);
MIDIPortRef port;
MIDIInputPortCreate(cl, CFSTR("MOII input"), readproc, NULL, &port);
int ndevices = MIDIGetNumberOfSources();
printf("number of MIDI sources: %d\n", ndevices);
if (ndevices < 1) {
fprintf(stderr, "no MIDI input devices detected!\n");
return 0;
}
else if (ndevices == 1) {
MIDIEndpointRef src = MIDIGetSource(0);
CFStringRef name;
OSStatus err = MIDIObjectGetStringProperty(src, kMIDIPropertyName, &name);
char *device_name = cfstring_to_cstr(name);
if (err == noErr) {
printf("Defaulting to input device 0 (%s) (only 1 MIDI input device available)\n", device_name);
}
MIDIPortConnectSource(port, src, NULL);
device_index = 0;
free(device_name);
}
else {
for (int i = 0; i < ndevices; ++i) {
MIDIEndpointRef src = MIDIGetSource(i);
CFStringRef name;
OSStatus err = MIDIObjectGetStringProperty(src, kMIDIPropertyName, &name);
char *device_name = cfstring_to_cstr(name);
if (err == noErr) {
printf("source %d: name: %s\n", i, device_name);
}
printf("If you want to select this MIDI input, enter y:\n");
char buffer[256];
fgets(buffer, 256, stdin);
if (strcmp(buffer, "y\n") == 0) {
MIDIPortConnectSource(port, src, NULL);
if (device_name) free(device_name);
device_index = i;
printf("Device %d selected!\n", i);
break;
}
}
}
if (device_index == -1) {
printf("No device selected! Closing.\n");
return 0;
}
return 1;
}
void init_curses() {
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
mvaddstr(0, 0, "--- JOOH JUUH MIDII JA SILLEE 8) ---");
mvaddstr(2, 0, "* press P to toggle piano sample sound");
mvaddstr(3, 0, "* press H to toggle automatic harmony");
mvaddstr(4, 0, "* press Q to quit");
if (KB_INPUT_ENABLED) {
mvaddstr(5, 0, "* computer keyboard input enabled (D, G, A)");
}
refresh();
}
static void handle_KB_INPUT(int c) {
#define KB_D 46
#define KB_G 51
#define KB_A 53
switch(c) {
// TODO: add rest :D
case 'd':
case 'D':
key_on(&keys[KB_D], 0x7F);
key_on2(KB_D, 0x7F);
break;
case 'g':
case 'G':
key_on(&keys[KB_G], 0x7F);
key_on2(KB_G, 0x7F);
break;
case 'a':
case 'A':
key_on(&keys[KB_A], 0x7F);
key_on2(KB_A, 0x7F);
break;
default:
break;
}
}
void *key_loop(void * _Nullable arg) {
for (;;) {
int c = getch();
switch (c) {
case 'p':
case 'P':
sound_enabled = !sound_enabled;
break;
case 'h':
case 'H':
harmony_enabled = !harmony_enabled;
break;
case 'q':
case 'Q':
endwin();
exit(0);
break;
default:
if (KB_INPUT_ENABLED) {
handle_KB_INPUT(c);
}
break;
}
}
}
static int ask_confirmation(const char* str) {
puts(str);
char ansbuf[16];
fgets(ansbuf, 15, stdin);
ansbuf[15] = '\0';
if (strcmp(ansbuf, "y\n") == 0 || strcmp(ansbuf, "Y\n") == 0) {
return 1;
}
else return 0;
}
int main(int argc, char *args[]) {
srand(time(NULL));
memset(keys, 0, sizeof(keys));
mqueue_init(&mqueue);
init_eqtemp_hztable();
init_voicings();
// sound = load_sound(SAMPLE_TYPE_RAW, "samples/16/raws/piano_%d.raw", 2);
// fsound = load_fsound(SAMPLE_TYPE_RAW, "samples/16/raws/piano_%d.raw", 2);
fsound = load_fsound(SAMPLE_TYPE_RAW, "/Users/elinanora/midikeks/samples/timpani/timpani_%d.raw", 2);
if (!select_MIDI_input()) {
int ans = ask_confirmation("Couldn't find a MIDI input device. Want to use computer keyboard instead? [y/n]");
if (!ans) {
return 1;
}
else {
printf("Enabling computer keyboard \"MIDI\" input\n");
KB_INPUT_ENABLED = 1;
}
}
if (!init_output()) return 1;
// if (!init_input()) return 1;
// start_input();
init_curses();
pthread_t th;
pthread_create(&th, NULL, key_loop, NULL);
CFRunLoopRef runLoop;
runLoop = CFRunLoopGetCurrent();
CFRunLoopRun();
pthread_exit(NULL);
return 0;
}