Mercurial > hg > beaglert
changeset 554:3b0c860ef209 prerelease
Merge
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 24 Jun 2016 17:14:24 +0100 |
parents | e545ce04cf2c (current diff) f8bb6186498d (diff) |
children | 5ef33a8c9702 |
files | |
diffstat | 4 files changed, 1086 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/08-PureData/customRender/_main.pd Fri Jun 24 17:14:24 2016 +0100 @@ -0,0 +1,109 @@ +#N canvas 959 597 692 1046 10; +#X obj 45 615 dac~ 1 2; +#X obj 45 574 *~ 0.1; +#X obj 45 553 osc~ 440; +#X text 464 21 ............................; +#X text 464 31 . ____ ._____ _ .......___ .; +#X text 464 41 .| __ )| ____| |....../ _ |.; +#X text 464 61 .| |_) | |___| |___ / ___ |.; +#X text 464 71 .|____/|_____|_____/_/..._|.io; +#X text 464 81 ............................; +#X text 464 51 .| __ || __|.| |...../ _| |.; +#X text 35 32 Using custom render.cpp files; +#X text 35 42 =============================; +#X text 35 58 It is possible to modify the default libpd and heavy +; +#X text 35 72 wrapper templates in order to combine c++ code with; +#X text 35 86 your puredata patches.; +#X text 35 100 In this example you should hear a tremolo effect being +; +#X text 35 114 applied to the output. This is done by taking the output +; +#X text 35 128 buffer returned from libpd or heavy and applying further +; +#X text 35 142 processing before writing the buffer to the Bela context. +; +#X text 35 156 It is also possible to define more input or output channels +; +#X text 35 170 within the render.cpp file in order to pass auxiliary +; +#X text 35 198 code.; +#X text 35 184 signals (e.g. envelopes) across the pd patch and the +c++; +#X obj 46 716 adc~ 3; +#X obj 46 738 snapshot~; +#X obj 97 694 loadbang; +#X obj 97 716 metro 5; +#X obj 46 804 * 20; +#X obj 46 826 + 0.5; +#X text 94 826 map to exponential range 0.5Hz<->20Hz; +#X obj 46 848 s tremoloRate; +#X text 154 848 <<< this receiver doesn't exist in the patch but is +parsed; +#X text 36 520 Simple 440Hz sine wave; +#X text 36 530 ----------------------; +#X text 36 660 Sending messages to modified render.cpp; +#X text 36 670 ---------------------------------------; +#X text 35 211 In this example we are sending float values to a receiver +; +#X text 35 225 named 'tremoloRate' which is parsed by the modified +; +#X text 35 239 render.cpp file and used to control the rate of the +tremolo; +#X text 35 253 effect applied to the output.; +#X obj 46 760 t f f; +#X obj 46 782 *; +#X text 35 267 See the render.cpp file in the project folder for the +libpd; +#X text 35 282 implementation. The heavy implementation can be found +inside; +#X text 35 295 the enclosed /heavy folder. (This is where custom render.cpp +; +#X text 35 309 files for heavy need to be placed when compiling with +heavy); +#X text 179 860 by a hook function in the modified render.cpp file. +; +#X text 200 897 Bela_floatHook(const char *source \, float value); +#X text 180 962 For heavy this is:; +#X text 179 884 For libpd this function is:; +#X text 200 973 sendHook( double timestamp \, const char *receiverName +; +#X text 260 986 \, const HvMessage *const m \, void *userData); +#X text 180 911 Note that in libpd the receiver name needs to be registered +; +#X text 180 925 using the libpd_bind(const char *sym) function (see +the; +#X text 180 939 contents of setup()); +#X text 180 1002 The receiver name does not need to be registered anywhere. +; +#X text 35 324 Search for 'MODIFICATION' (no quotation marks) inside +either; +#X text 35 338 of the render files to inspect all the modifications +that; +#X text 36 352 were made for this example.; +#X text 36 386 Running the patch with the modified render.cpp file +; +#X text 36 396 --------------------------------------------------- +; +#X text 36 409 If using libpd \, you can simply run this patch from +the; +#X text 36 423 Bela IDE. The system will automatically detect the; +#X text 36 437 modified file use it instead of the template.; +#X text 36 457 Similarly \, the build script for the Heavy compiler +; +#X text 36 471 will detect the presence of a render.cpp file inside +; +#X text 36 485 the enclosed /heavy/ folder and bypass the default wrapper. +; +#X connect 1 0 0 1; +#X connect 1 0 0 0; +#X connect 2 0 1 0; +#X connect 23 0 24 0; +#X connect 24 0 40 0; +#X connect 25 0 26 0; +#X connect 26 0 24 0; +#X connect 27 0 28 0; +#X connect 28 0 30 0; +#X connect 40 0 41 0; +#X connect 40 1 41 1; +#X connect 41 0 27 0;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/08-PureData/customRender/heavy/render.cpp Fri Jun 24 17:14:24 2016 +0100 @@ -0,0 +1,491 @@ +/* + * render.cpp + * + * Template render.cpp file for on-board heavy compiling + * + * N.B. this is currently *not* compatible with foleyDesigner source files! + * + * Created on: November 5, 2015 + * + * Christian Heinrichs + * + */ + +#include <Bela.h> +#include <Midi.h> +#include <Scope.h> +#include <cmath> +#include <Heavy_bela.h> +#include <string.h> +#include <stdlib.h> +#include <string.h> +#include <DigitalChannelManager.h> + +/* + * MODIFICATION + * ------------ + * Global variables for tremolo effect applied to libpd output + */ + +float gTremoloRate = 4.0; +float gPhase; + +/*********/ + +/* + * HEAVY CONTEXT & BUFFERS + */ + +Hv_bela *gHeavyContext; +float *gHvInputBuffers = NULL, *gHvOutputBuffers = NULL; +unsigned int gHvInputChannels = 0, gHvOutputChannels = 0; + +float gInverseSampleRate; + +/* + * HEAVY FUNCTIONS + */ + +// TODO: rename this +#define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs) + +void printHook(double timestampSecs, const char *printLabel, const char *msgString, void *userData) { + rt_printf("Message from Heavy patch: [@ %.3f] %s: %s\n", timestampSecs, printLabel, msgString); +} + + +// digitals +static DigitalChannelManager dcm; + +void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){ + hv_sendFloatToReceiver(gHeavyContext, hv_stringToHash((char*)receiverName), (float)state); +// rt_printf("%s: %d\n", (char*)receiverName, state); +} + +// TODO: turn them into hv hashes and adjust sendDigitalMessage accordingly +char hvDigitalInHashes[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 void sendHook( + double timestamp, // in milliseconds + const char *receiverName, + const HvMessage *const m, + void *userData) { + + /* + * MODIFICATION + * ------------ + * Parse float sent to receiver 'tremoloRate' and assign it to a global variable + */ + + if(strncmp(receiverName, "tremoloRate", 11) == 0){ + float value = hv_msg_getFloat(m, 0); // see the Heavy C API documentation: https://enzienaudio.com/docs/index.html#8.c + gTremoloRate = value; + } + + /*********/ + + // Bela digital + + // Bela digital run-time messages + + // TODO: this first block is almost an exact copy of libpd's code, should we add this to the class? + // 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(receiverName, "bela_digitalOut", prefixLength)==0){ + if(receiverName[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2) + if(receiverName[prefixLength + 1] != 0){ + // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi + int receiver = ((receiverName[prefixLength] - 48) * 10); + receiver += (receiverName[prefixLength+1] - 48); + unsigned int channel = receiver - LIBPD_DIGITAL_OFFSET; // go back to the actual Bela digital channel number + bool value = hv_msg_getFloat(m, 0); + if(channel < 16){ //16 is the hardcoded value for the number of digital channels + dcm.setValue(channel, value); + } + } + } + } + + // Bela digital initialization messages + if(strcmp(receiverName, "bela_setDigital") == 0){ + // Third argument (optional) can be ~ or sig for signal-rate, message-rate otherwise. + // [in 14 ~( + // | + // [s bela_setDigital] + // 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; + int numArgs = hv_msg_getNumElements(m); + if(numArgs < 2 || numArgs > 3 || !hv_msg_isSymbol(m, 0) || !hv_msg_isFloat(m, 1)) + return; + if(numArgs == 3 && !hv_msg_isSymbol(m,2)) + return; + char * symbol = hv_msg_getSymbol(m, 0); + + 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; + } + int channel = hv_msg_getFloat(m, 1) - LIBPD_DIGITAL_OFFSET; + if(disable == true){ + dcm.unmanage(channel); + return; + } + if(numArgs >= 3){ + char* s = hv_msg_getSymbol(m, 2); + if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){ + isMessageRate = false; + } + } + dcm.manage(channel, direction, isMessageRate); + } +} + + +/* + * SETUP, RENDER LOOP & CLEANUP + */ + +// leaving this here, trying to come up with a coherent interface with libpd. +// commenting them out so the compiler does not warn +// 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs +//static const unsigned int gChannelsInUse = 30; +//static unsigned int gAnalogChannelsInUse = 8; // hard-coded for the moment, TODO: get it at run-time from hv_context +//static const unsigned int gFirstAudioChannel = 0; +//static const unsigned int gFirstAnalogChannel = 2; +static const unsigned int gFirstDigitalChannel = 10; +static const unsigned int gFirstScopeChannel = 26; +static unsigned int gDigitalSigInChannelsInUse; +static unsigned int gDigitalSigOutChannelsInUse; + +// Bela Midi +Midi midi; +unsigned int hvMidiHashes[7]; +// Bela Scope +Scope scope; +unsigned int gScopeChannelsInUse; +float* gScopeOut; + + +bool setup(BelaContext *context, void *userData) { + if(context->audioInChannels != context->audioOutChannels || + context->analogInChannels != context->analogOutChannels){ + // It should actually work, but let's test it before releasing it! + printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n"); + return false; + } + + /* + * MODIFICATION + * ------------ + * Initialise variables for tremolo effect + */ + + gPhase = 0.0; + + /*********/ + + /* HEAVY */ + hvMidiHashes[kmmNoteOn] = hv_stringToHash("__hv_notein"); +// hvMidiHashes[kmmNoteOff] = hv_stringToHash("noteoff"); // this is handled differently, see the render function + hvMidiHashes[kmmControlChange] = hv_stringToHash("__hv_ctlin"); + // Note that the ones below are not defined by Heavy, but they are here for (wishing) forward-compatibility + // You need to receive from the corresponding symbol in Pd and unpack the message, e.g.: + //[r __hv_pgmin] + //| + //[unpack f f] + //| | + //| [print pgmin_channel] + //[print pgmin_number] + hvMidiHashes[kmmProgramChange] = hv_stringToHash("__hv_pgmin"); + hvMidiHashes[kmmPolyphonicKeyPressure] = hv_stringToHash("__hv_polytouchin"); + hvMidiHashes[kmmChannelPressure] = hv_stringToHash("__hv_touchin"); + hvMidiHashes[kmmPitchBend] = hv_stringToHash("__hv_bendin"); + + gHeavyContext = hv_bela_new(context->audioSampleRate); + + gHvInputChannels = hv_getNumInputChannels(gHeavyContext); + gHvOutputChannels = hv_getNumOutputChannels(gHeavyContext); + + gScopeChannelsInUse = gHvOutputChannels > gFirstScopeChannel ? + gHvOutputChannels - gFirstScopeChannel : 0; + gDigitalSigInChannelsInUse = gHvInputChannels > gFirstDigitalChannel ? + gHvInputChannels - gFirstDigitalChannel : 0; + gDigitalSigOutChannelsInUse = gHvOutputChannels > gFirstDigitalChannel ? + gHvOutputChannels - gFirstDigitalChannel - gScopeChannelsInUse: 0; + + printf("Starting Heavy context with %d input channels and %d output channels\n", + gHvInputChannels, gHvOutputChannels); + printf("Channels in use:\n"); + printf("Digital in : %u, Digital out: %u\n", gDigitalSigInChannelsInUse, gDigitalSigOutChannelsInUse); + printf("Scope out: %u\n", gScopeChannelsInUse); + + if(gHvInputChannels != 0) { + gHvInputBuffers = (float *)calloc(gHvInputChannels * context->audioFrames,sizeof(float)); + } + if(gHvOutputChannels != 0) { + gHvOutputBuffers = (float *)calloc(gHvOutputChannels * context->audioFrames,sizeof(float)); + } + + gInverseSampleRate = 1.0 / context->audioSampleRate; + + // Set heavy print hook + hv_setPrintHook(gHeavyContext, printHook); + // Set heavy send hook + hv_setSendHook(gHeavyContext, sendHook); + + // TODO: change these hardcoded port values and actually change them in the Midi class + midi.readFrom(0); + midi.writeTo(0); + midi.enableParser(true); + + if(gScopeChannelsInUse > 0){ + // block below copy/pasted from libpd, except + scope.setup(gScopeChannelsInUse, context->audioSampleRate); + gScopeOut = new float[gScopeChannelsInUse]; + } + // Bela digital + dcm.setCallback(sendDigitalMessage); + if(context->digitalChannels > 0){ + for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){ + dcm.setCallbackArgument(ch, hvDigitalInHashes[ch]); + } + } + // unlike libpd, no need here to bind the bela_digitalOut.. receivers + + return true; +} + + +void render(BelaContext *context, void *userData) +{ + { + int num; + while((num = midi.getParser()->numAvailableMessages()) > 0){ + static MidiChannelMessage message; + message = midi.getParser()->getNextChannelMessage(); + switch(message.getType()){ + case kmmNoteOn: { + //message.prettyPrint(); + int noteNumber = message.getDataByte(0); + int velocity = message.getDataByte(1); + int channel = message.getChannel(); + // rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff", + (float)noteNumber, (float)velocity, (float)channel+1); + 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(); + // note we are sending the below to hvHashes[kmmNoteOn] !! + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff", + (float)noteNumber, (float)0, (float)channel+1); + break; + } + case kmmControlChange: { + int channel = message.getChannel(); + int controller = message.getDataByte(0); + int value = message.getDataByte(1); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmControlChange], 0, "fff", + (float)value, (float)controller, (float)channel+1); + break; + } + case kmmProgramChange: { + int channel = message.getChannel(); + int program = message.getDataByte(0); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmProgramChange], 0, "ff", + (float)program, (float)channel+1); + break; + } + case kmmPolyphonicKeyPressure: { + //TODO: untested, I do not have anything with polyTouch... who does, anyhow? + int channel = message.getChannel(); + int pitch = message.getDataByte(0); + int value = message.getDataByte(1); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPolyphonicKeyPressure], 0, "fff", + (float)channel+1, (float)pitch, (float)value); + break; + } + case kmmChannelPressure: + { + int channel = message.getChannel(); + int value = message.getDataByte(0); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmChannelPressure], 0, "ff", + (float)value, (float)channel+1); + break; + } + case kmmPitchBend: + { + int channel = message.getChannel(); + int value = ((message.getDataByte(1) << 7) | message.getDataByte(0)); + hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPitchBend], 0, "ff", + (float)value, (float)channel+1); + break; + } + case kmmNone: + case kmmAny: + break; + } + } + } + + // De-interleave the data + if(gHvInputBuffers != NULL) { + for(unsigned int n = 0; n < context->audioFrames; n++) { + for(unsigned int ch = 0; ch < gHvInputChannels; ch++) { + if(ch >= context->audioInChannels+context->analogInChannels) { + // THESE ARE PARAMETER INPUT 'CHANNELS' USED FOR ROUTING + // 'sensor' outputs from routing channels of dac~ are passed through here + break; + } else { + // If more than 2 ADC inputs are used in the pd patch, route the analog inputs + // i.e. ADC3->analogIn0 etc. (first two are always audio inputs) + if(ch >= context->audioInChannels) { + int m = n/2; + float mIn = context->analogIn[m*context->analogInChannels + (ch-context->audioInChannels)]; + gHvInputBuffers[ch * context->audioFrames + n] = mIn; + } else { + gHvInputBuffers[ch * context->audioFrames + n] = context->audioIn[n * context->audioInChannels + ch]; + } + } + } + } + } + + // Bela digital in + // note: in multiple places below we assume that the number of digital frames is same as number of audio + // Bela digital in at message-rate + dcm.processInput(context->digital, context->digitalFrames); + + // Bela digital in at signal-rate + if(gDigitalSigInChannelsInUse > 0) + { + unsigned int j, k; + float *p0, *p1; + const unsigned int gLibpdBlockSize = context->audioFrames; + const unsigned int audioFrameBase = 0; + float* gInBuf = gHvInputBuffers; + // block below copy/pasted from libpd, except + // 16 has been replaced with gDigitalSigInChannelsInUse + for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { + unsigned int digitalFrame = audioFrameBase + j; + for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel; + k < gDigitalSigInChannelsInUse; ++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); + } + } + } + } + + + // replacement for bang~ object + //hv_vscheduleMessageForReceiver(gHeavyContext, "bela_bang", 0.0f, "b"); + + hv_bela_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames); + + // Bela digital out + // Bela digital out at signal-rate + if(gDigitalSigOutChannelsInUse > 0) + { + unsigned int j, k; + float *p0, *p1; + const unsigned int gLibpdBlockSize = context->audioFrames; + const unsigned int audioFrameBase = 0; + float* gOutBuf = gHvOutputBuffers; + // block below copy/pasted from libpd, except + // context->digitalChannels has been replaced with gDigitalSigOutChannelsInUse + for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) { + unsigned int digitalFrame = (audioFrameBase + j); + for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel; + k < gDigitalSigOutChannelsInUse; 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); + } + } + } + } + // Bela digital out at message-rate + dcm.processOutput(context->digital, context->digitalFrames); + + // Bela scope + if(gScopeChannelsInUse > 0) + { + unsigned int j, k; + float *p0, *p1; + const unsigned int gLibpdBlockSize = context->audioFrames; + float* gOutBuf = gHvOutputBuffers; + + // block below copy/pasted from libpd + 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); + } + } + + // Interleave the output data + if(gHvOutputBuffers != NULL) { + for(unsigned int n = 0; n < context->audioFrames; n++) { + + /* + * 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(unsigned int ch = 0; ch < gHvOutputChannels; ch++) { + if(ch <= context->audioOutChannels+context->analogOutChannels) { + if(ch >= context->audioOutChannels) { + int m = n/2; + context->analogOut[m * context->analogFrames + (ch-context->audioOutChannels)] = constrain(gHvOutputBuffers[ch*context->audioFrames + n],0.0,1.0); + } else { + context->audioOut[n * context->audioOutChannels + ch] = gHvOutputBuffers[ch * context->audioFrames + n] * lfo; // MODIFICATION (* lfo) + } + } + } + } + } + +} + + +void cleanup(BelaContext *context, void *userData) +{ + + hv_bela_free(gHeavyContext); + if(gHvInputBuffers != NULL) + free(gHvInputBuffers); + if(gHvOutputBuffers != NULL) + free(gHvOutputBuffers); + delete[] gScopeOut; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/08-PureData/customRender/render.cpp Fri Jun 24 17:14:24 2016 +0100 @@ -0,0 +1,484 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include <Bela.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> + +/* + * MODIFICATION + * ------------ + * Global variables for tremolo effect applied to libpd output + */ + +float gTremoloRate = 4.0; +float gPhase; +float gInverseSampleRate; + +/*********/ + +// if you are 100% sure of what value was used to compile libpd/puredata, then +// you could #define gBufLength instead of getting it at runtime. It has proved to give some 0.3% +// performance boost when it is 8 (thanks to vectorize optimizations I guess). +int gBufLength; + +float* gInBuf; +float* gOutBuf; + +void pdnoteon(int ch, int pitch, int vel) { + printf("noteon: %d %d %d\n", ch, pitch, vel); +} + +void Bela_printHook(const char *recv){ + rt_printf("%s", recv); +} +#define PARSE_MIDI +static Midi midi; +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; + +bool setup(BelaContext *context, void *userData) +{ + + /* + * 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.readFrom(0); + midi.writeTo(0); +#ifdef PARSE_MIDI + midi.enableParser(true); +#else + midi.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 + libpd_set_printhook(Bela_printHook); + libpd_set_floathook(Bela_floatHook); + libpd_set_messagehook(Bela_messageHook); + libpd_set_noteonhook(pdnoteon); + //TODO: add hooks for other midi events and generate MIDI output appropriately + libpd_init(); + //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( + void* patch = libpd_openfile(file, folder); + if(patch == NULL){ + printf("Error: file %s/%s is corrupted.\n", folder, file); + return false; + } + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. + +void render(BelaContext *context, void *userData) +{ + int num; + // the safest thread-safe option to handle MIDI input is to process the MIDI buffer + // from the audio thread. +#ifdef PARSE_MIDI + while((num = midi.getParser()->numAvailableMessages()) > 0){ + static MidiChannelMessage message; + message = midi.getParser()->getNextChannelMessage(); + //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, 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, noteNumber, 0); + break; + } + case kmmControlChange: + { + int channel = message.getChannel(); + int controller = message.getDataByte(0); + int value = message.getDataByte(1); + libpd_controlchange(channel, controller, value); + break; + } + case kmmProgramChange: + { + int channel = message.getChannel(); + int program = message.getDataByte(0); + libpd_programchange(channel, program); + break; + } + case kmmPolyphonicKeyPressure: + { + int channel = message.getChannel(); + int pitch = message.getDataByte(0); + int value = message.getDataByte(1); + libpd_polyaftertouch(channel, pitch, value); + break; + } + case kmmChannelPressure: + { + int channel = message.getChannel(); + int value = message.getDataByte(0); + libpd_aftertouch(channel, value); + break; + } + case kmmPitchBend: + { + int channel = message.getChannel(); + int value = ((message.getDataByte(1) << 7)| message.getDataByte(0)) - 8192; + libpd_pitchbend(channel, value); + break; + } + case kmmNone: + case kmmAny: + break; + } + } +#else + int input; + while((input = midi.getInput()) >= 0){ + libpd_midibyte(0, 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); + } + } + } + } +} + +// cleanup() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in setup(). + +void cleanup(BelaContext *context, void *userData) +{ + delete [] gScopeOut; +}
--- a/examples/08-PureData/digital/_main.pd Fri Jun 24 17:13:45 2016 +0100 +++ b/examples/08-PureData/digital/_main.pd Fri Jun 24 17:14:24 2016 +0100 @@ -1,4 +1,4 @@ -#N canvas 1059 41 685 1592 10; +#N canvas 1005 79 685 1592 10; #X obj 60 800 r bela_digitalIn11; #X obj 272 800 r bela_digitalIn12; #X obj 60 897 adc~ 13 14; @@ -79,7 +79,7 @@ #X text 44 30 ===========; #X text 43 62 Bela's digital inputs and outputs can be addressed from ; -#X text 43 88 rate or as signals. Input/output modes and rate need +#X text 44 88 rate or as signals. Input/output modes and rate need ; #X text 43 101 to be initialised first.; #X text 43 75 pd. They can either be received as messages at message