annotate examples/basic_libpd/render.cpp @ 343:4823ee13bcac prerelease

basic_libpd now populates pd's audio buffers directly. Updated libpd.so binary. This required an API change with respect to standard libpdAPI because for some strange reason, accessing sys_audioin and sys_audioout directly from the render.cpp file would return invalid values.
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 06 Jun 2016 15:15:15 +0100
parents 860c42b3830e
children 0e1e0dfe24c5
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@338 15 #include "z_queued.h"
giuliomoro@342 16 #include "s_stuff.h"
giuliomoro@230 17 #include <UdpServer.h>
giuliomoro@324 18 #include <Midi.h>
giuliomoro@324 19
giuliomoro@343 20 // if you are 100% sure of what value that was used to compile libpd/puredata, then
giuliomoro@343 21 // you could define this, 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@343 23 unsigned int gLibpdBlockSize;
giuliomoro@230 24
giuliomoro@337 25 unsigned int gChannelsInUse = 10;
giuliomoro@230 26 int gBufLength;
giuliomoro@230 27
giuliomoro@230 28 float* gInBuf;
giuliomoro@230 29 float* gOutBuf;
giuliomoro@230 30
giuliomoro@230 31 void pdnoteon(int ch, int pitch, int vel) {
giuliomoro@230 32 printf("noteon: %d %d %d\n", ch, pitch, vel);
giuliomoro@230 33 }
giuliomoro@230 34
giuliomoro@301 35 void Bela_printHook(const char *recv){
giuliomoro@230 36 rt_printf("%s", recv);
giuliomoro@230 37 }
giuliomoro@230 38
giuliomoro@340 39 void libpdReadFilesLoop(){
giuliomoro@230 40 while(!gShouldStop){
giuliomoro@337 41 // check for modified sockets/file descriptors
giuliomoro@337 42 // (libpd would normally do this every block WITHIN the audio thread)
giuliomoro@337 43 // not sure if this is thread-safe at the moment
giuliomoro@230 44 libpd_sys_microsleep(0);
giuliomoro@230 45 usleep(1000);
giuliomoro@230 46 }
giuliomoro@230 47 }
giuliomoro@340 48
giuliomoro@325 49 #define PARSE_MIDI
giuliomoro@340 50 AuxiliaryTask libpdReadFilesTask;
giuliomoro@340 51 AuxiliaryTask libpdProcessMessageQueueTask;
giuliomoro@340 52 AuxiliaryTask libpdProcessMidiQueueTask;
giuliomoro@324 53 Midi midi;
giuliomoro@342 54 //UdpServer udpServer;
giuliomoro@337 55
giuliomoro@301 56 bool setup(BelaContext *context, void *userData)
giuliomoro@230 57 {
giuliomoro@324 58 midi.readFrom(0);
giuliomoro@324 59 midi.writeTo(0);
giuliomoro@325 60 #ifdef PARSE_MIDI
giuliomoro@324 61 midi.enableParser(true);
giuliomoro@325 62 #else
giuliomoro@325 63 midi.enableParser(false);
giuliomoro@325 64 #endif /* PARSE_MIDI */
giuliomoro@232 65 gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
giuliomoro@342 66 // udpServer.bindToPort(1234);
giuliomoro@230 67
giuliomoro@340 68 gLibpdBlockSize = libpd_blocksize();
giuliomoro@337 69 // check that we are not running with a blocksize smaller than gLibPdBlockSize
giuliomoro@230 70 // it would still work, but the load would be executed unevenly between calls to render
giuliomoro@340 71 if(context->audioFrames < gLibpdBlockSize){
giuliomoro@340 72 fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
giuliomoro@230 73 return false;
giuliomoro@230 74 }
giuliomoro@230 75 // init pd
giuliomoro@338 76 libpd_set_queued_printhook(Bela_printHook); // set this before calling libpd_init
giuliomoro@338 77 libpd_set_queued_noteonhook(pdnoteon);
giuliomoro@340 78 //TODO: add hooks for other midi events and generate MIDI output appropriately
giuliomoro@338 79 libpd_queued_init();
giuliomoro@340 80 //TODO: ideally, we would analyse the ASCII of the patch file and find the in/outs to use
giuliomoro@230 81 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
giuliomoro@230 82
giuliomoro@230 83 libpd_start_message(1); // one entry in list
giuliomoro@230 84 libpd_add_float(1.0f);
giuliomoro@230 85 libpd_finish_message("pd", "dsp");
giuliomoro@230 86
giuliomoro@340 87 gBufLength = max(gLibpdBlockSize, context->audioFrames);
giuliomoro@230 88
giuliomoro@230 89 char file[] = "_main.pd";
giuliomoro@230 90 char folder[] = "./";
giuliomoro@230 91 // open patch [; pd open file folder(
giuliomoro@230 92 libpd_openfile(file, folder);
giuliomoro@342 93 gInBuf = libpd_get_sys_soundin();
giuliomoro@342 94 gOutBuf = libpd_get_sys_soundout();
giuliomoro@340 95 libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
giuliomoro@340 96 Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
giuliomoro@340 97
giuliomoro@340 98 // Higher priority for the midi queue and lower priority for the message queue. Adjust to taste
giuliomoro@340 99 libpdProcessMidiQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_midi_messages, 80, "libpdProcessMidiQueue");
giuliomoro@340 100 libpdProcessMessageQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_pd_messages, 70, "libpdProcessMessageQueue");
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@341 112 // the safest thread-safe option to handle MIDI input is to process the MIDI buffer
giuliomoro@341 113 // from the audio thread.
giuliomoro@325 114 #ifdef PARSE_MIDI
giuliomoro@324 115 while((num = midi.getParser()->numAvailableMessages()) > 0){
giuliomoro@324 116 static MidiChannelMessage message;
giuliomoro@324 117 message = midi.getParser()->getNextChannelMessage();
giuliomoro@341 118 //message.prettyPrint(); // use this to print beautified message (channel, data bytes)
giuliomoro@324 119 switch(message.getType()){
giuliomoro@325 120 case kmmNoteOn:
giuliomoro@325 121 {
giuliomoro@324 122 int noteNumber = message.getDataByte(0);
giuliomoro@324 123 int velocity = message.getDataByte(1);
giuliomoro@324 124 int channel = message.getChannel();
giuliomoro@324 125 libpd_noteon(channel, noteNumber, velocity);
giuliomoro@325 126 break;
giuliomoro@324 127 }
giuliomoro@325 128 case kmmNoteOff:
giuliomoro@325 129 {
giuliomoro@325 130 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
giuliomoro@325 131 * so that the noteoff velocity is ignored. Here we convert them to noteon
giuliomoro@325 132 * with a velocity of 0.
giuliomoro@325 133 */
giuliomoro@325 134 int noteNumber = message.getDataByte(0);
giuliomoro@325 135 // int velocity = message.getDataByte(1); // would be ignored by Pd
giuliomoro@325 136 int channel = message.getChannel();
giuliomoro@325 137 libpd_noteon(channel, noteNumber, 0);
giuliomoro@325 138 break;
giuliomoro@324 139 }
giuliomoro@325 140 case kmmControlChange:
giuliomoro@325 141 {
giuliomoro@325 142 int channel = message.getChannel();
giuliomoro@325 143 int controller = message.getDataByte(0);
giuliomoro@325 144 int value = message.getDataByte(1);
giuliomoro@325 145 libpd_controlchange(channel, controller, value);
giuliomoro@325 146 break;
giuliomoro@325 147 }
giuliomoro@325 148 case kmmProgramChange:
giuliomoro@325 149 {
giuliomoro@325 150 int channel = message.getChannel();
giuliomoro@325 151 int program = message.getDataByte(0);
giuliomoro@325 152 libpd_programchange(channel, program);
giuliomoro@325 153 break;
giuliomoro@325 154 }
giuliomoro@325 155 case kmmPolyphonicKeyPressure:
giuliomoro@325 156 {
giuliomoro@325 157 int channel = message.getChannel();
giuliomoro@325 158 int pitch = message.getDataByte(0);
giuliomoro@325 159 int value = message.getDataByte(1);
giuliomoro@325 160 libpd_polyaftertouch(channel, pitch, value);
giuliomoro@325 161 break;
giuliomoro@325 162 }
giuliomoro@325 163 case kmmChannelPressure:
giuliomoro@325 164 {
giuliomoro@325 165 int channel = message.getChannel();
giuliomoro@325 166 int value = message.getDataByte(0);
giuliomoro@325 167 libpd_aftertouch(channel, value);
giuliomoro@325 168 break;
giuliomoro@325 169 }
giuliomoro@325 170 case kmmPitchBend:
giuliomoro@325 171 {
giuliomoro@325 172 int channel = message.getChannel();
giuliomoro@325 173 int value = (message.getDataByte(1) << 7)| message.getDataByte(0);
giuliomoro@325 174 libpd_pitchbend(channel, value);
giuliomoro@325 175 break;
giuliomoro@325 176 }
giuliomoro@337 177 case kmmNone:
giuliomoro@337 178 case kmmAny:
giuliomoro@337 179 break;
giuliomoro@324 180 }
giuliomoro@324 181 }
giuliomoro@325 182 #else
giuliomoro@325 183 int input;
giuliomoro@325 184 while((input = midi.getInput()) >= 0){
giuliomoro@325 185 libpd_midibyte(0, input);
giuliomoro@325 186 }
giuliomoro@325 187 #endif /* PARSE_MIDI */
giuliomoro@340 188
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@341 196 static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize;
giuliomoro@343 197 for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){
giuliomoro@343 198 unsigned int audioOutFrameBase = gLibpdBlockSize * tick;
giuliomoro@343 199 unsigned int j;
giuliomoro@343 200 unsigned int k;
giuliomoro@343 201 float* p0;
giuliomoro@343 202 float* p1;
giuliomoro@343 203 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 204 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 205 *p1 = audioRead(context, audioOutFrameBase + j, k);
giuliomoro@232 206 }
giuliomoro@343 207 }
giuliomoro@343 208 // then analogs
giuliomoro@343 209 // this loop resamples by ZOH, as needed, using m
giuliomoro@343 210 if(context->analogChannels == 8 ){ //hold the value for two frames
giuliomoro@343 211 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 212 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 213 unsigned int analogFrame = (audioOutFrameBase + j) / 2;
giuliomoro@343 214 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 215 }
giuliomoro@343 216 }
giuliomoro@343 217 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@343 218 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 219 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 220 unsigned int analogFrame = audioOutFrameBase + j;
giuliomoro@343 221 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 222 }
giuliomoro@343 223 }
giuliomoro@343 224 } else if(context->analogChannels == 2){ //drop every other frame
giuliomoro@343 225 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 226 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 227 unsigned int analogFrame = (audioOutFrameBase + j) * 2;
giuliomoro@343 228 *p1 = analogRead(context, analogFrame, k);
giuliomoro@341 229 }
giuliomoro@232 230 }
giuliomoro@230 231 }
giuliomoro@342 232 libpd_process_sys(); // process the block
giuliomoro@343 233 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 234 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 235 audioWrite(context, audioOutFrameBase + j, k, *p1);
giuliomoro@341 236 }
giuliomoro@343 237 }
giuliomoro@343 238 if(context->analogChannels == 8){
giuliomoro@343 239 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames
giuliomoro@343 240 unsigned int analogFrame = (audioOutFrameBase + j) / 2;
giuliomoro@343 241 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 242 analogWrite(context, analogFrame, k, *p1);
giuliomoro@341 243 }
giuliomoro@343 244 }
giuliomoro@343 245 } else if(context->analogChannels == 4){ //write every frame
giuliomoro@343 246 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
giuliomoro@343 247 unsigned int analogFrame = (audioOutFrameBase + j);
giuliomoro@343 248 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 249 analogWrite(context, analogFrame, k, *p1);
giuliomoro@341 250 }
giuliomoro@343 251 }
giuliomoro@343 252 } else if(context->analogChannels == 2){ //write every frame twice
giuliomoro@343 253 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
giuliomoro@343 254 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) {
giuliomoro@343 255 int analogFrame = audioOutFrameBase * 2 + j * 2;
giuliomoro@343 256 analogWrite(context, analogFrame, k, *p1);
giuliomoro@343 257 analogWrite(context, analogFrame + 1, k, *p1);
giuliomoro@232 258 }
giuliomoro@232 259 }
giuliomoro@230 260 }
giuliomoro@230 261 }
giuliomoro@340 262 Bela_scheduleAuxiliaryTask(libpdProcessMidiQueueTask);
giuliomoro@340 263 Bela_scheduleAuxiliaryTask(libpdProcessMessageQueueTask);
giuliomoro@230 264 }
giuliomoro@232 265
giuliomoro@230 266 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@230 267 // Release any resources that were allocated in setup().
giuliomoro@230 268
giuliomoro@301 269 void cleanup(BelaContext *context, void *userData)
giuliomoro@230 270 {
giuliomoro@338 271 libpd_queued_release();
giuliomoro@230 272 }