annotate examples/basic_libpd/render.cpp @ 352:749aa016fa16 prerelease

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