giuliomoro@230: /* giuliomoro@230: * render.cpp giuliomoro@230: * giuliomoro@230: * Created on: Oct 24, 2014 giuliomoro@230: * Author: parallels giuliomoro@230: */ giuliomoro@230: giuliomoro@301: #include giuliomoro@345: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include "z_libpd.h" giuliomoro@338: #include "z_queued.h" giuliomoro@342: #include "s_stuff.h" giuliomoro@230: #include giuliomoro@324: #include giuliomoro@345: //extern t_sample* sys_soundin; giuliomoro@345: //extern t_sample* sys_soundout; giuliomoro@345: // if you are 100% sure of what value was used to compile libpd/puredata, then giuliomoro@345: // you could #define this instead of getting it at runtime. It has proved to give some 0.3% giuliomoro@343: // performance boost when it is 8 (thanks to vectorize optimizations I guess). 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@301: void Bela_printHook(const char *recv){ giuliomoro@230: rt_printf("%s", recv); giuliomoro@230: } giuliomoro@230: giuliomoro@340: void libpdReadFilesLoop(){ giuliomoro@230: while(!gShouldStop){ giuliomoro@337: // check for modified sockets/file descriptors giuliomoro@337: // (libpd would normally do this every block WITHIN the audio thread) giuliomoro@337: // not sure if this is thread-safe at the moment giuliomoro@230: libpd_sys_microsleep(0); giuliomoro@230: usleep(1000); giuliomoro@230: } giuliomoro@230: } giuliomoro@340: giuliomoro@325: #define PARSE_MIDI giuliomoro@340: AuxiliaryTask libpdReadFilesTask; giuliomoro@340: AuxiliaryTask libpdProcessMessageQueueTask; giuliomoro@340: AuxiliaryTask libpdProcessMidiQueueTask; giuliomoro@324: Midi midi; giuliomoro@342: //UdpServer udpServer; giuliomoro@337: giuliomoro@345: void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){ giuliomoro@345: libpd_float((char*)receiverName, (float)state); giuliomoro@345: // rt_printf("%s: %d\n", (char*)receiverName, state); giuliomoro@345: } giuliomoro@345: char receiverNames[16][21]={ giuliomoro@345: {"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"}, giuliomoro@345: {"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"}, giuliomoro@345: {"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"}, giuliomoro@345: {"bela_digitalIn26"} giuliomoro@345: }; giuliomoro@345: giuliomoro@345: static DigitalToMessage** dtm; giuliomoro@345: static unsigned int analogChannelsInUse; giuliomoro@345: static unsigned int gLibpdBlockSize; giuliomoro@345: static unsigned int gChannelsInUse = 26; giuliomoro@345: giuliomoro@301: bool setup(BelaContext *context, void *userData) giuliomoro@230: { giuliomoro@345: analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels - context->digitalChannels); giuliomoro@345: dtm = new DigitalToMessage* [context->digitalChannels]; giuliomoro@345: if(context->digitalChannels > 0){ giuliomoro@345: for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){ giuliomoro@345: dtm[ch] = new DigitalToMessage; giuliomoro@345: dtm[ch]->setCallback(sendDigitalMessage, receiverNames[ch]); giuliomoro@345: pinMode(context, 0, ch, OUTPUT); giuliomoro@345: } giuliomoro@345: } giuliomoro@324: midi.readFrom(0); giuliomoro@324: midi.writeTo(0); giuliomoro@325: #ifdef PARSE_MIDI giuliomoro@324: midi.enableParser(true); giuliomoro@325: #else giuliomoro@325: midi.enableParser(false); giuliomoro@325: #endif /* PARSE_MIDI */ giuliomoro@345: // gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse); giuliomoro@342: // udpServer.bindToPort(1234); giuliomoro@230: giuliomoro@340: gLibpdBlockSize = libpd_blocksize(); giuliomoro@337: // check that we are not running with a blocksize smaller than gLibPdBlockSize giuliomoro@230: // it would still work, but the load would be executed unevenly between calls to render giuliomoro@340: if(context->audioFrames < gLibpdBlockSize){ giuliomoro@340: fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize); giuliomoro@230: return false; giuliomoro@230: } giuliomoro@230: // init pd giuliomoro@338: libpd_set_queued_printhook(Bela_printHook); // set this before calling libpd_init giuliomoro@338: libpd_set_queued_noteonhook(pdnoteon); giuliomoro@340: //TODO: add hooks for other midi events and generate MIDI output appropriately giuliomoro@338: libpd_queued_init(); giuliomoro@340: //TODO: ideally, we would 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@340: gBufLength = max(gLibpdBlockSize, context->audioFrames); 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@342: gInBuf = libpd_get_sys_soundin(); giuliomoro@342: gOutBuf = libpd_get_sys_soundout(); giuliomoro@340: libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles"); giuliomoro@340: Bela_scheduleAuxiliaryTask(libpdReadFilesTask); giuliomoro@340: giuliomoro@340: // Higher priority for the midi queue and lower priority for the message queue. Adjust to taste giuliomoro@340: libpdProcessMidiQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_midi_messages, 80, "libpdProcessMidiQueue"); giuliomoro@340: libpdProcessMessageQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_pd_messages, 70, "libpdProcessMessageQueue"); 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@345: giuliomoro@301: void render(BelaContext *context, void *userData) giuliomoro@230: { giuliomoro@324: int num; giuliomoro@341: // the safest thread-safe option to handle MIDI input is to process the MIDI buffer giuliomoro@341: // from the audio thread. giuliomoro@325: #ifdef PARSE_MIDI giuliomoro@324: while((num = midi.getParser()->numAvailableMessages()) > 0){ giuliomoro@324: static MidiChannelMessage message; giuliomoro@324: message = midi.getParser()->getNextChannelMessage(); giuliomoro@341: //message.prettyPrint(); // use this to print beautified message (channel, data bytes) giuliomoro@324: switch(message.getType()){ giuliomoro@325: case kmmNoteOn: giuliomoro@325: { giuliomoro@324: int noteNumber = message.getDataByte(0); giuliomoro@324: int velocity = message.getDataByte(1); giuliomoro@324: int channel = message.getChannel(); giuliomoro@324: libpd_noteon(channel, noteNumber, velocity); giuliomoro@325: break; giuliomoro@324: } giuliomoro@325: case kmmNoteOff: giuliomoro@325: { giuliomoro@325: /* PureData does not seem to handle noteoff messages as per the MIDI specs, giuliomoro@325: * so that the noteoff velocity is ignored. Here we convert them to noteon giuliomoro@325: * with a velocity of 0. giuliomoro@325: */ giuliomoro@325: int noteNumber = message.getDataByte(0); giuliomoro@325: // int velocity = message.getDataByte(1); // would be ignored by Pd giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: libpd_noteon(channel, noteNumber, 0); giuliomoro@325: break; giuliomoro@324: } giuliomoro@325: case kmmControlChange: giuliomoro@325: { giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: int controller = message.getDataByte(0); giuliomoro@325: int value = message.getDataByte(1); giuliomoro@325: libpd_controlchange(channel, controller, value); giuliomoro@325: break; giuliomoro@325: } giuliomoro@325: case kmmProgramChange: giuliomoro@325: { giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: int program = message.getDataByte(0); giuliomoro@325: libpd_programchange(channel, program); giuliomoro@325: break; giuliomoro@325: } giuliomoro@325: case kmmPolyphonicKeyPressure: giuliomoro@325: { giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: int pitch = message.getDataByte(0); giuliomoro@325: int value = message.getDataByte(1); giuliomoro@325: libpd_polyaftertouch(channel, pitch, value); giuliomoro@325: break; giuliomoro@325: } giuliomoro@325: case kmmChannelPressure: giuliomoro@325: { giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: int value = message.getDataByte(0); giuliomoro@325: libpd_aftertouch(channel, value); giuliomoro@325: break; giuliomoro@325: } giuliomoro@325: case kmmPitchBend: giuliomoro@325: { giuliomoro@325: int channel = message.getChannel(); giuliomoro@325: int value = (message.getDataByte(1) << 7)| message.getDataByte(0); giuliomoro@325: libpd_pitchbend(channel, value); giuliomoro@325: break; giuliomoro@325: } giuliomoro@337: case kmmNone: giuliomoro@337: case kmmAny: giuliomoro@337: break; giuliomoro@324: } giuliomoro@324: } giuliomoro@325: #else giuliomoro@325: int input; giuliomoro@325: while((input = midi.getInput()) >= 0){ giuliomoro@325: libpd_midibyte(0, input); giuliomoro@325: } giuliomoro@325: #endif /* PARSE_MIDI */ giuliomoro@340: giuliomoro@230: /* giuliomoro@230: * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers giuliomoro@337: * and the blocksize of Bela is the same as gLibPdBlockSize, 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@341: static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize; giuliomoro@343: for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){ giuliomoro@345: unsigned int audioFrameBase = gLibpdBlockSize * tick; giuliomoro@343: unsigned int j; giuliomoro@343: unsigned int k; giuliomoro@343: float* p0; giuliomoro@343: float* p1; giuliomoro@343: for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) { giuliomoro@345: *p1 = audioRead(context, audioFrameBase + j, k); giuliomoro@232: } giuliomoro@343: } giuliomoro@343: // then analogs giuliomoro@343: // this loop resamples by ZOH, as needed, using m giuliomoro@343: if(context->analogChannels == 8 ){ //hold the value for two frames giuliomoro@343: for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: unsigned int analogFrame = (audioFrameBase + j) / 2; giuliomoro@343: *p1 = analogRead(context, analogFrame, k); giuliomoro@341: } giuliomoro@343: } giuliomoro@343: } else if(context->analogChannels == 4){ //write every frame giuliomoro@343: for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: unsigned int analogFrame = audioFrameBase + j; giuliomoro@343: *p1 = analogRead(context, analogFrame, k); giuliomoro@341: } giuliomoro@343: } giuliomoro@343: } else if(context->analogChannels == 2){ //drop every other frame giuliomoro@343: for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: unsigned int analogFrame = (audioFrameBase + j) * 2; giuliomoro@343: *p1 = analogRead(context, analogFrame, k); giuliomoro@341: } giuliomoro@232: } giuliomoro@230: } giuliomoro@345: giuliomoro@345: //then digital giuliomoro@345: //TODO: in multiple places we assume that the number of digitals is same as number of audio giuliomoro@345: for(unsigned int n = 0; n < context->digitalChannels; ++n){ giuliomoro@345: // TODO: note that we consider only the first sample of the block giuliomoro@345: // considering all of them is notably more expensive giuliomoro@345: // TODO: only process the channels marked as such giuliomoro@345: dtm[n]->process(n + 16, &context->digital[audioFrameBase], 1); giuliomoro@345: } giuliomoro@345: giuliomoro@342: libpd_process_sys(); // process the block giuliomoro@345: giuliomoro@345: //digital giuliomoro@345: for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) { giuliomoro@345: unsigned int digitalFrame = (audioFrameBase + j); giuliomoro@345: for (k = 0, p1 = p0 + gLibpdBlockSize * (context->audioChannels + 8); giuliomoro@345: k < context->digitalChannels; k++, p1 += gLibpdBlockSize) { giuliomoro@345: // TODO: only process the channels marked as such giuliomoro@345: digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5); giuliomoro@345: } giuliomoro@345: } giuliomoro@345: //audio giuliomoro@343: for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) { giuliomoro@345: audioWrite(context, audioFrameBase + j, k, *p1); giuliomoro@341: } giuliomoro@343: } giuliomoro@345: //analog giuliomoro@343: if(context->analogChannels == 8){ giuliomoro@343: for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames giuliomoro@345: unsigned int analogFrame = (audioFrameBase + j) / 2; giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: analogWriteOnce(context, analogFrame, k, *p1); giuliomoro@341: } giuliomoro@343: } giuliomoro@343: } else if(context->analogChannels == 4){ //write every frame giuliomoro@343: for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) { giuliomoro@345: unsigned int analogFrame = (audioFrameBase + j); giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: analogWriteOnce(context, analogFrame, k, *p1); giuliomoro@341: } giuliomoro@343: } giuliomoro@343: } else if(context->analogChannels == 2){ //write every frame twice giuliomoro@343: for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@343: for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < analogChannelsInUse; k++, p1 += gLibpdBlockSize) { giuliomoro@345: int analogFrame = audioFrameBase * 2 + j * 2; giuliomoro@345: analogWriteOnce(context, analogFrame, k, *p1); giuliomoro@345: analogWriteOnce(context, analogFrame + 1, k, *p1); giuliomoro@232: } giuliomoro@232: } giuliomoro@230: } giuliomoro@230: } giuliomoro@340: Bela_scheduleAuxiliaryTask(libpdProcessMidiQueueTask); giuliomoro@340: Bela_scheduleAuxiliaryTask(libpdProcessMessageQueueTask); giuliomoro@230: } giuliomoro@232: 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@301: void cleanup(BelaContext *context, void *userData) giuliomoro@230: { giuliomoro@338: libpd_queued_release(); giuliomoro@345: delete[] dtm; giuliomoro@230: }