/
render.cpp
579 lines (510 loc) · 18.7 KB
/
render.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
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
#include <Bela.h>
#include <OSCServer.h>
#include <DigitalChannelManager.h>
#include <cmath>
#include <I2c_Codec.h>
#include <PRU.h>
#include <stdio.h>
#include <libpd/z_libpd.h>
#include <libpd/s_stuff.h>
#include <UdpServer.h>
#include <Midi.h>
#include <Scope.h>
OSCServer oscServer;
float gTremoloRate = 4.0;
float gPhase;
float gInverseSampleRate;
int gBufLength;
int incomingValue;
float* gInBuf;
float* gOutBuf;
#define PARSE_MIDI
static std::vector<Midi*> midi;
std::vector<const char*> gMidiPortNames;
static unsigned int getPortChannel(int* channel){
unsigned int port = 0;
while(*channel > 16){
*channel -= 16;
port += 1;
}
if(port >= midi.size()){
// if the port number exceeds the number of ports available, send out
// of the first port
rt_fprintf(stderr, "Port out of range, using port 0 instead\n");
port = 0;
}
return port;
}
int parseMessage(oscpkt::Message msg){
rt_printf("received message to: %s\n", msg.addressPattern().c_str());
int intArg;
float floatArg;
if (msg.match("/nexus-ui/").popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs()){
rt_printf("received int %i and float %f\n", intArg, floatArg);
}
return intArg;
};
void Bela_MidiOutNoteOn(int channel, int pitch, int velocity) {
int port = getPortChannel(&channel);
rt_printf("noteout _ port: %d, channel: %d, pitch: %d, velocity %d\n", port, channel, pitch, velocity);
midi[port]->writeNoteOn(channel, pitch, velocity);
}
void Bela_MidiOutControlChange(int channel, int controller, int value) {
int port = getPortChannel(&channel);
rt_printf("ctlout _ port: %d, channel: %d, controller: %d, value: %d\n", port, channel, controller, value);
midi[port]->writeControlChange(channel, controller, value);
}
void Bela_MidiOutProgramChange(int channel, int program) {
int port = getPortChannel(&channel);
rt_printf("pgmout _ port: %d, channel: %d, program: %d\n", port, channel, program);
midi[port]->writeProgramChange(channel, program);
}
void Bela_MidiOutPitchBend(int channel, int value) {
int port = getPortChannel(&channel);
rt_printf("bendout _ port: %d, channel: %d, value: %d\n", port, channel, value);
midi[port]->writePitchBend(channel, value);
}
void Bela_MidiOutAftertouch(int channel, int pressure){
int port = getPortChannel(&channel);
rt_printf("touchout _ port: %d, channel: %d, pressure: %d\n", port, channel, pressure);
midi[port]->writeChannelPressure(channel, pressure);
}
void Bela_MidiOutPolyAftertouch(int channel, int pitch, int pressure){
int port = getPortChannel(&channel);
rt_printf("polytouchout _ port: %d, channel: %d, pitch: %d, pressure: %d\n", port, channel, pitch, pressure);
midi[port]->writePolyphonicKeyPressure(channel, pitch, pressure);
}
void Bela_MidiOutByte(int port, int byte){
printf("port: %d, byte: %d\n", port, byte);
if(port > (int)midi.size()){
// if the port is out of range, redirect to the first port.
rt_fprintf(stderr, "Port out of range, using port 0 instead\n");
port = 0;
}
midi[port]->writeOutput(byte);
}
void Bela_printHook(const char *recv){
rt_printf("%s", recv);
}
static DigitalChannelManager dcm;
void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
libpd_float((char*)receiverName, (float)state);
// rt_printf("%s: %d\n", (char*)receiverName, state);
}
#define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
if(strcmp(source, "bela_setDigital") == 0){
// symbol is the direction, argv[0] is the channel, argv[1] (optional)
// is signal("sig" or "~") or message("message", default) rate
bool isMessageRate = true; // defaults to message rate
bool direction = 0; // initialize it just to avoid the compiler's warning
bool disable = false;
if(strcmp(symbol, "in") == 0){
direction = INPUT;
} else if(strcmp(symbol, "out") == 0){
direction = OUTPUT;
} else if(strcmp(symbol, "disable") == 0){
disable = true;
} else {
return;
}
if(argc == 0){
return;
} else if (libpd_is_float(&argv[0]) == false){
return;
}
int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
if(disable == true){
dcm.unmanage(channel);
return;
}
if(argc >= 2){
t_atom* a = &argv[1];
if(libpd_is_symbol(a)){
char *s = libpd_get_symbol(a);
if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
isMessageRate = false;
}
}
}
dcm.manage(channel, direction, isMessageRate);
}
}
void Bela_floatHook(const char *source, float value){
/*
* MODIFICATION
* ------------
* Parse float sent to receiver 'tremoloRate' and assign it to a global variable
* N.B. When using libpd receiver names need to be registered (see setup() function below)
*/
if(strncmp(source, "tremoloRate", 11) == 0){
gTremoloRate = value;
}
/*********/
// let's make this as optimized as possible for built-in digital Out parsing
// the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
static int prefixLength = 15; // strlen("bela_digitalOut")
if(strncmp(source, "bela_digitalOut", prefixLength)==0){
if(source[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
if(source[prefixLength + 1] != 0){
// quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
int receiver = ((source[prefixLength] - 48) * 10);
receiver += (source[prefixLength+1] - 48);
unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number
if(channel < 16){ //16 is the hardcoded value for the number of digital channels
dcm.setValue(channel, value);
}
}
}
}
}
char receiverNames[16][21]={
{"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
{"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
{"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
{"bela_digitalIn26"}
};
static unsigned int gAnalogChannelsInUse;
static unsigned int gLibpdBlockSize;
// 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
static const unsigned int gChannelsInUse = 30;
//static const unsigned int gFirstAudioChannel = 0;
static const unsigned int gFirstAnalogChannel = 2;
static const unsigned int gFirstDigitalChannel = 10;
static const unsigned int gFirstScopeChannel = 26;
Scope scope;
unsigned int gScopeChannelsInUse = 4;
float* gScopeOut;
void* gPatch;
bool setup(BelaContext *context, void *userData)
{
oscServer.setup(4368);
return true;
// add here other devices you need
gMidiPortNames.push_back("hw:1,0,0");
//gMidiPortNames.push_back("hw:0,0,0");
/*
* MODIFICATION
* ------------
* Initialise variables for tremolo effect
*/
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
/*********/
scope.setup(gScopeChannelsInUse, context->audioSampleRate);
gScopeOut = new float[gScopeChannelsInUse];
// Check first of all if file exists. Will actually open it later.
char file[] = "_main.pd";
char folder[] = "./";
unsigned int strSize = strlen(file) + strlen(folder) + 1;
char* str = (char*)malloc(sizeof(char) * strSize);
snprintf(str, strSize, "%s%s", folder, file);
if(access(str, F_OK) == -1 ) {
printf("Error file %s/%s not found. The %s file should be your main patch.\n", folder, file, file);
return false;
}
if(context->analogInChannels != context->analogOutChannels ||
context->audioInChannels != context->audioOutChannels){
printf("This project requires the number of inputs and the number of outputs to be the same\n");
return false;
}
// analog setup
gAnalogChannelsInUse = context->analogInChannels;
// digital setup
dcm.setCallback(sendDigitalMessage);
if(context->digitalChannels > 0){
for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
dcm.setCallbackArgument(ch, receiverNames[ch]);
}
}
midi.resize(gMidiPortNames.size());
for(unsigned int n = 0; n < midi.size(); ++n){
midi[n] = new Midi();
const char* name = gMidiPortNames[n];
midi[n]->readFrom(name);
midi[n]->writeTo(name);
#ifdef PARSE_MIDI
midi[n]->enableParser(true);
#else
midi[n]->enableParser(false);
#endif /* PARSE_MIDI */
}
// udpServer.bindToPort(1234);
gLibpdBlockSize = libpd_blocksize();
// check that we are not running with a blocksize smaller than gLibPdBlockSize
// We could still make it work, but the load would be executed unevenly between calls to render
if(context->audioFrames < gLibpdBlockSize){
fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
return false;
}
// set hooks before calling libpd_init
if(midi.size() > 0){
// do not register callbacks if no MIDI device is in use.
libpd_set_printhook(Bela_printHook);
libpd_set_floathook(Bela_floatHook);
libpd_set_messagehook(Bela_messageHook);
libpd_set_noteonhook(Bela_MidiOutNoteOn);
libpd_set_controlchangehook(Bela_MidiOutControlChange);
libpd_set_programchangehook(Bela_MidiOutProgramChange);
libpd_set_pitchbendhook(Bela_MidiOutPitchBend);
libpd_set_aftertouchhook(Bela_MidiOutAftertouch);
libpd_set_polyaftertouchhook(Bela_MidiOutPolyAftertouch);
libpd_set_midibytehook(Bela_MidiOutByte);
}
//initialize libpd. This clears the search path
libpd_init();
//Add the current folder to the search path for externals
libpd_add_to_search_path(".");
libpd_add_to_search_path("../pd-externals");
//TODO: ideally, we would analyse the ASCII of the patch file and find out which in/outs to use
libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
gInBuf = libpd_get_sys_soundin();
gOutBuf = libpd_get_sys_soundout();
libpd_start_message(1); // one entry in list
libpd_add_float(1.0f);
libpd_finish_message("pd", "dsp");
gBufLength = max(gLibpdBlockSize, context->audioFrames);
// bind your receivers here
libpd_bind("bela_digitalOut11");
libpd_bind("bela_digitalOut12");
libpd_bind("bela_digitalOut13");
libpd_bind("bela_digitalOut14");
libpd_bind("bela_digitalOut15");
libpd_bind("bela_digitalOut16");
libpd_bind("bela_digitalOut17");
libpd_bind("bela_digitalOut18");
libpd_bind("bela_digitalOut19");
libpd_bind("bela_digitalOut20");
libpd_bind("bela_digitalOut21");
libpd_bind("bela_digitalOut22");
libpd_bind("bela_digitalOut23");
libpd_bind("bela_digitalOut24");
libpd_bind("bela_digitalOut25");
libpd_bind("bela_digitalOut26");
libpd_bind("bela_setDigital");
/*
* MODIFICATION
* ------------
* Bind an additional receiver for the tremoloRate parameter
*/
libpd_bind("tremoloRate");
/*********/
// open patch [; pd open file folder(
gPatch = libpd_openfile(file, folder);
if(gPatch == NULL){
printf("Error: file %s/%s is corrupted.\n", folder, file);
return false;
}
return true;
}
void render(BelaContext *context, void *userData)
{
while (oscServer.messageWaiting()){
oscpkt::Message msg = oscServer.popMessage();
//rt_printf("received message to: %s %s\n", msg.addressPattern().c_str(), msg.);
//oscpkt::Message::ArgReader::
incomingValue = parseMessage(msg);
}
int num;
// the safest thread-safe option to handle MIDI input is to process the MIDI buffer
// from the audio thread.
#ifdef PARSE_MIDI
for(unsigned int port = 0; port < midi.size(); ++port){
while((num = midi[port]->getParser()->numAvailableMessages()) > 0){
static MidiChannelMessage message;
message = midi[port]->getParser()->getNextChannelMessage();
rt_printf("On port %d (%s): ", port, gMidiPortNames[port]);
message.prettyPrint(); // use this to print beautified message (channel, data bytes)
switch(message.getType()){
case kmmNoteOn:
{
int noteNumber = message.getDataByte(0);
int velocity = message.getDataByte(1);
int channel = message.getChannel();
libpd_noteon(channel + port * 16, noteNumber, velocity);
break;
}
case kmmNoteOff:
{
/* PureData does not seem to handle noteoff messages as per the MIDI specs,
* so that the noteoff velocity is ignored. Here we convert them to noteon
* with a velocity of 0.
*/
int noteNumber = message.getDataByte(0);
// int velocity = message.getDataByte(1); // would be ignored by Pd
int channel = message.getChannel();
libpd_noteon(channel + port * 16, noteNumber, 0);
break;
}
case kmmControlChange:
{
int channel = message.getChannel();
int controller = message.getDataByte(0);
int value = message.getDataByte(1);
libpd_controlchange(channel + port * 16, controller, value);
break;
}
case kmmProgramChange:
{
int channel = message.getChannel();
int program = message.getDataByte(0);
libpd_programchange(channel + port * 16, program);
break;
}
case kmmPolyphonicKeyPressure:
{
int channel = message.getChannel();
int pitch = message.getDataByte(0);
int value = message.getDataByte(1);
libpd_polyaftertouch(channel + port * 16, pitch, value);
break;
}
case kmmChannelPressure:
{
int channel = message.getChannel();
int value = message.getDataByte(0);
libpd_aftertouch(channel + port * 16, value);
break;
}
case kmmPitchBend:
{
int channel = message.getChannel();
int value = ((message.getDataByte(1) << 7)| message.getDataByte(0)) - 8192;
libpd_pitchbend(channel + port * 16, value);
break;
}
case kmmNone:
case kmmAny:
break;
}
}
}
#else
int input;
for(unsigned int port = 0; port < NUM_MIDI_PORTS; ++port){
while((input = midi[port].getInput()) >= 0){
libpd_midibyte(port, input);
}
}
#endif /* PARSE_MIDI */
static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize;
for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){
unsigned int audioFrameBase = gLibpdBlockSize * tick;
unsigned int j;
unsigned int k;
float* p0;
float* p1;
for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
for (k = 0, p1 = p0; k < context->audioInChannels; k++, p1 += gLibpdBlockSize) {
*p1 = audioRead(context, audioFrameBase + j, k);
}
}
// then analogs
// this loop resamples by ZOH, as needed, using m
if(context->analogInChannels == 8 ){ //hold the value for two frames
for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
unsigned int analogFrame = (audioFrameBase + j) / 2;
*p1 = analogRead(context, analogFrame, k);
}
}
} else if(context->analogInChannels == 4){ //write every frame
for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
unsigned int analogFrame = audioFrameBase + j;
*p1 = analogRead(context, analogFrame, k);
}
}
} else if(context->analogInChannels == 2){ //drop every other frame
for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
unsigned int analogFrame = (audioFrameBase + j) * 2;
*p1 = analogRead(context, analogFrame, k);
}
}
}
// Bela digital input
// note: in multiple places below we assume that the number of digitals is same as number of audio
// digital in at message-rate
dcm.processInput(&context->digital[audioFrameBase], gLibpdBlockSize);
// digital in at signal-rate
for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
unsigned int digitalFrame = audioFrameBase + j;
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
k < 16; ++k, p1 += gLibpdBlockSize) {
if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
*p1 = digitalRead(context, digitalFrame, k);
}
}
}
libpd_process_sys(); // process the block
//digital out
// digital out at signal-rate
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
unsigned int digitalFrame = (audioFrameBase + j);
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
k < context->digitalChannels; k++, p1 += gLibpdBlockSize) {
if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
}
}
}
// digital out at message-rate
dcm.processOutput(&context->digital[audioFrameBase], gLibpdBlockSize);
//audio
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
/*
* MODIFICATION
* ------------
* Processing for tremolo effect while writing libpd output to Bela output buffer
*/
// Generate a sinewave with frequency set by gTremoloRate
// and amplitude from -0.5 to 0.5
float lfo = sinf(gPhase) * 0.5;
// Keep track and wrap the phase of the sinewave
gPhase += 2.0 * M_PI * gTremoloRate * gInverseSampleRate;
if(gPhase > 2.0 * M_PI)
gPhase -= 2.0 * M_PI;
/*********/
for (k = 0, p1 = p0; k < context->audioOutChannels; k++, p1 += gLibpdBlockSize) {
audioWrite(context, audioFrameBase + j, k, *p1 * lfo); // MODIFICATION (* lfo)
}
}
//scope
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
gScopeOut[k] = *p1;
}
scope.log(gScopeOut[0], gScopeOut[1], gScopeOut[2], gScopeOut[3]);
}
//analog
if(context->analogOutChannels == 8){
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames
unsigned int analogFrame = (audioFrameBase + j) / 2;
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
analogWriteOnce(context, analogFrame, k, *p1);
}
}
} else if(context->analogOutChannels == 4){ //write every frame
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
unsigned int analogFrame = (audioFrameBase + j);
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
analogWriteOnce(context, analogFrame, k, *p1);
}
}
} else if(context->analogOutChannels == 2){ //write every frame twice
for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
int analogFrame = audioFrameBase * 2 + j * 2;
analogWriteOnce(context, analogFrame, k, *p1);
analogWriteOnce(context, analogFrame + 1, k, *p1);
}
}
}
}
}
void cleanup(BelaContext *context, void *userData)
{
libpd_closefile(gPatch);
delete [] gScopeOut;
}