-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_midi.h
399 lines (334 loc) · 10.8 KB
/
platform_midi.h
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
#ifndef _PLATFORM_MIDI_H_
#define _PLATFORM_MIDI_H_
struct platform_midi_driver;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct platform_midi_driver* (*platform_midi_init_fn)(const char *, void*);
typedef void (*platform_midi_deinit_fn)(struct platform_midi_driver*);
typedef int (*platform_midi_read_fn)(struct platform_midi_driver*, unsigned char*, int);
typedef int (*platform_midi_write_fn)(struct platform_midi_driver*, const unsigned char*, int);
typedef int (*platform_midi_avail_fn)(struct platform_midi_driver*);
struct platform_midi_driver* platform_midi_init(const char *name);
void platform_midi_deinit(struct platform_midi_driver *driver);
int platform_midi_read(struct platform_midi_driver *driver, unsigned char *out, int size);
int platform_midi_avail(struct platform_midi_driver *driver);
int platform_midi_write(struct platform_midi_driver *driver, const unsigned char *buf, int size);
#if defined(__linux) || defined(__linux__) || defined(linux) || defined(__LINUX__)
#define PLATFORM_MIDI_ALSA_RAWMIDI 1
#define PLATFORM_MIDI_ALSA 1
#elif defined(__APPLE__)
#define PLATFORM_MIDI_COREMIDI 1
#else
#define PLATFORM_MIDI_WINMM 1
#endif
#ifndef PLATFORM_MIDI_EVENT_BUFFER_ITEMS
#define PLATFORM_MIDI_EVENT_BUFFER_ITEMS 32
#endif
#ifndef PLATFORM_MIDI_EVENT_BUFFER_SIZE
#define PLATFORM_MIDI_EVENT_BUFFER_SIZE 1024
#endif
#ifdef PLATFORM_MIDI_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct platform_midi_packet_info
{
unsigned int offset;
unsigned int length;
};
struct platform_midi_ringbuf
{
unsigned char buffer[PLATFORM_MIDI_EVENT_BUFFER_SIZE];
struct platform_midi_packet_info packets[PLATFORM_MIDI_EVENT_BUFFER_ITEMS];
unsigned int read_pos;
unsigned int write_pos;
unsigned int buffer_offset;
unsigned int buffer_end;
};
#define platform_midi_buffer_empty(buf) (buf->read_pos == buf->write_pos)
static void platform_midi_push_packet(struct platform_midi_ringbuf *buf, unsigned char *data, unsigned int length)
{
if ((buf->write_pos + 1) % PLATFORM_MIDI_EVENT_BUFFER_ITEMS == buf->read_pos)
{
printf("Warn: MIDI packet buffer is full, dropping an event\n");
// The buffer is full... gotta drop a packet
buf->buffer_offset = (buf->buffer_offset + buf->packets[buf->read_pos].length) % PLATFORM_MIDI_EVENT_BUFFER_SIZE;
buf->read_pos = (buf->read_pos + 1) % PLATFORM_MIDI_EVENT_BUFFER_ITEMS;
}
buf->packets[buf->write_pos].offset = buf->buffer_end;
buf->packets[buf->write_pos].length = length;
if (buf->buffer_end + length <= PLATFORM_MIDI_EVENT_BUFFER_SIZE)
{
memcpy(&buf->buffer[buf->buffer_end], data, length);
}
else
{
unsigned int startLen = PLATFORM_MIDI_EVENT_BUFFER_SIZE - buf->buffer_end;
memcpy(&buf->buffer[buf->buffer_end], data, startLen);
memcpy(buf->buffer, data + startLen, length - startLen);
}
buf->buffer_end = (buf->buffer_end + length) % PLATFORM_MIDI_EVENT_BUFFER_SIZE;
buf->write_pos = (buf->write_pos + 1) % PLATFORM_MIDI_EVENT_BUFFER_ITEMS;
}
static int platform_midi_convert_from_ump(unsigned char *out, unsigned int maxlen, const unsigned int *umpWords, unsigned int wordCount)
{
unsigned int written = 0;
unsigned char type = (umpWords[0] & 0xF0000000) >> 28;
//unsigned char group = (umpWords[0] & 0x0F000000) >> 24;
switch (type)
{
case 0x01:
{
// System real-time / system common
unsigned char status = (umpWords[0] & 0x00FF0000) >> 20;
unsigned int packetLen = 1;
switch (status)
{
case 0xF1:
case 0xF3:
{
packetLen = 2;
break;
}
case 0xF2:
{
packetLen = 3;
break;
}
default:
break;
}
if (written + packetLen > maxlen)
{
return written;
}
out[written++] = status;
if (packetLen > 1)
{
out[written++] = (umpWords[0] & 0x0000FF00) >> 8;
if (packetLen > 2)
{
out[written++] = (umpWords[0] & 0x000000FF);
}
}
break;
}
case 0x02:
{
// Channel voice
unsigned char status = (umpWords[0] & 0x00F00000) >> 20;
unsigned int packetLen = 3;
switch (status)
{
case 0xC:
case 0xD:
{
packetLen = 2;
break;
}
default:
break;
}
if (written + packetLen > maxlen)
{
return written;
}
out[written++] = (umpWords[0] & 0x00FF0000) >> 16;
out[written++] = (umpWords[0] & 0x0000FF00) >> 8;
if (packetLen > 2)
{
out[written++] = umpWords[0] & 0x000000FF;
}
break;
}
case 0x03:
{
// Data (SysEx)
// TODO
break;
}
}
return written;
}
static int platform_midi_convert_to_ump(unsigned int *out, unsigned int maxWords, const unsigned char *data, unsigned int dataLen)
{
unsigned int *ump = out;
unsigned int read = 0;
while (ump < out + maxWords && read < dataLen)
{
// Always 1
unsigned char group = 0;
// default 2: Channel Voice
unsigned char type = 2;
if (data[read] == 0xF7)
{
type = 3;
}
else if ((data[read] & 0xF0) == 0xF0)
{
type = 1;
}
*ump = ((type & 0xF) << 28) | ((group & 0xF) << 24);
int bits = 16;
while (read < dataLen)
{
*ump |= data[read++] << bits;
bits -= 8;
if (bits == 0)
{
ump++;
}
}
}
return ump - out;
}
static int platform_midi_packet_count(struct platform_midi_ringbuf *buf)
{
if (buf->read_pos == buf->write_pos)
{
return 0;
}
else if (buf->read_pos < buf->write_pos)
{
return buf->write_pos - buf->read_pos;
}
else
{
return PLATFORM_MIDI_EVENT_BUFFER_ITEMS - buf->read_pos - buf->write_pos;
}
}
static int platform_midi_pop_packet(struct platform_midi_ringbuf *buf, unsigned char *out, unsigned int size)
{
if (buf->read_pos == buf->write_pos)
{
return 0;
}
struct platform_midi_packet_info *packet = &buf->packets[buf->read_pos];
int toCopy = (size < packet->length) ? size : packet->length;
// TODO detect large events overrunning the buffer
if (packet->offset + packet->length > PLATFORM_MIDI_EVENT_BUFFER_SIZE)
{
// event is split across the end and beginning of the buffer
int endCopy = (packet->offset + toCopy <= PLATFORM_MIDI_EVENT_BUFFER_SIZE) ? toCopy : PLATFORM_MIDI_EVENT_BUFFER_SIZE - packet->offset;
int startCopy = packet->length - endCopy;
memcpy(out, &buf->buffer[packet->offset], endCopy);
memcpy(out + endCopy, buf->buffer, startCopy);
}
else
{
memcpy(out, &buf->buffer[packet->offset], toCopy);
}
buf->buffer_offset = (buf->buffer_offset + packet->length) % PLATFORM_MIDI_EVENT_BUFFER_SIZE;
buf->read_pos = (buf->read_pos + 1) % PLATFORM_MIDI_EVENT_BUFFER_ITEMS;
return toCopy;
}
static int platform_midi_buffer_init(struct platform_midi_ringbuf *buf)
{
buf->read_pos = 0;
buf->write_pos = 0;
buf->buffer_offset = 0;
buf->buffer_end = 0;
memset(buf->buffer, 0, PLATFORM_MIDI_EVENT_BUFFER_SIZE);
return 1;
}
static void platform_midi_buffer_deinit(struct platform_midi_ringbuf *buf)
{
platform_midi_buffer_init(buf);
}
struct platform_midi_driver
{
platform_midi_deinit_fn deinitFn;
platform_midi_avail_fn availFn;
platform_midi_read_fn readFn;
platform_midi_write_fn writeFn;
void *data;
};
#endif
//#define PLATFORM_MIDI_DRIVER_NULL { 1, "NULL", platform_midi_init_null }
//#include "platform_midi_null.h"
#define PLATFORM_MIDI_DRIVER_NULL { 0, 0, 0 }
#ifdef PLATFORM_MIDI_ALSA
#define PLATFORM_MIDI_DRIVER_ALSA { 2, "ALSA-Sequencer", platform_midi_init_alsa }
#include "platform_midi_alsa.h"
#else
#define PLATFORM_MIDI_DRIVER_ALSA { 0, 0, 0 }
#endif
#ifdef PLATFORM_MIDI_ALSA_RAWMIDI
#define PLATFORM_MIDI_DRIVER_ALSA_RAWMIDI { 1, "ALSA-RawMIDI", platform_midi_init_alsa_rawmidi }
#include "platform_midi_alsa_rawmidi.h"
#else
#define PLATFORM_MIDI_DRIVER_ALSA_RAWMIDI { 0, 0, 0 }
#endif
#ifdef PLATFORM_MIDI_COREMIDI
#define PLATFORM_MIDI_DRIVER_COREMIDI { 2, "CoreMIDI", platform_midi_init_coremidi }
#include "platform_midi_coremidi.h"
#else
#define PLATFORM_MIDI_DRIVER_COREMIDI { 0, 0, 0 }
#endif
#ifdef PLATFORM_MIDI_WINMM
#define PLATFORM_MIDI_DRIVER_WINMM { 2, "WinMM", platform_midi_init_winmm }
#include "platform_midi_winmm.h"
#else
#define PLATFORM_MIDI_DRIVER_WINMM { 0, 0, 0 }
#endif
#ifdef PLATFORM_MIDI_JACK
#define PLATFORM_MIDI_DRIVER_JACK { 3, "JACK", platform_midi_init_jack }
#include "platform_midi_jack.h"
#else
#define PLATFORM_MIDI_DRIVER_JACK { 0, 0, 0 }
#endif
struct platform_midi_driver_def
{
int priority;
const char *name;
platform_midi_init_fn initFn;
};
static const struct platform_midi_driver_def PLATFORM_MIDI_DRIVERS[] = {
PLATFORM_MIDI_DRIVER_NULL,
PLATFORM_MIDI_DRIVER_ALSA,
PLATFORM_MIDI_DRIVER_ALSA_RAWMIDI,
PLATFORM_MIDI_DRIVER_COREMIDI,
PLATFORM_MIDI_DRIVER_WINMM,
PLATFORM_MIDI_DRIVER_JACK,
};
struct platform_midi_driver* platform_midi_init(const char* name)
{
const struct platform_midi_driver_def *driver = &PLATFORM_MIDI_DRIVERS[0];
for (int i = 0; i < sizeof(PLATFORM_MIDI_DRIVERS) / sizeof(PLATFORM_MIDI_DRIVERS[0]); i++)
{
if (PLATFORM_MIDI_DRIVERS[i].priority > driver->priority && PLATFORM_MIDI_DRIVERS[i].initFn)
{
driver = &PLATFORM_MIDI_DRIVERS[i];
}
}
if (!driver->initFn)
{
printf("ERR: No suitable MIDI backends found.\n");
return NULL;
}
return driver->initFn(name, NULL);
}
void platform_midi_deinit(struct platform_midi_driver* driver)
{
if (driver && driver->deinitFn)
{
driver->deinitFn(driver);
}
}
int platform_midi_read(struct platform_midi_driver* driver, unsigned char * out, int size)
{
return driver->readFn(driver, out, size);
}
int platform_midi_avail(struct platform_midi_driver* driver)
{
return driver->availFn(driver);
}
int platform_midi_write(struct platform_midi_driver* driver, const unsigned char* buf, int size)
{
return driver->writeFn(driver, buf, size);
}
#ifdef __cplusplus
};
#endif
#endif