annotate examples/basic_libpd/render.cpp @ 337:5e2780bfbfed prerelease

Clean up of basic_libpd
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 05 Jun 2016 20:28:43 +0100
parents a9905703219d
children 1802f99cd77f
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@230 9 #include <cmath>
giuliomoro@230 10 #include <Utilities.h>
giuliomoro@230 11 #include <I2c_Codec.h>
giuliomoro@230 12 #include <PRU.h>
giuliomoro@230 13 #include <stdio.h>
giuliomoro@230 14 #include "z_libpd.h"
giuliomoro@230 15 #include <UdpServer.h>
giuliomoro@324 16 #include <Midi.h>
giuliomoro@324 17
giuliomoro@230 18
giuliomoro@230 19 // setup() is called once before the audio rendering starts.
giuliomoro@230 20 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@230 21 // on the period size or sample rate.
giuliomoro@230 22 //
giuliomoro@230 23 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@230 24 // in from the call to initAudio().
giuliomoro@230 25 //
giuliomoro@230 26 // Return true on success; returning false halts the program.
giuliomoro@337 27 unsigned int gLibPdBlockSize; //make sure this matches the one used to compile libpd
giuliomoro@230 28
giuliomoro@337 29 unsigned int gChannelsInUse = 10;
giuliomoro@230 30 int gBufLength;
giuliomoro@230 31
giuliomoro@230 32 float* gInBuf;
giuliomoro@230 33 float* gOutBuf;
giuliomoro@230 34
giuliomoro@230 35 void pdnoteon(int ch, int pitch, int vel) {
giuliomoro@230 36 printf("noteon: %d %d %d\n", ch, pitch, vel);
giuliomoro@230 37 }
giuliomoro@230 38
giuliomoro@301 39 void Bela_printHook(const char *recv){
giuliomoro@230 40 rt_printf("%s", recv);
giuliomoro@230 41 }
giuliomoro@230 42
giuliomoro@230 43 UdpServer udpServer;
giuliomoro@230 44
giuliomoro@230 45 void udpRead(){
giuliomoro@230 46 while(!gShouldStop){
giuliomoro@337 47 // check for modified sockets/file descriptors
giuliomoro@337 48 // (libpd would normally do this every block WITHIN the audio thread)
giuliomoro@337 49 // not sure if this is thread-safe at the moment
giuliomoro@230 50 libpd_sys_microsleep(0);
giuliomoro@230 51 usleep(1000);
giuliomoro@230 52 }
giuliomoro@230 53 }
giuliomoro@325 54 #define PARSE_MIDI
giuliomoro@337 55 AuxiliaryTask udpReadTask;
giuliomoro@324 56 Midi midi;
giuliomoro@337 57
giuliomoro@301 58 bool setup(BelaContext *context, void *userData)
giuliomoro@230 59 {
giuliomoro@324 60 midi.readFrom(0);
giuliomoro@324 61 midi.writeTo(0);
giuliomoro@325 62 #ifdef PARSE_MIDI
giuliomoro@324 63 midi.enableParser(true);
giuliomoro@325 64 #else
giuliomoro@325 65 midi.enableParser(false);
giuliomoro@325 66 #endif /* PARSE_MIDI */
giuliomoro@232 67 gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
giuliomoro@230 68 udpServer.bindToPort(1234);
giuliomoro@230 69
giuliomoro@337 70 gLibPdBlockSize = libpd_blocksize();
giuliomoro@337 71 // check that we are not running with a blocksize smaller than gLibPdBlockSize
giuliomoro@230 72 // it would still work, but the load would be executed unevenly between calls to render
giuliomoro@337 73 if(context->audioFrames < gLibPdBlockSize){
giuliomoro@337 74 fprintf(stderr, "Error: minimum block size must be %d\n", gLibPdBlockSize);
giuliomoro@230 75 return false;
giuliomoro@230 76 }
giuliomoro@230 77 // init pd
giuliomoro@301 78 libpd_set_printhook(Bela_printHook); // set this before calling libpd_init
giuliomoro@230 79 libpd_set_noteonhook(pdnoteon);
giuliomoro@230 80 libpd_init();
giuliomoro@230 81 //TODO: analyse the ASCII of the patch file and find the in/outs to use
giuliomoro@230 82 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
giuliomoro@230 83
giuliomoro@230 84 libpd_start_message(1); // one entry in list
giuliomoro@230 85 libpd_add_float(1.0f);
giuliomoro@230 86 libpd_finish_message("pd", "dsp");
giuliomoro@230 87
giuliomoro@337 88 gBufLength = max((uint32_t)gLibPdBlockSize, context->audioFrames);
giuliomoro@230 89 unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength;
giuliomoro@230 90 gInBuf = (float*)malloc(bufferSize);
giuliomoro@230 91 gOutBuf = (float*)malloc(bufferSize);
giuliomoro@230 92 // no need to memset to zero
giuliomoro@230 93
giuliomoro@230 94 char file[] = "_main.pd";
giuliomoro@230 95 char folder[] = "./";
giuliomoro@230 96 // open patch [; pd open file folder(
giuliomoro@230 97 libpd_openfile(file, folder);
giuliomoro@230 98
giuliomoro@301 99 udpReadTask = Bela_createAuxiliaryTask(udpRead, 60, "udpReadTask");
giuliomoro@301 100 Bela_scheduleAuxiliaryTask(udpReadTask);
giuliomoro@230 101 return true;
giuliomoro@230 102 }
giuliomoro@230 103
giuliomoro@230 104 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@230 105 // Input and output are given from the audio hardware and the other
giuliomoro@230 106 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@230 107 // will be 0.
giuliomoro@301 108 BelaContext *c;
giuliomoro@301 109 void render(BelaContext *context, void *userData)
giuliomoro@230 110 {
giuliomoro@324 111 int num;
giuliomoro@325 112 #ifdef PARSE_MIDI
giuliomoro@324 113 while((num = midi.getParser()->numAvailableMessages()) > 0){
giuliomoro@324 114 static MidiChannelMessage message;
giuliomoro@324 115 message = midi.getParser()->getNextChannelMessage();
giuliomoro@325 116 // message.prettyPrint(); // use this to print beautified message (channel, data bytes)
giuliomoro@324 117 switch(message.getType()){
giuliomoro@325 118 case kmmNoteOn:
giuliomoro@325 119 {
giuliomoro@324 120 int noteNumber = message.getDataByte(0);
giuliomoro@324 121 int velocity = message.getDataByte(1);
giuliomoro@324 122 int channel = message.getChannel();
giuliomoro@324 123 libpd_noteon(channel, noteNumber, velocity);
giuliomoro@325 124 break;
giuliomoro@324 125 }
giuliomoro@325 126 case kmmNoteOff:
giuliomoro@325 127 {
giuliomoro@325 128 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
giuliomoro@325 129 * so that the noteoff velocity is ignored. Here we convert them to noteon
giuliomoro@325 130 * with a velocity of 0.
giuliomoro@325 131 */
giuliomoro@325 132 int noteNumber = message.getDataByte(0);
giuliomoro@325 133 // int velocity = message.getDataByte(1); // would be ignored by Pd
giuliomoro@325 134 int channel = message.getChannel();
giuliomoro@325 135 libpd_noteon(channel, noteNumber, 0);
giuliomoro@325 136 break;
giuliomoro@324 137 }
giuliomoro@325 138 case kmmControlChange:
giuliomoro@325 139 {
giuliomoro@325 140 int channel = message.getChannel();
giuliomoro@325 141 int controller = message.getDataByte(0);
giuliomoro@325 142 int value = message.getDataByte(1);
giuliomoro@325 143 libpd_controlchange(channel, controller, value);
giuliomoro@325 144 break;
giuliomoro@325 145 }
giuliomoro@325 146 case kmmProgramChange:
giuliomoro@325 147 {
giuliomoro@325 148 int channel = message.getChannel();
giuliomoro@325 149 int program = message.getDataByte(0);
giuliomoro@325 150 libpd_programchange(channel, program);
giuliomoro@325 151 break;
giuliomoro@325 152 }
giuliomoro@325 153 case kmmPolyphonicKeyPressure:
giuliomoro@325 154 {
giuliomoro@325 155 int channel = message.getChannel();
giuliomoro@325 156 int pitch = message.getDataByte(0);
giuliomoro@325 157 int value = message.getDataByte(1);
giuliomoro@325 158 libpd_polyaftertouch(channel, pitch, value);
giuliomoro@325 159 break;
giuliomoro@325 160 }
giuliomoro@325 161 case kmmChannelPressure:
giuliomoro@325 162 {
giuliomoro@325 163 int channel = message.getChannel();
giuliomoro@325 164 int value = message.getDataByte(0);
giuliomoro@325 165 libpd_aftertouch(channel, value);
giuliomoro@325 166 break;
giuliomoro@325 167 }
giuliomoro@325 168 case kmmPitchBend:
giuliomoro@325 169 {
giuliomoro@325 170 int channel = message.getChannel();
giuliomoro@325 171 int value = (message.getDataByte(1) << 7)| message.getDataByte(0);
giuliomoro@325 172 libpd_pitchbend(channel, value);
giuliomoro@325 173 break;
giuliomoro@325 174 }
giuliomoro@337 175 case kmmNone:
giuliomoro@337 176 case kmmAny:
giuliomoro@337 177 break;
giuliomoro@324 178 }
giuliomoro@324 179 }
giuliomoro@325 180 #else
giuliomoro@325 181 int input;
giuliomoro@325 182 while((input = midi.getInput()) >= 0){
giuliomoro@325 183 libpd_midibyte(0, input);
giuliomoro@325 184 rt_printf("input: %d\n", input);
giuliomoro@325 185 }
giuliomoro@325 186 #endif /* PARSE_MIDI */
giuliomoro@337 187 static unsigned int inW = 0;
giuliomoro@337 188 static unsigned int outR = 0;
giuliomoro@230 189 /*
giuliomoro@230 190 * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers
giuliomoro@337 191 * and the blocksize of Bela is the same as gLibPdBlockSize, then you probably
giuliomoro@230 192 * do not need the for loops before and after libpd_process_float, so you can save quite some
giuliomoro@230 193 * memory operations.
giuliomoro@230 194 */
giuliomoro@337 195 static unsigned int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels);
giuliomoro@232 196 // rt_printf("channelsInUse: %d, analogChannels in Use: %d\n", gChannelsInUse, analogChannelsInUse);
giuliomoro@232 197 for(unsigned int n = 0; n < context->audioFrames; ++n){ //pd buffers are interleaved
giuliomoro@232 198 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){ //first two channels are audio
andrewm@308 199 gInBuf[inW++] = audioRead(context, n, ch);
giuliomoro@230 200 }
giuliomoro@232 201 // then analogs
giuliomoro@232 202 // this loop resamples by ZOH, as needed, using m
giuliomoro@232 203 if(context->analogChannels == 8 ){ //hold the value for two frames
giuliomoro@337 204 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
andrewm@308 205 gInBuf[inW++] = analogRead(context, n/2, analogCh); // n/2 wil be the same for n and n+1 when n is even
giuliomoro@232 206 }
giuliomoro@232 207 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@337 208 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
andrewm@308 209 gInBuf[inW++] = analogRead(context, n, analogCh);
giuliomoro@232 210 }
giuliomoro@232 211 } else if(context->analogChannels == 2){ //drop every other frame
giuliomoro@337 212 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
andrewm@308 213 gInBuf[inW++] = analogRead(context, n*2, analogCh);
giuliomoro@232 214 }
giuliomoro@230 215 }
giuliomoro@230 216 if(inW == gBufLength * gChannelsInUse){
giuliomoro@230 217 inW = 0;
giuliomoro@230 218 }
giuliomoro@230 219 }
giuliomoro@232 220 // rt_printf("inW %d\n", inW);
giuliomoro@230 221 if(inW == 0){ //if the buffer is full, process it
giuliomoro@337 222 static int numberOfPdBlocksToProcess = gBufLength/gLibPdBlockSize;
giuliomoro@230 223 libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf);
giuliomoro@232 224 outR = 0; // reset the read pointer. NOTE: hopefully this is needed only the first time
giuliomoro@230 225 }
giuliomoro@230 226
giuliomoro@230 227 for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved
giuliomoro@232 228 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){
andrewm@308 229 audioWrite(context, n, ch, gOutBuf[outR++]);
giuliomoro@230 230 }
giuliomoro@232 231 //and analogs
giuliomoro@232 232 if(context->analogChannels == 8){
giuliomoro@232 233 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
giuliomoro@232 234 float analogOut = gOutBuf[outR++];
giuliomoro@232 235 if((n&1) == 0){//write every two frames
andrewm@308 236 analogWrite(context, n/2, analogCh, analogOut);
giuliomoro@232 237 } else {
giuliomoro@232 238 // discard this sample
giuliomoro@232 239 }
giuliomoro@232 240 }
giuliomoro@232 241 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@337 242 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
giuliomoro@232 243 float analogOut = gOutBuf[outR++];
andrewm@308 244 analogWrite(context, n, analogCh, analogOut);
giuliomoro@232 245 }
giuliomoro@232 246 } else if(context->analogChannels == 2){ //write twice every frame
giuliomoro@232 247 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
giuliomoro@232 248 float analogOut = gOutBuf[outR++];
andrewm@308 249 analogWrite(context, 2*n, analogCh, analogOut);
andrewm@308 250 analogWrite(context, 2*n + 1, analogCh, analogOut);
giuliomoro@232 251 }
giuliomoro@230 252 }
giuliomoro@230 253 if(outR == gBufLength * gChannelsInUse){
giuliomoro@230 254 outR = 0;
giuliomoro@230 255 }
giuliomoro@230 256 }
giuliomoro@232 257 // rt_printf("outR %d, analogChannelsInUse %d, channelsInUse %d\n",
giuliomoro@232 258 // outR , analogChannelsInUse, gChannelsInUse);
giuliomoro@230 259 }
giuliomoro@232 260
giuliomoro@230 261 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@230 262 // Release any resources that were allocated in setup().
giuliomoro@230 263
giuliomoro@301 264 void cleanup(BelaContext *context, void *userData)
giuliomoro@230 265 {
giuliomoro@230 266 free(gInBuf);
giuliomoro@230 267 free(gOutBuf);
giuliomoro@230 268 }