Mercurial > hg > beaglert
view 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 |
line wrap: on
line source
/* * render.cpp * * Created on: Oct 24, 2014 * Author: parallels */ #include <Bela.h> #include <cmath> #include <Utilities.h> #include <I2c_Codec.h> #include <PRU.h> #include <stdio.h> #include "z_libpd.h" #include "z_queued.h" #include "s_stuff.h" #include <UdpServer.h> #include <Midi.h> // if you are 100% sure of what value that was used to compile libpd/puredata, then // you could define this, instead of getting it at runtime. It has proved to give some 0.3% // performance boost when it is 8 (thanks to vectorize optimizations I guess). unsigned int gLibpdBlockSize; unsigned int gChannelsInUse = 10; int gBufLength; float* gInBuf; float* gOutBuf; void pdnoteon(int ch, int pitch, int vel) { printf("noteon: %d %d %d\n", ch, pitch, vel); } void Bela_printHook(const char *recv){ rt_printf("%s", recv); } void libpdReadFilesLoop(){ while(!gShouldStop){ // check for modified sockets/file descriptors // (libpd would normally do this every block WITHIN the audio thread) // not sure if this is thread-safe at the moment libpd_sys_microsleep(0); usleep(1000); } } #define PARSE_MIDI AuxiliaryTask libpdReadFilesTask; AuxiliaryTask libpdProcessMessageQueueTask; AuxiliaryTask libpdProcessMidiQueueTask; Midi midi; //UdpServer udpServer; bool setup(BelaContext *context, void *userData) { midi.readFrom(0); midi.writeTo(0); #ifdef PARSE_MIDI midi.enableParser(true); #else midi.enableParser(false); #endif /* PARSE_MIDI */ gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse); // udpServer.bindToPort(1234); gLibpdBlockSize = libpd_blocksize(); // check that we are not running with a blocksize smaller than gLibPdBlockSize // it would still work, but the load would be executed unevenly between calls to render if(context->audioFrames < gLibpdBlockSize){ fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize); return false; } // init pd libpd_set_queued_printhook(Bela_printHook); // set this before calling libpd_init libpd_set_queued_noteonhook(pdnoteon); //TODO: add hooks for other midi events and generate MIDI output appropriately libpd_queued_init(); //TODO: ideally, we would analyse the ASCII of the patch file and find the in/outs to use libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate); libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); gBufLength = max(gLibpdBlockSize, context->audioFrames); char file[] = "_main.pd"; char folder[] = "./"; // open patch [; pd open file folder( libpd_openfile(file, folder); gInBuf = libpd_get_sys_soundin(); gOutBuf = libpd_get_sys_soundout(); libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles"); Bela_scheduleAuxiliaryTask(libpdReadFilesTask); // Higher priority for the midi queue and lower priority for the message queue. Adjust to taste libpdProcessMidiQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_midi_messages, 80, "libpdProcessMidiQueue"); libpdProcessMessageQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_pd_messages, 70, "libpdProcessMessageQueue"); return true; } // render() is called regularly at the highest priority by the audio engine. // Input and output are given from the audio hardware and the other // ADCs and DACs (if available). If only audio is available, numMatrixFrames // will be 0. BelaContext *c; void render(BelaContext *context, void *userData) { int num; // the safest thread-safe option to handle MIDI input is to process the MIDI buffer // from the audio thread. #ifdef PARSE_MIDI while((num = midi.getParser()->numAvailableMessages()) > 0){ static MidiChannelMessage message; message = midi.getParser()->getNextChannelMessage(); //message.prettyPrint(); // use this to print beautified message (channel, data bytes) switch(message.getType()){ case kmmNoteOn: { int noteNumber = message.getDataByte(0); int velocity = message.getDataByte(1); int channel = message.getChannel(); libpd_noteon(channel, noteNumber, velocity); break; } case kmmNoteOff: { /* PureData does not seem to handle noteoff messages as per the MIDI specs, * so that the noteoff velocity is ignored. Here we convert them to noteon * with a velocity of 0. */ int noteNumber = message.getDataByte(0); // int velocity = message.getDataByte(1); // would be ignored by Pd int channel = message.getChannel(); libpd_noteon(channel, noteNumber, 0); break; } case kmmControlChange: { int channel = message.getChannel(); int controller = message.getDataByte(0); int value = message.getDataByte(1); libpd_controlchange(channel, controller, value); break; } case kmmProgramChange: { int channel = message.getChannel(); int program = message.getDataByte(0); libpd_programchange(channel, program); break; } case kmmPolyphonicKeyPressure: { int channel = message.getChannel(); int pitch = message.getDataByte(0); int value = message.getDataByte(1); libpd_polyaftertouch(channel, pitch, value); break; } case kmmChannelPressure: { int channel = message.getChannel(); int value = message.getDataByte(0); libpd_aftertouch(channel, value); break; } case kmmPitchBend: { int channel = message.getChannel(); int value = (message.getDataByte(1) << 7)| message.getDataByte(0); libpd_pitchbend(channel, value); break; } case kmmNone: case kmmAny: break; } } #else int input; while((input = midi.getInput()) >= 0){ libpd_midibyte(0, input); } #endif /* PARSE_MIDI */ /* * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers * and the blocksize of Bela is the same as gLibPdBlockSize, then you probably * do not need the for loops before and after libpd_process_float, so you can save quite some * memory operations. */ static unsigned int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels); static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize; for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){ unsigned int audioOutFrameBase = gLibpdBlockSize * tick; unsigned int j; unsigned int k; float* p0; float* p1; for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) { *p1 = audioRead(context, audioOutFrameBase + j, k); } } // then analogs // this loop resamples by ZOH, as needed, using m if(context->analogChannels == 8 ){ //hold the value for two frames for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { unsigned int analogFrame = (audioOutFrameBase + j) / 2; *p1 = analogRead(context, analogFrame, k); } } } else if(context->analogChannels == 4){ //write every frame for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { unsigned int analogFrame = audioOutFrameBase + j; *p1 = analogRead(context, analogFrame, k); } } } else if(context->analogChannels == 2){ //drop every other frame for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { unsigned int analogFrame = (audioOutFrameBase + j) * 2; *p1 = analogRead(context, analogFrame, k); } } } libpd_process_sys(); // process the block for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) { audioWrite(context, audioOutFrameBase + j, k, *p1); } } if(context->analogChannels == 8){ for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames unsigned int analogFrame = (audioOutFrameBase + j) / 2; for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { analogWrite(context, analogFrame, k, *p1); } } } else if(context->analogChannels == 4){ //write every frame for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) { unsigned int analogFrame = (audioOutFrameBase + j); for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { analogWrite(context, analogFrame, k, *p1); } } } else if(context->analogChannels == 2){ //write every frame twice for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) { for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { int analogFrame = audioOutFrameBase * 2 + j * 2; analogWrite(context, analogFrame, k, *p1); analogWrite(context, analogFrame + 1, k, *p1); } } } } Bela_scheduleAuxiliaryTask(libpdProcessMidiQueueTask); Bela_scheduleAuxiliaryTask(libpdProcessMessageQueueTask); } // cleanup() is called once at the end, after the audio has stopped. // Release any resources that were allocated in setup(). void cleanup(BelaContext *context, void *userData) { libpd_queued_release(); }