annotate core/default_libpd_render.cpp @ 523:42f6af3a5f1d prerelease

build_project and build_pd_heavy mostly done
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 23 Jun 2016 04:31:09 +0100
parents 485913c58a61
children bfcbeb437869
rev   line source
giuliomoro@230 1 /*
giuliomoro@230 2 * render.cpp
giuliomoro@230 3 *
giuliomoro@230 4 * Created on: Oct 24, 2014
giuliomoro@230 5 * Author: parallels
giuliomoro@230 6 */
giuliomoro@230 7
giuliomoro@301 8 #include <Bela.h>
giuliomoro@353 9 #include <DigitalChannelManager.h>
giuliomoro@230 10 #include <cmath>
giuliomoro@230 11 #include <I2c_Codec.h>
giuliomoro@230 12 #include <PRU.h>
giuliomoro@230 13 #include <stdio.h>
giuliomoro@467 14 #include <libpd/z_libpd.h>
giuliomoro@467 15 #include <libpd/s_stuff.h>
giuliomoro@230 16 #include <UdpServer.h>
giuliomoro@324 17 #include <Midi.h>
giuliomoro@474 18 #include <Scope.h>
giuliomoro@474 19
giuliomoro@345 20 // if you are 100% sure of what value was used to compile libpd/puredata, then
giuliomoro@474 21 // you could #define gBufLength instead of getting it at runtime. It has proved to give some 0.3%
giuliomoro@343 22 // performance boost when it is 8 (thanks to vectorize optimizations I guess).
giuliomoro@230 23 int gBufLength;
giuliomoro@230 24
giuliomoro@230 25 float* gInBuf;
giuliomoro@230 26 float* gOutBuf;
giuliomoro@230 27
giuliomoro@230 28 void pdnoteon(int ch, int pitch, int vel) {
giuliomoro@230 29 printf("noteon: %d %d %d\n", ch, pitch, vel);
giuliomoro@230 30 }
giuliomoro@230 31
giuliomoro@301 32 void Bela_printHook(const char *recv){
giuliomoro@230 33 rt_printf("%s", recv);
giuliomoro@230 34 }
giuliomoro@325 35 #define PARSE_MIDI
giuliomoro@353 36 static Midi midi;
giuliomoro@353 37 static DigitalChannelManager dcm;
giuliomoro@337 38
giuliomoro@345 39 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
giuliomoro@345 40 libpd_float((char*)receiverName, (float)state);
giuliomoro@345 41 // rt_printf("%s: %d\n", (char*)receiverName, state);
giuliomoro@345 42 }
giuliomoro@350 43
giuliomoro@352 44 #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
giuliomoro@352 45
giuliomoro@352 46 void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
giuliomoro@352 47 if(strcmp(source, "bela_setDigital") == 0){
giuliomoro@352 48 // symbol is the direction, argv[0] is the channel, argv[1] (optional)
giuliomoro@363 49 // is signal("sig" or "~") or message("message", default) rate
giuliomoro@353 50 bool isMessageRate = true; // defaults to message rate
giuliomoro@354 51 bool direction = 0; // initialize it just to avoid the compiler's warning
giuliomoro@354 52 bool disable = false;
giuliomoro@352 53 if(strcmp(symbol, "in") == 0){
giuliomoro@352 54 direction = INPUT;
giuliomoro@352 55 } else if(strcmp(symbol, "out") == 0){
giuliomoro@352 56 direction = OUTPUT;
giuliomoro@354 57 } else if(strcmp(symbol, "disable") == 0){
giuliomoro@354 58 disable = true;
giuliomoro@352 59 } else {
giuliomoro@354 60 return;
giuliomoro@352 61 }
giuliomoro@352 62 if(argc == 0){
giuliomoro@354 63 return;
giuliomoro@352 64 } else if (libpd_is_float(&argv[0]) == false){
giuliomoro@352 65 return;
giuliomoro@352 66 }
giuliomoro@352 67 int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
giuliomoro@354 68 if(disable == true){
giuliomoro@354 69 dcm.unmanage(channel);
giuliomoro@355 70 return;
giuliomoro@354 71 }
giuliomoro@352 72 if(argc >= 2){
giuliomoro@352 73 t_atom* a = &argv[1];
giuliomoro@352 74 if(libpd_is_symbol(a)){
giuliomoro@352 75 char *s = libpd_get_symbol(a);
giuliomoro@352 76 if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
giuliomoro@353 77 isMessageRate = false;
giuliomoro@352 78 }
giuliomoro@352 79 }
giuliomoro@352 80 }
giuliomoro@354 81 dcm.manage(channel, direction, isMessageRate);
giuliomoro@352 82 }
giuliomoro@352 83 }
giuliomoro@352 84
giuliomoro@350 85 void Bela_floatHook(const char *source, float value){
giuliomoro@350 86 // let's make this as optimized as possible for built-in digital Out parsing
giuliomoro@350 87 // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
giuliomoro@350 88 static int prefixLength = 15; // strlen("bela_digitalOut")
giuliomoro@350 89 if(strncmp(source, "bela_digitalOut", prefixLength)==0){
giuliomoro@350 90 if(source[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
giuliomoro@350 91 if(source[prefixLength + 1] != 0){
giuliomoro@357 92 // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
giuliomoro@350 93 int receiver = ((source[prefixLength] - 48) * 10);
giuliomoro@350 94 receiver += (source[prefixLength+1] - 48);
giuliomoro@350 95 unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number
giuliomoro@474 96 if(channel < 16){ //16 is the hardcoded value for the number of digital channels
giuliomoro@354 97 dcm.setValue(channel, value);
giuliomoro@350 98 }
giuliomoro@350 99 }
giuliomoro@350 100 }
giuliomoro@350 101 }
giuliomoro@350 102 }
giuliomoro@350 103
giuliomoro@345 104 char receiverNames[16][21]={
giuliomoro@345 105 {"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
giuliomoro@345 106 {"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
giuliomoro@345 107 {"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
giuliomoro@345 108 {"bela_digitalIn26"}
giuliomoro@345 109 };
giuliomoro@345 110
giuliomoro@474 111 static unsigned int gAnalogChannelsInUse;
giuliomoro@345 112 static unsigned int gLibpdBlockSize;
giuliomoro@474 113 // 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
giuliomoro@474 114 static const unsigned int gChannelsInUse = 30;
giuliomoro@476 115 //static const unsigned int gFirstAudioChannel = 0;
giuliomoro@474 116 static const unsigned int gFirstAnalogChannel = 2;
giuliomoro@474 117 static const unsigned int gFirstDigitalChannel = 10;
giuliomoro@474 118 static const unsigned int gFirstScopeChannel = 26;
giuliomoro@345 119
giuliomoro@474 120 Scope scope;
giuliomoro@483 121 unsigned int gScopeChannelsInUse = 4;
giuliomoro@476 122 float* gScopeOut;
giuliomoro@476 123
giuliomoro@301 124 bool setup(BelaContext *context, void *userData)
giuliomoro@230 125 {
giuliomoro@483 126 scope.setup(gScopeChannelsInUse, context->audioSampleRate);
giuliomoro@483 127 gScopeOut = new float[gScopeChannelsInUse];
giuliomoro@474 128
giuliomoro@470 129 // Check first of all if file exists. Will actually open it later.
giuliomoro@470 130 char file[] = "_main.pd";
giuliomoro@470 131 char folder[] = "./";
giuliomoro@470 132 unsigned int strSize = strlen(file) + strlen(folder) + 1;
giuliomoro@470 133 char* str = (char*)malloc(sizeof(char) * strSize);
giuliomoro@470 134 snprintf(str, strSize, "%s%s", folder, file);
giuliomoro@470 135 if(access(str, F_OK) == -1 ) {
giuliomoro@470 136 printf("Error file %s/%s not found. The %s file should be your main patch.\n", folder, file, file);
giuliomoro@470 137 return false;
giuliomoro@470 138 }
giuliomoro@480 139
giuliomoro@480 140 // analog setup
giuliomoro@480 141 gAnalogChannelsInUse = context->analogChannels;
giuliomoro@480 142
giuliomoro@480 143 // digital setup
giuliomoro@353 144 dcm.setCallback(sendDigitalMessage);
giuliomoro@345 145 if(context->digitalChannels > 0){
giuliomoro@345 146 for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
giuliomoro@353 147 dcm.setCallbackArgument(ch, receiverNames[ch]);
giuliomoro@345 148 }
giuliomoro@345 149 }
giuliomoro@480 150
giuliomoro@324 151 midi.readFrom(0);
giuliomoro@324 152 midi.writeTo(0);
giuliomoro@325 153 #ifdef PARSE_MIDI
giuliomoro@324 154 midi.enableParser(true);
giuliomoro@325 155 #else
giuliomoro@325 156 midi.enableParser(false);
giuliomoro@325 157 #endif /* PARSE_MIDI */
giuliomoro@342 158 // udpServer.bindToPort(1234);
giuliomoro@230 159
giuliomoro@340 160 gLibpdBlockSize = libpd_blocksize();
giuliomoro@337 161 // check that we are not running with a blocksize smaller than gLibPdBlockSize
giuliomoro@474 162 // We could still make it work, but the load would be executed unevenly between calls to render
giuliomoro@340 163 if(context->audioFrames < gLibpdBlockSize){
giuliomoro@340 164 fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
giuliomoro@230 165 return false;
giuliomoro@230 166 }
giuliomoro@349 167 // set hooks before calling libpd_init
giuliomoro@349 168 libpd_set_printhook(Bela_printHook);
giuliomoro@349 169 libpd_set_floathook(Bela_floatHook);
giuliomoro@352 170 libpd_set_messagehook(Bela_messageHook);
giuliomoro@349 171 libpd_set_noteonhook(pdnoteon);
giuliomoro@340 172 //TODO: add hooks for other midi events and generate MIDI output appropriately
giuliomoro@349 173 libpd_init();
giuliomoro@357 174 //TODO: ideally, we would analyse the ASCII of the patch file and find out which in/outs to use
giuliomoro@230 175 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
giuliomoro@350 176 gInBuf = libpd_get_sys_soundin();
giuliomoro@350 177 gOutBuf = libpd_get_sys_soundout();
giuliomoro@230 178
giuliomoro@230 179 libpd_start_message(1); // one entry in list
giuliomoro@230 180 libpd_add_float(1.0f);
giuliomoro@230 181 libpd_finish_message("pd", "dsp");
giuliomoro@230 182
giuliomoro@340 183 gBufLength = max(gLibpdBlockSize, context->audioFrames);
giuliomoro@230 184
giuliomoro@350 185
giuliomoro@350 186 // bind your receivers here
giuliomoro@350 187 libpd_bind("bela_digitalOut11");
giuliomoro@350 188 libpd_bind("bela_digitalOut12");
giuliomoro@350 189 libpd_bind("bela_digitalOut13");
giuliomoro@350 190 libpd_bind("bela_digitalOut14");
giuliomoro@350 191 libpd_bind("bela_digitalOut15");
giuliomoro@350 192 libpd_bind("bela_digitalOut16");
giuliomoro@350 193 libpd_bind("bela_digitalOut17");
giuliomoro@350 194 libpd_bind("bela_digitalOut18");
giuliomoro@350 195 libpd_bind("bela_digitalOut19");
giuliomoro@350 196 libpd_bind("bela_digitalOut20");
giuliomoro@350 197 libpd_bind("bela_digitalOut21");
giuliomoro@350 198 libpd_bind("bela_digitalOut22");
giuliomoro@350 199 libpd_bind("bela_digitalOut23");
giuliomoro@350 200 libpd_bind("bela_digitalOut24");
giuliomoro@350 201 libpd_bind("bela_digitalOut25");
giuliomoro@350 202 libpd_bind("bela_digitalOut26");
giuliomoro@352 203 libpd_bind("bela_setDigital");
giuliomoro@350 204
giuliomoro@353 205 // open patch [; pd open file folder(
giuliomoro@470 206 void* patch = libpd_openfile(file, folder);
giuliomoro@470 207 if(patch == NULL){
giuliomoro@470 208 printf("Error: file %s/%s is corrupted.\n", folder, file);
giuliomoro@470 209 return false;
giuliomoro@470 210 }
giuliomoro@230 211 return true;
giuliomoro@230 212 }
giuliomoro@230 213
giuliomoro@230 214 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@230 215 // Input and output are given from the audio hardware and the other
giuliomoro@230 216 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@230 217 // will be 0.
giuliomoro@345 218
giuliomoro@301 219 void render(BelaContext *context, void *userData)
giuliomoro@230 220 {
giuliomoro@324 221 int num;
giuliomoro@341 222 // the safest thread-safe option to handle MIDI input is to process the MIDI buffer
giuliomoro@341 223 // from the audio thread.
giuliomoro@325 224 #ifdef PARSE_MIDI
giuliomoro@324 225 while((num = midi.getParser()->numAvailableMessages()) > 0){
giuliomoro@324 226 static MidiChannelMessage message;
giuliomoro@324 227 message = midi.getParser()->getNextChannelMessage();
giuliomoro@341 228 //message.prettyPrint(); // use this to print beautified message (channel, data bytes)
giuliomoro@324 229 switch(message.getType()){
giuliomoro@325 230 case kmmNoteOn:
giuliomoro@325 231 {
giuliomoro@324 232 int noteNumber = message.getDataByte(0);
giuliomoro@324 233 int velocity = message.getDataByte(1);
giuliomoro@324 234 int channel = message.getChannel();
giuliomoro@324 235 libpd_noteon(channel, noteNumber, velocity);
giuliomoro@325 236 break;
giuliomoro@324 237 }
giuliomoro@325 238 case kmmNoteOff:
giuliomoro@325 239 {
giuliomoro@325 240 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
giuliomoro@325 241 * so that the noteoff velocity is ignored. Here we convert them to noteon
giuliomoro@325 242 * with a velocity of 0.
giuliomoro@325 243 */
giuliomoro@325 244 int noteNumber = message.getDataByte(0);
giuliomoro@325 245 // int velocity = message.getDataByte(1); // would be ignored by Pd
giuliomoro@325 246 int channel = message.getChannel();
giuliomoro@325 247 libpd_noteon(channel, noteNumber, 0);
giuliomoro@325 248 break;
giuliomoro@324 249 }
giuliomoro@325 250 case kmmControlChange:
giuliomoro@325 251 {
giuliomoro@325 252 int channel = message.getChannel();
giuliomoro@325 253 int controller = message.getDataByte(0);
giuliomoro@325 254 int value = message.getDataByte(1);
giuliomoro@325 255 libpd_controlchange(channel, controller, value);
giuliomoro@325 256 break;
giuliomoro@325 257 }
giuliomoro@325 258 case kmmProgramChange:
giuliomoro@325 259 {
giuliomoro@325 260 int channel = message.getChannel();
giuliomoro@325 261 int program = message.getDataByte(0);
giuliomoro@325 262 libpd_programchange(channel, program);
giuliomoro@325 263 break;
giuliomoro@325 264 }
giuliomoro@325 265 case kmmPolyphonicKeyPressure:
giuliomoro@325 266 {
giuliomoro@325 267 int channel = message.getChannel();
giuliomoro@325 268 int pitch = message.getDataByte(0);
giuliomoro@325 269 int value = message.getDataByte(1);
giuliomoro@325 270 libpd_polyaftertouch(channel, pitch, value);
giuliomoro@325 271 break;
giuliomoro@325 272 }
giuliomoro@325 273 case kmmChannelPressure:
giuliomoro@325 274 {
giuliomoro@325 275 int channel = message.getChannel();
giuliomoro@325 276 int value = message.getDataByte(0);
giuliomoro@325 277 libpd_aftertouch(channel, value);
giuliomoro@325 278 break;
giuliomoro@325 279 }
giuliomoro@325 280 case kmmPitchBend:
giuliomoro@325 281 {
giuliomoro@325 282 int channel = message.getChannel();
giuliomoro@495 283 int value = ((message.getDataByte(1) << 7)| message.getDataByte(0)) - 8192;
giuliomoro@325 284 libpd_pitchbend(channel, value);
giuliomoro@325 285 break;
giuliomoro@325 286 }
giuliomoro@337 287 case kmmNone:
giuliomoro@337 288 case kmmAny:
giuliomoro@337 289 break;
giuliomoro@324 290 }
giuliomoro@324 291 }
giuliomoro@325 292 #else
giuliomoro@325 293 int input;
giuliomoro@325 294 while((input = midi.getInput()) >= 0){
giuliomoro@325 295 libpd_midibyte(0, input);
giuliomoro@325 296 }
giuliomoro@325 297 #endif /* PARSE_MIDI */
giuliomoro@340 298
giuliomoro@341 299 static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize;
giuliomoro@350 300
giuliomoro@343 301 for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){
giuliomoro@345 302 unsigned int audioFrameBase = gLibpdBlockSize * tick;
giuliomoro@343 303 unsigned int j;
giuliomoro@343 304 unsigned int k;
giuliomoro@343 305 float* p0;
giuliomoro@343 306 float* p1;
giuliomoro@343 307 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 308 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
giuliomoro@345 309 *p1 = audioRead(context, audioFrameBase + j, k);
giuliomoro@232 310 }
giuliomoro@343 311 }
giuliomoro@343 312 // then analogs
giuliomoro@343 313 // this loop resamples by ZOH, as needed, using m
giuliomoro@343 314 if(context->analogChannels == 8 ){ //hold the value for two frames
giuliomoro@343 315 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@474 316 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
giuliomoro@345 317 unsigned int analogFrame = (audioFrameBase + j) / 2;
giuliomoro@343 318 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 319 }
giuliomoro@343 320 }
giuliomoro@343 321 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@343 322 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@474 323 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
giuliomoro@345 324 unsigned int analogFrame = audioFrameBase + j;
giuliomoro@343 325 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 326 }
giuliomoro@343 327 }
giuliomoro@343 328 } else if(context->analogChannels == 2){ //drop every other frame
giuliomoro@343 329 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@474 330 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
giuliomoro@345 331 unsigned int analogFrame = (audioFrameBase + j) * 2;
giuliomoro@343 332 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 333 }
giuliomoro@232 334 }
giuliomoro@230 335 }
giuliomoro@345 336
giuliomoro@480 337 // Bela digital input
giuliomoro@353 338 // note: in multiple places below we assume that the number of digitals is same as number of audio
giuliomoro@348 339 // digital in at message-rate
giuliomoro@353 340 dcm.processInput(&context->digital[audioFrameBase], gLibpdBlockSize);
giuliomoro@353 341
giuliomoro@348 342 // digital in at signal-rate
giuliomoro@348 343 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@353 344 unsigned int digitalFrame = audioFrameBase + j;
giuliomoro@474 345 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
giuliomoro@348 346 k < 16; ++k, p1 += gLibpdBlockSize) {
giuliomoro@353 347 if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
giuliomoro@353 348 *p1 = digitalRead(context, digitalFrame, k);
giuliomoro@353 349 }
giuliomoro@348 350 }
giuliomoro@348 351 }
giuliomoro@345 352
giuliomoro@342 353 libpd_process_sys(); // process the block
giuliomoro@345 354
giuliomoro@348 355 //digital out
giuliomoro@353 356 // digital out at signal-rate
giuliomoro@345 357 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
giuliomoro@345 358 unsigned int digitalFrame = (audioFrameBase + j);
giuliomoro@474 359 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
giuliomoro@345 360 k < context->digitalChannels; k++, p1 += gLibpdBlockSize) {
giuliomoro@353 361 if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
giuliomoro@353 362 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
giuliomoro@353 363 }
giuliomoro@345 364 }
giuliomoro@345 365 }
giuliomoro@348 366
giuliomoro@350 367 // digital out at message-rate
giuliomoro@353 368 dcm.processOutput(&context->digital[audioFrameBase], gLibpdBlockSize);
giuliomoro@350 369
giuliomoro@345 370 //audio
giuliomoro@343 371 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 372 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
giuliomoro@345 373 audioWrite(context, audioFrameBase + j, k, *p1);
giuliomoro@341 374 }
giuliomoro@343 375 }
giuliomoro@474 376 //scope
giuliomoro@474 377 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
giuliomoro@483 378 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@476 379 gScopeOut[k] = *p1;
giuliomoro@474 380 }
giuliomoro@476 381 scope.log(gScopeOut[0], gScopeOut[1], gScopeOut[2], gScopeOut[3]);
giuliomoro@474 382 }
giuliomoro@474 383
giuliomoro@354 384
giuliomoro@345 385 //analog
giuliomoro@343 386 if(context->analogChannels == 8){
giuliomoro@343 387 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames
giuliomoro@345 388 unsigned int analogFrame = (audioFrameBase + j) / 2;
giuliomoro@474 389 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@345 390 analogWriteOnce(context, analogFrame, k, *p1);
giuliomoro@341 391 }
giuliomoro@343 392 }
giuliomoro@343 393 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@343 394 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
giuliomoro@345 395 unsigned int analogFrame = (audioFrameBase + j);
giuliomoro@474 396 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@345 397 analogWriteOnce(context, analogFrame, k, *p1);
giuliomoro@341 398 }
giuliomoro@343 399 }
giuliomoro@343 400 } else if(context->analogChannels == 2){ //write every frame twice
giuliomoro@343 401 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@474 402 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@345 403 int analogFrame = audioFrameBase * 2 + j * 2;
giuliomoro@345 404 analogWriteOnce(context, analogFrame, k, *p1);
giuliomoro@345 405 analogWriteOnce(context, analogFrame + 1, k, *p1);
giuliomoro@232 406 }
giuliomoro@232 407 }
giuliomoro@230 408 }
giuliomoro@230 409 }
giuliomoro@230 410 }
giuliomoro@232 411
giuliomoro@230 412 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@230 413 // Release any resources that were allocated in setup().
giuliomoro@230 414
giuliomoro@301 415 void cleanup(BelaContext *context, void *userData)
giuliomoro@230 416 {
giuliomoro@476 417 delete [] gScopeOut;
giuliomoro@230 418 }