annotate examples/08-PureData/customRender/heavy/render.cpp @ 552:f8bb6186498d prerelease

added customRender example for predate
author chnrx <chris.heinrichs@gmail.com>
date Fri, 24 Jun 2016 16:22:17 +0100
parents
children 5ef33a8c9702
rev   line source
chris@552 1 /*
chris@552 2 * render.cpp
chris@552 3 *
chris@552 4 * Template render.cpp file for on-board heavy compiling
chris@552 5 *
chris@552 6 * N.B. this is currently *not* compatible with foleyDesigner source files!
chris@552 7 *
chris@552 8 * Created on: November 5, 2015
chris@552 9 *
chris@552 10 * Christian Heinrichs
chris@552 11 *
chris@552 12 */
chris@552 13
chris@552 14 #include <Bela.h>
chris@552 15 #include <Midi.h>
chris@552 16 #include <Scope.h>
chris@552 17 #include <cmath>
chris@552 18 #include <Heavy_bela.h>
chris@552 19 #include <string.h>
chris@552 20 #include <stdlib.h>
chris@552 21 #include <string.h>
chris@552 22 #include <DigitalChannelManager.h>
chris@552 23
chris@552 24 /*
chris@552 25 * MODIFICATION
chris@552 26 * ------------
chris@552 27 * Global variables for tremolo effect applied to libpd output
chris@552 28 */
chris@552 29
chris@552 30 float gTremoloRate = 4.0;
chris@552 31 float gPhase;
chris@552 32
chris@552 33 /*********/
chris@552 34
chris@552 35 /*
chris@552 36 * HEAVY CONTEXT & BUFFERS
chris@552 37 */
chris@552 38
chris@552 39 Hv_bela *gHeavyContext;
chris@552 40 float *gHvInputBuffers = NULL, *gHvOutputBuffers = NULL;
chris@552 41 unsigned int gHvInputChannels = 0, gHvOutputChannels = 0;
chris@552 42
chris@552 43 float gInverseSampleRate;
chris@552 44
chris@552 45 /*
chris@552 46 * HEAVY FUNCTIONS
chris@552 47 */
chris@552 48
chris@552 49 // TODO: rename this
chris@552 50 #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
chris@552 51
chris@552 52 void printHook(double timestampSecs, const char *printLabel, const char *msgString, void *userData) {
chris@552 53 rt_printf("Message from Heavy patch: [@ %.3f] %s: %s\n", timestampSecs, printLabel, msgString);
chris@552 54 }
chris@552 55
chris@552 56
chris@552 57 // digitals
chris@552 58 static DigitalChannelManager dcm;
chris@552 59
chris@552 60 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
chris@552 61 hv_sendFloatToReceiver(gHeavyContext, hv_stringToHash((char*)receiverName), (float)state);
chris@552 62 // rt_printf("%s: %d\n", (char*)receiverName, state);
chris@552 63 }
chris@552 64
chris@552 65 // TODO: turn them into hv hashes and adjust sendDigitalMessage accordingly
chris@552 66 char hvDigitalInHashes[16][21]={
chris@552 67 {"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
chris@552 68 {"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
chris@552 69 {"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
chris@552 70 {"bela_digitalIn26"}
chris@552 71 };
chris@552 72
chris@552 73 static void sendHook(
chris@552 74 double timestamp, // in milliseconds
chris@552 75 const char *receiverName,
chris@552 76 const HvMessage *const m,
chris@552 77 void *userData) {
chris@552 78
chris@552 79 /*
chris@552 80 * MODIFICATION
chris@552 81 * ------------
chris@552 82 * Parse float sent to receiver 'tremoloRate' and assign it to a global variable
chris@552 83 */
chris@552 84
chris@552 85 if(strncmp(receiverName, "tremoloRate", 11) == 0){
chris@552 86 float value = hv_msg_getFloat(m, 0); // see the Heavy C API documentation: https://enzienaudio.com/docs/index.html#8.c
chris@552 87 gTremoloRate = value;
chris@552 88 }
chris@552 89
chris@552 90 /*********/
chris@552 91
chris@552 92 // Bela digital
chris@552 93
chris@552 94 // Bela digital run-time messages
chris@552 95
chris@552 96 // TODO: this first block is almost an exact copy of libpd's code, should we add this to the class?
chris@552 97 // let's make this as optimized as possible for built-in digital Out parsing
chris@552 98 // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
chris@552 99 static int prefixLength = 15; // strlen("bela_digitalOut")
chris@552 100 if(strncmp(receiverName, "bela_digitalOut", prefixLength)==0){
chris@552 101 if(receiverName[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
chris@552 102 if(receiverName[prefixLength + 1] != 0){
chris@552 103 // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
chris@552 104 int receiver = ((receiverName[prefixLength] - 48) * 10);
chris@552 105 receiver += (receiverName[prefixLength+1] - 48);
chris@552 106 unsigned int channel = receiver - LIBPD_DIGITAL_OFFSET; // go back to the actual Bela digital channel number
chris@552 107 bool value = hv_msg_getFloat(m, 0);
chris@552 108 if(channel < 16){ //16 is the hardcoded value for the number of digital channels
chris@552 109 dcm.setValue(channel, value);
chris@552 110 }
chris@552 111 }
chris@552 112 }
chris@552 113 }
chris@552 114
chris@552 115 // Bela digital initialization messages
chris@552 116 if(strcmp(receiverName, "bela_setDigital") == 0){
chris@552 117 // Third argument (optional) can be ~ or sig for signal-rate, message-rate otherwise.
chris@552 118 // [in 14 ~(
chris@552 119 // |
chris@552 120 // [s bela_setDigital]
chris@552 121 // is signal("sig" or "~") or message("message", default) rate
chris@552 122 bool isMessageRate = true; // defaults to message rate
chris@552 123 bool direction = 0; // initialize it just to avoid the compiler's warning
chris@552 124 bool disable = false;
chris@552 125 int numArgs = hv_msg_getNumElements(m);
chris@552 126 if(numArgs < 2 || numArgs > 3 || !hv_msg_isSymbol(m, 0) || !hv_msg_isFloat(m, 1))
chris@552 127 return;
chris@552 128 if(numArgs == 3 && !hv_msg_isSymbol(m,2))
chris@552 129 return;
chris@552 130 char * symbol = hv_msg_getSymbol(m, 0);
chris@552 131
chris@552 132 if(strcmp(symbol, "in") == 0){
chris@552 133 direction = INPUT;
chris@552 134 } else if(strcmp(symbol, "out") == 0){
chris@552 135 direction = OUTPUT;
chris@552 136 } else if(strcmp(symbol, "disable") == 0){
chris@552 137 disable = true;
chris@552 138 } else {
chris@552 139 return;
chris@552 140 }
chris@552 141 int channel = hv_msg_getFloat(m, 1) - LIBPD_DIGITAL_OFFSET;
chris@552 142 if(disable == true){
chris@552 143 dcm.unmanage(channel);
chris@552 144 return;
chris@552 145 }
chris@552 146 if(numArgs >= 3){
chris@552 147 char* s = hv_msg_getSymbol(m, 2);
chris@552 148 if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
chris@552 149 isMessageRate = false;
chris@552 150 }
chris@552 151 }
chris@552 152 dcm.manage(channel, direction, isMessageRate);
chris@552 153 }
chris@552 154 }
chris@552 155
chris@552 156
chris@552 157 /*
chris@552 158 * SETUP, RENDER LOOP & CLEANUP
chris@552 159 */
chris@552 160
chris@552 161 // leaving this here, trying to come up with a coherent interface with libpd.
chris@552 162 // commenting them out so the compiler does not warn
chris@552 163 // 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
chris@552 164 //static const unsigned int gChannelsInUse = 30;
chris@552 165 //static unsigned int gAnalogChannelsInUse = 8; // hard-coded for the moment, TODO: get it at run-time from hv_context
chris@552 166 //static const unsigned int gFirstAudioChannel = 0;
chris@552 167 //static const unsigned int gFirstAnalogChannel = 2;
chris@552 168 static const unsigned int gFirstDigitalChannel = 10;
chris@552 169 static const unsigned int gFirstScopeChannel = 26;
chris@552 170 static unsigned int gDigitalSigInChannelsInUse;
chris@552 171 static unsigned int gDigitalSigOutChannelsInUse;
chris@552 172
chris@552 173 // Bela Midi
chris@552 174 Midi midi;
chris@552 175 unsigned int hvMidiHashes[7];
chris@552 176 // Bela Scope
chris@552 177 Scope scope;
chris@552 178 unsigned int gScopeChannelsInUse;
chris@552 179 float* gScopeOut;
chris@552 180
chris@552 181
chris@552 182 bool setup(BelaContext *context, void *userData) {
chris@552 183 if(context->audioInChannels != context->audioOutChannels ||
chris@552 184 context->analogInChannels != context->analogOutChannels){
chris@552 185 // It should actually work, but let's test it before releasing it!
chris@552 186 printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n");
chris@552 187 return false;
chris@552 188 }
chris@552 189
chris@552 190 /*
chris@552 191 * MODIFICATION
chris@552 192 * ------------
chris@552 193 * Initialise variables for tremolo effect
chris@552 194 */
chris@552 195
chris@552 196 gPhase = 0.0;
chris@552 197
chris@552 198 /*********/
chris@552 199
chris@552 200 /* HEAVY */
chris@552 201 hvMidiHashes[kmmNoteOn] = hv_stringToHash("__hv_notein");
chris@552 202 // hvMidiHashes[kmmNoteOff] = hv_stringToHash("noteoff"); // this is handled differently, see the render function
chris@552 203 hvMidiHashes[kmmControlChange] = hv_stringToHash("__hv_ctlin");
chris@552 204 // Note that the ones below are not defined by Heavy, but they are here for (wishing) forward-compatibility
chris@552 205 // You need to receive from the corresponding symbol in Pd and unpack the message, e.g.:
chris@552 206 //[r __hv_pgmin]
chris@552 207 //|
chris@552 208 //[unpack f f]
chris@552 209 //| |
chris@552 210 //| [print pgmin_channel]
chris@552 211 //[print pgmin_number]
chris@552 212 hvMidiHashes[kmmProgramChange] = hv_stringToHash("__hv_pgmin");
chris@552 213 hvMidiHashes[kmmPolyphonicKeyPressure] = hv_stringToHash("__hv_polytouchin");
chris@552 214 hvMidiHashes[kmmChannelPressure] = hv_stringToHash("__hv_touchin");
chris@552 215 hvMidiHashes[kmmPitchBend] = hv_stringToHash("__hv_bendin");
chris@552 216
chris@552 217 gHeavyContext = hv_bela_new(context->audioSampleRate);
chris@552 218
chris@552 219 gHvInputChannels = hv_getNumInputChannels(gHeavyContext);
chris@552 220 gHvOutputChannels = hv_getNumOutputChannels(gHeavyContext);
chris@552 221
chris@552 222 gScopeChannelsInUse = gHvOutputChannels > gFirstScopeChannel ?
chris@552 223 gHvOutputChannels - gFirstScopeChannel : 0;
chris@552 224 gDigitalSigInChannelsInUse = gHvInputChannels > gFirstDigitalChannel ?
chris@552 225 gHvInputChannels - gFirstDigitalChannel : 0;
chris@552 226 gDigitalSigOutChannelsInUse = gHvOutputChannels > gFirstDigitalChannel ?
chris@552 227 gHvOutputChannels - gFirstDigitalChannel - gScopeChannelsInUse: 0;
chris@552 228
chris@552 229 printf("Starting Heavy context with %d input channels and %d output channels\n",
chris@552 230 gHvInputChannels, gHvOutputChannels);
chris@552 231 printf("Channels in use:\n");
chris@552 232 printf("Digital in : %u, Digital out: %u\n", gDigitalSigInChannelsInUse, gDigitalSigOutChannelsInUse);
chris@552 233 printf("Scope out: %u\n", gScopeChannelsInUse);
chris@552 234
chris@552 235 if(gHvInputChannels != 0) {
chris@552 236 gHvInputBuffers = (float *)calloc(gHvInputChannels * context->audioFrames,sizeof(float));
chris@552 237 }
chris@552 238 if(gHvOutputChannels != 0) {
chris@552 239 gHvOutputBuffers = (float *)calloc(gHvOutputChannels * context->audioFrames,sizeof(float));
chris@552 240 }
chris@552 241
chris@552 242 gInverseSampleRate = 1.0 / context->audioSampleRate;
chris@552 243
chris@552 244 // Set heavy print hook
chris@552 245 hv_setPrintHook(gHeavyContext, printHook);
chris@552 246 // Set heavy send hook
chris@552 247 hv_setSendHook(gHeavyContext, sendHook);
chris@552 248
chris@552 249 // TODO: change these hardcoded port values and actually change them in the Midi class
chris@552 250 midi.readFrom(0);
chris@552 251 midi.writeTo(0);
chris@552 252 midi.enableParser(true);
chris@552 253
chris@552 254 if(gScopeChannelsInUse > 0){
chris@552 255 // block below copy/pasted from libpd, except
chris@552 256 scope.setup(gScopeChannelsInUse, context->audioSampleRate);
chris@552 257 gScopeOut = new float[gScopeChannelsInUse];
chris@552 258 }
chris@552 259 // Bela digital
chris@552 260 dcm.setCallback(sendDigitalMessage);
chris@552 261 if(context->digitalChannels > 0){
chris@552 262 for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
chris@552 263 dcm.setCallbackArgument(ch, hvDigitalInHashes[ch]);
chris@552 264 }
chris@552 265 }
chris@552 266 // unlike libpd, no need here to bind the bela_digitalOut.. receivers
chris@552 267
chris@552 268 return true;
chris@552 269 }
chris@552 270
chris@552 271
chris@552 272 void render(BelaContext *context, void *userData)
chris@552 273 {
chris@552 274 {
chris@552 275 int num;
chris@552 276 while((num = midi.getParser()->numAvailableMessages()) > 0){
chris@552 277 static MidiChannelMessage message;
chris@552 278 message = midi.getParser()->getNextChannelMessage();
chris@552 279 switch(message.getType()){
chris@552 280 case kmmNoteOn: {
chris@552 281 //message.prettyPrint();
chris@552 282 int noteNumber = message.getDataByte(0);
chris@552 283 int velocity = message.getDataByte(1);
chris@552 284 int channel = message.getChannel();
chris@552 285 // rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel);
chris@552 286 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff",
chris@552 287 (float)noteNumber, (float)velocity, (float)channel+1);
chris@552 288 break;
chris@552 289 }
chris@552 290 case kmmNoteOff: {
chris@552 291 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
chris@552 292 * so that the noteoff velocity is ignored. Here we convert them to noteon
chris@552 293 * with a velocity of 0.
chris@552 294 */
chris@552 295 int noteNumber = message.getDataByte(0);
chris@552 296 // int velocity = message.getDataByte(1); // would be ignored by Pd
chris@552 297 int channel = message.getChannel();
chris@552 298 // note we are sending the below to hvHashes[kmmNoteOn] !!
chris@552 299 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff",
chris@552 300 (float)noteNumber, (float)0, (float)channel+1);
chris@552 301 break;
chris@552 302 }
chris@552 303 case kmmControlChange: {
chris@552 304 int channel = message.getChannel();
chris@552 305 int controller = message.getDataByte(0);
chris@552 306 int value = message.getDataByte(1);
chris@552 307 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmControlChange], 0, "fff",
chris@552 308 (float)value, (float)controller, (float)channel+1);
chris@552 309 break;
chris@552 310 }
chris@552 311 case kmmProgramChange: {
chris@552 312 int channel = message.getChannel();
chris@552 313 int program = message.getDataByte(0);
chris@552 314 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmProgramChange], 0, "ff",
chris@552 315 (float)program, (float)channel+1);
chris@552 316 break;
chris@552 317 }
chris@552 318 case kmmPolyphonicKeyPressure: {
chris@552 319 //TODO: untested, I do not have anything with polyTouch... who does, anyhow?
chris@552 320 int channel = message.getChannel();
chris@552 321 int pitch = message.getDataByte(0);
chris@552 322 int value = message.getDataByte(1);
chris@552 323 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPolyphonicKeyPressure], 0, "fff",
chris@552 324 (float)channel+1, (float)pitch, (float)value);
chris@552 325 break;
chris@552 326 }
chris@552 327 case kmmChannelPressure:
chris@552 328 {
chris@552 329 int channel = message.getChannel();
chris@552 330 int value = message.getDataByte(0);
chris@552 331 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmChannelPressure], 0, "ff",
chris@552 332 (float)value, (float)channel+1);
chris@552 333 break;
chris@552 334 }
chris@552 335 case kmmPitchBend:
chris@552 336 {
chris@552 337 int channel = message.getChannel();
chris@552 338 int value = ((message.getDataByte(1) << 7) | message.getDataByte(0));
chris@552 339 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPitchBend], 0, "ff",
chris@552 340 (float)value, (float)channel+1);
chris@552 341 break;
chris@552 342 }
chris@552 343 case kmmNone:
chris@552 344 case kmmAny:
chris@552 345 break;
chris@552 346 }
chris@552 347 }
chris@552 348 }
chris@552 349
chris@552 350 // De-interleave the data
chris@552 351 if(gHvInputBuffers != NULL) {
chris@552 352 for(unsigned int n = 0; n < context->audioFrames; n++) {
chris@552 353 for(unsigned int ch = 0; ch < gHvInputChannels; ch++) {
chris@552 354 if(ch >= context->audioInChannels+context->analogInChannels) {
chris@552 355 // THESE ARE PARAMETER INPUT 'CHANNELS' USED FOR ROUTING
chris@552 356 // 'sensor' outputs from routing channels of dac~ are passed through here
chris@552 357 break;
chris@552 358 } else {
chris@552 359 // If more than 2 ADC inputs are used in the pd patch, route the analog inputs
chris@552 360 // i.e. ADC3->analogIn0 etc. (first two are always audio inputs)
chris@552 361 if(ch >= context->audioInChannels) {
chris@552 362 int m = n/2;
chris@552 363 float mIn = context->analogIn[m*context->analogInChannels + (ch-context->audioInChannels)];
chris@552 364 gHvInputBuffers[ch * context->audioFrames + n] = mIn;
chris@552 365 } else {
chris@552 366 gHvInputBuffers[ch * context->audioFrames + n] = context->audioIn[n * context->audioInChannels + ch];
chris@552 367 }
chris@552 368 }
chris@552 369 }
chris@552 370 }
chris@552 371 }
chris@552 372
chris@552 373 // Bela digital in
chris@552 374 // note: in multiple places below we assume that the number of digital frames is same as number of audio
chris@552 375 // Bela digital in at message-rate
chris@552 376 dcm.processInput(context->digital, context->digitalFrames);
chris@552 377
chris@552 378 // Bela digital in at signal-rate
chris@552 379 if(gDigitalSigInChannelsInUse > 0)
chris@552 380 {
chris@552 381 unsigned int j, k;
chris@552 382 float *p0, *p1;
chris@552 383 const unsigned int gLibpdBlockSize = context->audioFrames;
chris@552 384 const unsigned int audioFrameBase = 0;
chris@552 385 float* gInBuf = gHvInputBuffers;
chris@552 386 // block below copy/pasted from libpd, except
chris@552 387 // 16 has been replaced with gDigitalSigInChannelsInUse
chris@552 388 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
chris@552 389 unsigned int digitalFrame = audioFrameBase + j;
chris@552 390 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
chris@552 391 k < gDigitalSigInChannelsInUse; ++k, p1 += gLibpdBlockSize) {
chris@552 392 if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
chris@552 393 *p1 = digitalRead(context, digitalFrame, k);
chris@552 394 }
chris@552 395 }
chris@552 396 }
chris@552 397 }
chris@552 398
chris@552 399
chris@552 400 // replacement for bang~ object
chris@552 401 //hv_vscheduleMessageForReceiver(gHeavyContext, "bela_bang", 0.0f, "b");
chris@552 402
chris@552 403 hv_bela_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames);
chris@552 404
chris@552 405 // Bela digital out
chris@552 406 // Bela digital out at signal-rate
chris@552 407 if(gDigitalSigOutChannelsInUse > 0)
chris@552 408 {
chris@552 409 unsigned int j, k;
chris@552 410 float *p0, *p1;
chris@552 411 const unsigned int gLibpdBlockSize = context->audioFrames;
chris@552 412 const unsigned int audioFrameBase = 0;
chris@552 413 float* gOutBuf = gHvOutputBuffers;
chris@552 414 // block below copy/pasted from libpd, except
chris@552 415 // context->digitalChannels has been replaced with gDigitalSigOutChannelsInUse
chris@552 416 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
chris@552 417 unsigned int digitalFrame = (audioFrameBase + j);
chris@552 418 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
chris@552 419 k < gDigitalSigOutChannelsInUse; k++, p1 += gLibpdBlockSize) {
chris@552 420 if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
chris@552 421 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
chris@552 422 }
chris@552 423 }
chris@552 424 }
chris@552 425 }
chris@552 426 // Bela digital out at message-rate
chris@552 427 dcm.processOutput(context->digital, context->digitalFrames);
chris@552 428
chris@552 429 // Bela scope
chris@552 430 if(gScopeChannelsInUse > 0)
chris@552 431 {
chris@552 432 unsigned int j, k;
chris@552 433 float *p0, *p1;
chris@552 434 const unsigned int gLibpdBlockSize = context->audioFrames;
chris@552 435 float* gOutBuf = gHvOutputBuffers;
chris@552 436
chris@552 437 // block below copy/pasted from libpd
chris@552 438 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
chris@552 439 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
chris@552 440 gScopeOut[k] = *p1;
chris@552 441 }
chris@552 442 scope.log(gScopeOut);
chris@552 443 }
chris@552 444 }
chris@552 445
chris@552 446 // Interleave the output data
chris@552 447 if(gHvOutputBuffers != NULL) {
chris@552 448 for(unsigned int n = 0; n < context->audioFrames; n++) {
chris@552 449
chris@552 450 /*
chris@552 451 * MODIFICATION
chris@552 452 * ------------
chris@552 453 * Processing for tremolo effect while writing libpd output to Bela output buffer
chris@552 454 */
chris@552 455
chris@552 456 // Generate a sinewave with frequency set by gTremoloRate
chris@552 457 // and amplitude from -0.5 to 0.5
chris@552 458 float lfo = sinf(gPhase) * 0.5;
chris@552 459 // Keep track and wrap the phase of the sinewave
chris@552 460 gPhase += 2.0 * M_PI * gTremoloRate * gInverseSampleRate;
chris@552 461 if(gPhase > 2.0 * M_PI)
chris@552 462 gPhase -= 2.0 * M_PI;
chris@552 463
chris@552 464 /*********/
chris@552 465
chris@552 466 for(unsigned int ch = 0; ch < gHvOutputChannels; ch++) {
chris@552 467 if(ch <= context->audioOutChannels+context->analogOutChannels) {
chris@552 468 if(ch >= context->audioOutChannels) {
chris@552 469 int m = n/2;
chris@552 470 context->analogOut[m * context->analogFrames + (ch-context->audioOutChannels)] = constrain(gHvOutputBuffers[ch*context->audioFrames + n],0.0,1.0);
chris@552 471 } else {
chris@552 472 context->audioOut[n * context->audioOutChannels + ch] = gHvOutputBuffers[ch * context->audioFrames + n] * lfo; // MODIFICATION (* lfo)
chris@552 473 }
chris@552 474 }
chris@552 475 }
chris@552 476 }
chris@552 477 }
chris@552 478
chris@552 479 }
chris@552 480
chris@552 481
chris@552 482 void cleanup(BelaContext *context, void *userData)
chris@552 483 {
chris@552 484
chris@552 485 hv_bela_free(gHeavyContext);
chris@552 486 if(gHvInputBuffers != NULL)
chris@552 487 free(gHvInputBuffers);
chris@552 488 if(gHvOutputBuffers != NULL)
chris@552 489 free(gHvOutputBuffers);
chris@552 490 delete[] gScopeOut;
chris@552 491 }