-
Notifications
You must be signed in to change notification settings - Fork 1
/
JackMIDIUGens.cpp
397 lines (318 loc) · 12.5 KB
/
JackMIDIUGens.cpp
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
/*
JackMIDIUGens Jack MIDI for SuperCollider
Copyright (c) 2017 Carlo Capocasa. All rights reserved.
https://capocasa.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SC_PlugIn.h"
#include <iostream>
#include <jack/jack.h>
#include <jack/midiport.h>
// MIDI event types
#define EVENT_NOTEOFF 8
#define EVENT_NOTEON 9
#define EVENT_POLYTOUCH 10
#define EVENT_CONTROLLER 11
#define EVENT_TOUCH 13
#define EVENT_PITCHBEND 14
// Integers to represent configurable controllers
// Arbitrary, must be the same as in the sclang class file
#define CONTROLLER_PITCHBEND 1014
#define CONTROLLER_TOUCH 1013
static InterfaceTable *ft;
struct JackMIDIIn: public Unit
{
void* jack_midi_port_in_buffer;
jack_nframes_t jack_frame_time;
jack_nframes_t event_index;
jack_nframes_t event_count;
jack_nframes_t offset;
uint32 configured_controller_count;
uint32 configured_controllers[256];
uint32 output_buffer[256];
uint32 polyphony;
bool polytouch;
uint32 configured_channel_count;
uint32 configured_channels[16];
uint32 configured_note_count;
uint32 configured_notes[128];
};
static void JackMIDIIn_next(JackMIDIIn *unit, int inNumSamples);
static void JackMIDIIn_Ctor(JackMIDIIn* unit);
jack_client_t* jack_client = NULL;
jack_port_t* jack_midi_port_in = NULL;
jack_nframes_t jack_nframes = 0;
int jack_buffer_size(jack_nframes_t nframes, void *arg) {
jack_nframes = nframes;
}
void jack_init() {
if ((jack_client = jack_client_open("JackMIDIUGens", JackNullOption, NULL)) == 0)
{
//std::cout << "JackMIDIIn: cannot connect to jack server" << std::endl;
return;
}
jack_midi_port_in = jack_port_register (jack_client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
//jack_set_process_callback (client, process, unit);
if (jack_activate(jack_client) != 0)
{
//std::cout<< "JackMIDIIn: cannot activate jack client" << std::endl;
return;
}
jack_nframes = jack_get_buffer_size(jack_client);
jack_set_buffer_size_callback(jack_client, jack_buffer_size, 0);
}
PluginLoad(JackMIDIIn)
{
ft = inTable;
DefineSimpleUnit(JackMIDIIn);
jack_init();
}
void JackMIDIIn_Ctor(JackMIDIIn* unit)
{
unit->jack_frame_time = 0;
unit->polyphony = IN0(0);
uint32 configured_channel_count = IN0(1);
unit->configured_channel_count = configured_channel_count;
uint32 configured_controller_count = IN0(2);
unit->configured_controller_count = configured_controller_count;
unit->polytouch = IN0(3);
uint32 configured_note_count = IN0(4);
unit->configured_note_count = configured_note_count;
for (uint32 i = 0; i < 256; i++) {
unit->output_buffer[i] = 0;
}
uint32 offset_in = 5;
for (uint32 i = 0; i < configured_channel_count; i++) {
unit->configured_channels[i] = IN0(offset_in++);
//std::cout << unit->configured_channels[i] << " ";
}
//std::cout << "\n";
for (uint32 i = 0; i < configured_controller_count; i++) {
unit->configured_controllers[i] = IN0(offset_in++);
//std::cout << unit->configured_controllers[i] << " ";
}
//std::cout << "\n";
for (uint32 i = 0; i < configured_note_count; i++) {
unit->configured_notes[i] = IN0(offset_in++);
//std::cout << unit->configured_notes[i] << " ";
}
//std::cout << "\n";
SETCALC(JackMIDIIn_next);
}
inline bool is_configured_channel(uint32* configured_channels, uint32 configured_channel_count, uint32 event_channel) {
if (configured_channel_count == 0) {
return true;
}
for (int i = 0; i < configured_channel_count; i++) {
if (configured_channels[i] == event_channel) {
return true;
}
}
return false;
}
inline bool is_configured_note(uint32* configured_notes, uint32 configured_note_count, uint32 event_note) {
if (configured_note_count == 0) {
return true;
}
for (int i = 0; i < configured_note_count; i++) {
if (configured_notes[i] == event_note) {
return true;
}
}
return false;
}
void JackMIDIIn_next(JackMIDIIn *unit, int inNumSamples)
{
int numOutputs = unit->mNumOutputs;
//std::cout << "next" << std::endl;
jack_nframes_t jack_frame_time = jack_last_frame_time(jack_client);
void* jack_midi_port_in_buffer;
jack_nframes_t offset;
jack_nframes_t event_index;
jack_nframes_t event_count;
if (unit->jack_frame_time != jack_frame_time) {
jack_midi_port_in_buffer = jack_port_get_buffer(jack_midi_port_in, jack_nframes);
//std::cout << "numOutputs " << numOutputs << std::endl;
//std::cout << "new frame time " << jack_frame_time << std::endl;
jack_midi_event_t event;
event_index = 0;
event_count = jack_midi_get_event_count(jack_midi_port_in_buffer);
offset = 0;
} else {
//std::cout << "WARNING recycling" << std::endl;
jack_midi_port_in_buffer = unit->jack_midi_port_in_buffer;
event_index = unit->event_index;
event_count = unit->event_count;
offset = unit->offset;
}
//std::cout << "cycle" << std::endl;
jack_midi_event_t event;
jack_nframes_t time;
jack_nframes_t last_time = 0;
uint32* output_buffer = unit->output_buffer;
uint32 polyphony = unit->polyphony;
uint32 configured_controller_count = unit->configured_controller_count;
uint32* configured_controllers = unit->configured_controllers;
uint32 polytouch = unit->polytouch;
uint32 note_channel_count = 2 + polytouch;
uint32 notes_channel_count = note_channel_count * polyphony;
uint32* output_buffer_channel_controllers = output_buffer + notes_channel_count;
uint32* configured_channels = unit->configured_channels;
uint32 configured_channel_count = unit->configured_channel_count;
uint32* configured_notes = unit->configured_notes;
uint32 configured_note_count = unit->configured_note_count;
// I think James McCartney's spirit will haunt me for this one,
// but I just can't get myself to use nasty macros to avoid a
// single extra comparison per control period
bool audiorate = inNumSamples > 1;
for ( ; event_index < event_count; event_index++) {
jack_midi_event_get(&event, jack_midi_port_in_buffer, event_index);
time = event.time - offset;
if (time >= FULLBUFLENGTH) {
std::cout << "JackMIDIUGens: Warning, time too early, dropped note" << std::endl;
break;
}
//std::cout << "event event_index " << event_index << " event_count " << event_count << " time " << event.time << " offset " << offset << " buffer " << (int)event.buffer[0] << " " << (int)event.buffer[1] << " " << (int)event.buffer[2] << " jack_frame_time " << jack_frame_time << " FULLBUFLENGTH " << FULLBUFLENGTH << std::endl;
uint32 event_status = event.buffer[0];
uint32 event_num = event.buffer[1];
uint32 event_value = event.buffer[2];
uint32 event_type = (int) event_status / 16;
uint32 event_channel = event_status % 16;
//std::cout << "EVENT: event_type " << event_type << " event_channel " << event_channel << " event_num " << event_num << std::endl;
if ( ! is_configured_channel(configured_channels, configured_channel_count, event_channel)) {
//std::cout << "nochan note " << event_value << " channel " << event_channel << std::endl;
continue;
}
switch(event_type) {
case EVENT_NOTEON:
case EVENT_NOTEOFF:
if ( ! is_configured_note(configured_notes, configured_note_count, event_num)) {
// std::cout << "nonote note " << event_value << " channel " << event_channel << std::endl;
continue;
}
default:
break;
}
// Output up until just before this event
if (audiorate) {
for (jack_nframes_t i = last_time; i < time; i++) {
for (int j = 0; j < numOutputs; j++) {
OUT(j)[i] = (float)output_buffer[j];
}
}
} else {
// no intermittent output for control rate
}
uint32 output_buffer_index;
switch(event_type) {
case EVENT_NOTEON:
// find empty output
for (output_buffer_index = 0; output_buffer_index < notes_channel_count; output_buffer_index += note_channel_count) {
if (event_num == 0 || output_buffer[output_buffer_index] == 0) {
break;
}
}
if (output_buffer_index < notes_channel_count) {
output_buffer[output_buffer_index] = event_num;
output_buffer[output_buffer_index+1] = event_value;
//std::cout << "NOTE ON: note " << event_num << " on channel " << event_channel << std::endl;
} else {
// potentially warn
std::cout << "JackMIDIUGens: Warning, dropped noteOn event " << event_num << " on channel " << event_channel << std::endl;
}
break;
case EVENT_NOTEOFF:
// find playing note
for (output_buffer_index = 0; output_buffer_index < notes_channel_count; output_buffer_index += note_channel_count) {
if (output_buffer[output_buffer_index] == event_num) {
break;
}
}
if (output_buffer_index < notes_channel_count) {
output_buffer[output_buffer_index] = 0;
output_buffer[output_buffer_index+1] = 0;
if (polytouch) {
output_buffer[output_buffer_index+2] = 0;
}
//std::cout << "NOTE OFF: note " << event_num << " on channel " << event_channel << std::endl;
} else {
// potentially warn
//std::cout << "JackMIDIUGens: Warning, dropped noteOff event " << event_num << " on channel " << event_channel << std::endl;
}
break;
case EVENT_PITCHBEND:
//std::cout << "bend " << event_num << " " << event_value << std::endl;
for (int i = 0; i < configured_controller_count; i++) {
if (configured_controllers[i] == CONTROLLER_PITCHBEND) {
output_buffer_channel_controllers[i] = (float)(event_num + 128*event_value);
}
}
break;
case EVENT_CONTROLLER:
//std::cout << "controller " << event_num << " " << event_value << std::endl;
for (int i = 0; i < configured_controller_count; i++) {
if (configured_controllers[i] == event_num) {
output_buffer_channel_controllers[i] = (float)event_value;
}
}
break;
case EVENT_POLYTOUCH:
//std::cout << "polytouch " << event_num << " " << event_value << std::endl;
if (polytouch) {
// find playing note
for (output_buffer_index = 0; output_buffer_index < notes_channel_count; output_buffer_index += note_channel_count) {
if (output_buffer[output_buffer_index] == event_num) {
break;
}
}
if (output_buffer_index < notes_channel_count) {
output_buffer[output_buffer_index+2] = event_value;
} else {
// potentially warn
}
}
break;
case EVENT_TOUCH:
//std::cout << "touch " << event_num << " " << event_value << std::endl;
for (int i = 0; i < configured_controller_count; i++) {
if (configured_controllers[i] == CONTROLLER_TOUCH) {
output_buffer_channel_controllers[i] = (float)event_num;
}
}
break;
default:
// ignore other types
break;
}
last_time = time;
}
if (audiorate) {
// Output this event and to completion of cycle
for(jack_nframes_t i = last_time; i < FULLBUFLENGTH; i++) {
for (int j = 0; j < numOutputs; j++) {
OUT(j)[i] = (float)output_buffer[j];
}
}
} else {
// Output this event
for (int j = 0; j < numOutputs; j++) {
OUT0(j) = (float)output_buffer[j];
}
}
offset += FULLBUFLENGTH;
unit->jack_frame_time = jack_frame_time;
unit->offset = offset;
unit->event_index = event_index;
unit->event_count = event_count;
unit->jack_midi_port_in_buffer = jack_midi_port_in_buffer;
}