-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi_queue.jsfx-inc
More file actions
354 lines (283 loc) · 8.53 KB
/
midi_queue.jsfx-inc
File metadata and controls
354 lines (283 loc) · 8.53 KB
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
desc:Sample accurate MIDI queue
// Copyright (C) 2012-2022 Theo Niessink <theo@taletn.com>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
/* Example #1 (simple interface)
desc:Simple MIDI mono synth
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc
@sample
// Receive MIDI messages.
while(midi.midiq_recv()) (
// Parse MIDI message.
midi.msg1 &= 0xF0;
// Note On
midi.msg1 == 0x90 && midi.msg3 ? (
osc.poly_setf(note = midi.msg2, 440);
osc.a *= 0.5 * midi.msg3 / 128;
) :
// Note Off
midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
midi.msg2 == note ? osc.a = 0;
);
);
// Sawtooth oscillator.
spl0 = spl1 = osc.poly_saw();
Example #2 (queue interface)
desc:MIDI queue mono synth
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc
@init
// Set MIDI queue local memory index and size.
midi.midiq_init(0, 1024);
@block
// Receive MIDI messages and add them to queue.
midi.midiq_collect();
@sample
// Remove MIDI message from the head of queue.
while(midi.midiq_remove()) (
// Parse MIDI message.
midi.msg1 &= 0xF0;
// Note On
midi.msg1 == 0x90 && midi.msg3 ? (
osc.poly_setf(note = midi.msg2, 440);
osc.a *= 0.5 * midi.msg3 / 128;
) :
// Note Off
midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
midi.msg2 == note ? osc.a = 0;
);
);
// Sawtooth oscillator.
spl0 = spl1 = osc.poly_saw();
Simple Interface
* midiq_recv()
Context: @sample
Example: is_avail = midi.midiq_recv();
Receives any MIDI message, and stores it in the msg1, msg2, and msg3
instance variables.
Note: The simple interface doesn't actually queue any MIDI messages,
so it can and should be used on its own (i.e. without the other
functions).
* midiq_recv_sysex()
Context: @sample
Example: is_avail = (len = midi.midiq_recv_sysex()) > 0;
Like midiq_recv(), but supports regular 3-byte MIDI messages as well
as SysEx messages. Stores the first 3 bytes in msg1, msg2, and msg3,
but also stores the full message in the buf[] instance variable, and
returns the message length.
Initialization Functions
* midiq_init(index[, size])
Example: midi.midiq_init(0, 1024);
Sets the offset and size (omit for non-bounded size) of the local
memory buffer to store the queue in, and returns the next available
memory index (i.e. index+size*3).
* midiq_init_sysex(index, size)
Example: midi.midiq_init_sysex(0, 1024);
Sets the offset and size (>=4) of the local memory buffer to store a
single SysEx message in, and returns the next available memory index
(i.e. index+size).
* midiq_alloc(size)
* midiq_alloc_sysex(size)
* midiq_free()
Example: midi.midiq_alloc(1024);
Allocates/deallocates a block of local memory to store the queue in,
and returns its index.
Note: Requires Tale/malloc.jsfx-inc.
Queue Functions
* midiq_collect()
* midiq_collect(ch, mask)
Context: @block
Example: midi.midiq_collect();
Receives any MIDI messages, optionally filtering by channel (0..15,
<0=any) and status byte (1=Note Off, 2=Note On, 4=Poly Aftertouch,
8=Control Change, 16=Program Change, 32=Channel Atertouch, 64=Pitch
Bend).
* midiq_remove()
Context: @sample
Example: is_avail = midi.midiq_remove();
Checks if a MIDI message is available, removes it from the queue, and
stores it in the msg1, msg2, and msg3 instance variables.
Miscellaneous Functions
* midiq_add(ofs, msg1, msg2, msg3)
Example: midi.midiq_add(0, 0x90, 60, 127);
Adds a MIDI message to the queue.
* midiq_rewind()
Context: @block
Example: midi.midiq_rewind();
Rewinds (clears) the queue, so new MIDI messages can be added.
Note: This function is automatically called by midiq_collect().
* _midiq_rewind()
Context: @block
Example: midi._midiq_rewind();
Rewinds the queue, preserving any left-over messages.
* _midiq_collect()
* _midiq_collect(ch, mask)
Context: @block
Example: midi._midiq_collect();
Same as midiq_collect(), but without automatically rewinding the queue
first.
Instance Variables
* ofs
Example: offset_in_seconds = midi.ofs / srate;
The offset within the current sample block, in samples.
* msg1
* msg2
* msg3
Example: ch = midi.msg1 & 0x0F;
The MIDI message that was last removed from the queue.
* pend
* idx
Example: ofs_in_spls = midi.pend ? midi.ofs - midi.idx + 1;
A flag indicating that a MIDI message is pending (i.e. idx<=ofs), and
the sample index [0..samplesblock-1] of the next midiq_recv() call.
* buf
* size
Example: next_avail_index = midi.buf + midi.size * 3;
The local memory index and maximum size (in MIDI messages) of the
queue, or the index and maximum size (in bytes) of a single SysEx
message.
* head
* tail
Example: num_msgs = (midi.tail - midi.head) / 3;
Example: num_avail = midi.size - (midi.tail - midi.buf) / 3;
Pointers to the head and tail of the queue.
*/
@init
function midiq_init(index, size)
instance(buf)
(
(buf = index) + (this.size = size) * 3;
);
function midiq_init(index)
instance(buf, size)
(
size = 0;
buf = index;
);
function midiq_init_sysex(index, size)
instance(buf)
(
(buf = index) + (this.size = max(size, 4));
);
function midiq_rewind()
instance(buf, head, tail, ofs)
(
head = tail = buf;
ofs = 0;
);
function _midiq_rewind()
instance(buf, head, tail, ofs)
local(ptr)
(
head < tail ? (
ptr = head;
loop(tail - head,
ptr[] -= ofs;
ptr += 3;
);
) : (
head = tail = buf;
);
ofs = 0;
);
function midiq_add(ofs, msg1, msg23)
instance(buf, size, tail)
(
!size || tail < buf + size * 3 ? (
tail[0] = ofs;
tail[1] = msg1;
tail[2] = msg23;
tail += 3;
);
);
function midiq_add(ofs, msg1, msg2, msg3)
(
this.midiq_add(ofs, msg1, msg2 | (msg3 << 8));
);
function _midiq_collect(ch, mask)
local(ofs, msg1, msg23)
(
while(
midirecv(ofs, msg1, msg23) ? (
msg1 >= 0x80 && !(msg23 & 0x8080) && (ch < 0 || msg1 & 0x0F == ch) && (mask & (1 << ((msg1 >> 4) - 8))) ? this.midiq_add(ofs, msg1, msg23);
midisend(ofs, msg1, msg23);
);
);
);
function _midiq_collect()
local(ofs, msg1, msg23)
(
while(
midirecv(ofs, msg1, msg23) ? (
msg1 >= 0x80 && !(msg23 & 0x8080) ? this.midiq_add(ofs, msg1, msg23);
midisend(ofs, msg1, msg23);
);
);
);
function midiq_collect(ch, mask)
(
this.midiq_rewind();
this._midiq_collect(ch, mask);
);
function midiq_collect()
(
this.midiq_rewind();
this._midiq_collect();
);
function midiq_remove()
instance(head, tail, ofs, msg1, msg2, msg3, msg23)
(
head < tail && head[] <= ofs ? (
msg1 = head[1];
msg23 = head[2];
msg2 = msg23 & 0x7F;
msg3 = msg23 >> 8;
head += 3;
) : (
ofs += 1;
0;
);
);
// Simple interface
function midiq_recv()
// global(samplesblock)
instance(pend, idx, ofs, msg1, msg2, msg3)
(
pend <= 0 && midirecv(ofs, msg1, msg2) ? (
midisend(ofs, msg1, msg2);
msg3 = msg2 >> 8;
msg2 &= 0xFF;
pend = msg1 >= 0x80 && (msg2|msg3) < 0x80;
);
pend > 0 && idx >= ofs ? (
pend = 0;
1;
) : (idx += 1) >= samplesblock ? (
pend > 0 ? ofs -= samplesblock;
idx = 0;
);
);
function midiq_recv_sysex()
// global(samplesblock)
instance(pend, idx, ofs, buf, size, msg1, msg2, msg3)
local(len)
(
pend <= 0 && (len = midirecv_buf(ofs, buf, size)) > 0 ? (
midisend_buf(ofs, buf, len);
msg1 = buf[0];
msg2 = buf[1];
msg3 = buf[2];
(len == 3 && msg1 >= 0x80 && (msg2|msg3) < 0x80) ||
(len >= 4 && msg1 == 0xF0 && buf[len - 1] == 0xF7) ? pend = len;
);
pend > 0 && idx >= ofs ? (
len = pend;
pend = 0;
len;
) : (idx += 1) >= samplesblock ? (
pend > 0 ? ofs -= samplesblock;
idx = 0;
);
);