annotate scripts/hvresources/heavy_render.cpp @ 486:7f8d1a3e4cef prerelease

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