giuliomoro@230: /* giuliomoro@230: * render.cpp giuliomoro@230: * giuliomoro@230: * Created on: Oct 24, 2014 giuliomoro@230: * Author: parallels giuliomoro@230: */ giuliomoro@230: giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include "z_libpd.h" giuliomoro@230: #include giuliomoro@230: giuliomoro@230: // setup() is called once before the audio rendering starts. giuliomoro@230: // Use it to perform any initialisation and allocation which is dependent giuliomoro@230: // on the period size or sample rate. giuliomoro@230: // giuliomoro@230: // userData holds an opaque pointer to a data structure that was passed giuliomoro@230: // in from the call to initAudio(). giuliomoro@230: // giuliomoro@230: // Return true on success; returning false halts the program. giuliomoro@230: #define DEFDACBLKSIZE 8u //make sure this matches the one used to compile libpd giuliomoro@230: giuliomoro@230: const int gChannelsInUse = 6; giuliomoro@230: int gBufLength; giuliomoro@230: giuliomoro@230: float* gInBuf; giuliomoro@230: float* gOutBuf; giuliomoro@230: giuliomoro@230: void pdnoteon(int ch, int pitch, int vel) { giuliomoro@230: printf("noteon: %d %d %d\n", ch, pitch, vel); giuliomoro@230: } giuliomoro@230: giuliomoro@230: void BeagleRT_printHook(const char *recv){ giuliomoro@230: rt_printf("%s", recv); giuliomoro@230: } giuliomoro@230: giuliomoro@230: UdpServer udpServer; giuliomoro@230: giuliomoro@230: void udpRead(){ giuliomoro@230: char dest[100] = {0}; giuliomoro@230: while(!gShouldStop){ giuliomoro@230: libpd_sys_microsleep(0); giuliomoro@230: usleep(1000); giuliomoro@230: } giuliomoro@230: } giuliomoro@230: giuliomoro@230: AuxiliaryTask udpReadTask; giuliomoro@230: bool setup(BeagleRTContext *context, void *userData) giuliomoro@230: { giuliomoro@230: udpServer.bindToPort(1234); giuliomoro@230: giuliomoro@230: // check that we are not running with a blocksize smaller than DEFDACBLKSIZE giuliomoro@230: // it would still work, but the load would be executed unevenly between calls to render giuliomoro@230: if(context->audioFrames < DEFDACBLKSIZE){ giuliomoro@230: fprintf(stderr, "Error: minimum block size must be %d\n", DEFDACBLKSIZE); giuliomoro@230: return false; giuliomoro@230: } giuliomoro@230: giuliomoro@230: // check that the sampling rate of the analogs is the same as audio if running with giuliomoro@230: // more than 2 channels (that is with analog). If we fix the TODO in render, then giuliomoro@230: // this test is not needed. giuliomoro@230: if(context->analogFrames != context->audioFrames){ giuliomoro@230: fprintf(stderr, "Error: analog and audio sampling rates must be the same\n"); giuliomoro@230: return false; giuliomoro@230: } giuliomoro@230: //following lines borrowed from libpd/samples/c/pdtest/pdtest.c giuliomoro@230: // init pd giuliomoro@230: libpd_set_printhook(BeagleRT_printHook); // set this before calling libpd_init giuliomoro@230: libpd_set_noteonhook(pdnoteon); giuliomoro@230: libpd_init(); giuliomoro@230: //TODO: analyse the ASCII of the patch file and find the in/outs to use giuliomoro@230: libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate); giuliomoro@230: giuliomoro@230: libpd_start_message(1); // one entry in list giuliomoro@230: libpd_add_float(1.0f); giuliomoro@230: libpd_finish_message("pd", "dsp"); giuliomoro@230: giuliomoro@230: gBufLength = max(DEFDACBLKSIZE, context->audioFrames); giuliomoro@230: unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength; giuliomoro@230: gInBuf = (float*)malloc(bufferSize); giuliomoro@230: gOutBuf = (float*)malloc(bufferSize); giuliomoro@230: // no need to memset to zero giuliomoro@230: giuliomoro@230: giuliomoro@230: char file[] = "_main.pd"; giuliomoro@230: char folder[] = "./"; giuliomoro@230: // open patch [; pd open file folder( giuliomoro@230: libpd_openfile(file, folder); giuliomoro@230: giuliomoro@230: udpReadTask = BeagleRT_createAuxiliaryTask(udpRead, 60, "udpReadTask"); giuliomoro@230: BeagleRT_scheduleAuxiliaryTask(udpReadTask); giuliomoro@230: return true; giuliomoro@230: } giuliomoro@230: giuliomoro@230: // render() is called regularly at the highest priority by the audio engine. giuliomoro@230: // Input and output are given from the audio hardware and the other giuliomoro@230: // ADCs and DACs (if available). If only audio is available, numMatrixFrames giuliomoro@230: // will be 0. giuliomoro@230: BeagleRTContext *c; giuliomoro@230: void render(BeagleRTContext *context, void *userData) giuliomoro@230: { giuliomoro@230: static int inW = 0; giuliomoro@230: static int outR = 0; giuliomoro@230: /* giuliomoro@230: * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers giuliomoro@230: * and the blocksize of Bela is the same as DEFDACBLKSIZE, then you probably giuliomoro@230: * do not need the for loops before and after libpd_process_float, so you can save quite some giuliomoro@230: * memory operations. giuliomoro@230: */ giuliomoro@230: giuliomoro@230: for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved giuliomoro@230: for(unsigned int k = 0; k < context->audioChannels; k++){ giuliomoro@230: gInBuf[inW++] = audioReadFrame(context, n, k); giuliomoro@230: } giuliomoro@230: for(unsigned int k = 0; k < gChannelsInUse - context->audioChannels; k ++){ // add analogs giuliomoro@230: gInBuf[inW++] = analogReadFrame(context, n, k); giuliomoro@230: // TODO: Apply here sampling rate conversion from analogs to audio giuliomoro@230: } giuliomoro@230: if(inW == gBufLength * gChannelsInUse){ giuliomoro@230: inW = 0; giuliomoro@230: } giuliomoro@230: } giuliomoro@230: giuliomoro@230: if(inW == 0){ //if the buffer is full, process it giuliomoro@230: int numberOfPdBlocksToProcess = gBufLength/DEFDACBLKSIZE; giuliomoro@230: libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf); giuliomoro@230: outR = 0; // reset the read pointer. NOTE: hopefully this is not needed EXCEPT the first time giuliomoro@230: } giuliomoro@230: giuliomoro@230: for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved giuliomoro@230: for(unsigned int k = 0; k < context->audioChannels; k++){ giuliomoro@230: audioWriteFrame(context, n, k, gOutBuf[outR++]); giuliomoro@230: } giuliomoro@230: //add analogs here, limit them to channelsInUse giuliomoro@230: for(unsigned int k = 0; k < gChannelsInUse - context->audioChannels; k ++){ // add analogs giuliomoro@230: analogWriteFrame(context, n, k, gOutBuf[outR++]); giuliomoro@230: // TODO: Apply here sampling rate conversion from analogs to audio giuliomoro@230: } giuliomoro@230: if(outR == gBufLength * gChannelsInUse){ giuliomoro@230: outR = 0; giuliomoro@230: } giuliomoro@230: } giuliomoro@230: } giuliomoro@230: // cleanup() is called once at the end, after the audio has stopped. giuliomoro@230: // Release any resources that were allocated in setup(). giuliomoro@230: giuliomoro@230: void cleanup(BeagleRTContext *context, void *userData) giuliomoro@230: { giuliomoro@230: free(gInBuf); giuliomoro@230: free(gOutBuf); giuliomoro@230: }