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@353: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@230: #include giuliomoro@467: #include giuliomoro@467: #include 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@353: static AuxiliaryTask libpdReadFilesTask; giuliomoro@353: static Midi midi; giuliomoro@353: static DigitalChannelManager dcm; 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@350: giuliomoro@352: #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs) giuliomoro@352: giuliomoro@352: void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){ giuliomoro@352: if(strcmp(source, "bela_setDigital") == 0){ giuliomoro@352: // symbol is the direction, argv[0] is the channel, argv[1] (optional) giuliomoro@363: // is signal("sig" or "~") or message("message", default) rate giuliomoro@353: bool isMessageRate = true; // defaults to message rate giuliomoro@354: bool direction = 0; // initialize it just to avoid the compiler's warning giuliomoro@354: bool disable = false; giuliomoro@352: if(strcmp(symbol, "in") == 0){ giuliomoro@352: direction = INPUT; giuliomoro@352: } else if(strcmp(symbol, "out") == 0){ giuliomoro@352: direction = OUTPUT; giuliomoro@354: } else if(strcmp(symbol, "disable") == 0){ giuliomoro@354: disable = true; giuliomoro@352: } else { giuliomoro@354: return; giuliomoro@352: } giuliomoro@352: if(argc == 0){ giuliomoro@354: return; giuliomoro@352: } else if (libpd_is_float(&argv[0]) == false){ giuliomoro@352: return; giuliomoro@352: } giuliomoro@352: int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET; giuliomoro@354: if(disable == true){ giuliomoro@354: dcm.unmanage(channel); giuliomoro@355: return; giuliomoro@354: } giuliomoro@352: if(argc >= 2){ giuliomoro@352: t_atom* a = &argv[1]; giuliomoro@352: if(libpd_is_symbol(a)){ giuliomoro@352: char *s = libpd_get_symbol(a); giuliomoro@352: if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){ giuliomoro@353: isMessageRate = false; giuliomoro@352: } giuliomoro@352: } giuliomoro@352: } giuliomoro@354: dcm.manage(channel, direction, isMessageRate); giuliomoro@352: } giuliomoro@352: } giuliomoro@352: giuliomoro@350: void Bela_floatHook(const char *source, float value){ giuliomoro@350: // let's make this as optimized as possible for built-in digital Out parsing giuliomoro@350: // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26 giuliomoro@350: static int prefixLength = 15; // strlen("bela_digitalOut") giuliomoro@350: if(strncmp(source, "bela_digitalOut", prefixLength)==0){ giuliomoro@350: if(source[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2) giuliomoro@350: if(source[prefixLength + 1] != 0){ giuliomoro@357: // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi giuliomoro@350: int receiver = ((source[prefixLength] - 48) * 10); giuliomoro@350: receiver += (source[prefixLength+1] - 48); giuliomoro@350: unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number giuliomoro@350: if(channel >= 0 && channel < 16){ //16 is the hardcoded value for the number of digital channels giuliomoro@354: dcm.setValue(channel, value); giuliomoro@350: } giuliomoro@350: } giuliomoro@350: } giuliomoro@350: } giuliomoro@350: } giuliomoro@350: 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 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@470: // Check first of all if file exists. Will actually open it later. giuliomoro@470: char file[] = "_main.pd"; giuliomoro@470: char folder[] = "./"; giuliomoro@470: unsigned int strSize = strlen(file) + strlen(folder) + 1; giuliomoro@470: char* str = (char*)malloc(sizeof(char) * strSize); giuliomoro@470: snprintf(str, strSize, "%s%s", folder, file); giuliomoro@470: if(access(str, F_OK) == -1 ) { giuliomoro@470: printf("Error file %s/%s not found. The %s file should be your main patch.\n", folder, file, file); giuliomoro@470: return false; giuliomoro@470: } giuliomoro@353: dcm.setCallback(sendDigitalMessage); giuliomoro@345: analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels - context->digitalChannels); giuliomoro@345: if(context->digitalChannels > 0){ giuliomoro@345: for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){ giuliomoro@353: dcm.setCallbackArgument(ch, receiverNames[ch]); 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@349: // set hooks before calling libpd_init giuliomoro@349: libpd_set_printhook(Bela_printHook); giuliomoro@349: libpd_set_floathook(Bela_floatHook); giuliomoro@352: libpd_set_messagehook(Bela_messageHook); giuliomoro@349: libpd_set_noteonhook(pdnoteon); giuliomoro@340: //TODO: add hooks for other midi events and generate MIDI output appropriately giuliomoro@349: libpd_init(); giuliomoro@357: //TODO: ideally, we would analyse the ASCII of the patch file and find out which in/outs to use giuliomoro@230: libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate); giuliomoro@350: gInBuf = libpd_get_sys_soundin(); giuliomoro@350: gOutBuf = libpd_get_sys_soundout(); 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@350: giuliomoro@350: // bind your receivers here giuliomoro@350: libpd_bind("bela_digitalOut11"); giuliomoro@350: libpd_bind("bela_digitalOut12"); giuliomoro@350: libpd_bind("bela_digitalOut13"); giuliomoro@350: libpd_bind("bela_digitalOut14"); giuliomoro@350: libpd_bind("bela_digitalOut15"); giuliomoro@350: libpd_bind("bela_digitalOut16"); giuliomoro@350: libpd_bind("bela_digitalOut17"); giuliomoro@350: libpd_bind("bela_digitalOut18"); giuliomoro@350: libpd_bind("bela_digitalOut19"); giuliomoro@350: libpd_bind("bela_digitalOut20"); giuliomoro@350: libpd_bind("bela_digitalOut21"); giuliomoro@350: libpd_bind("bela_digitalOut22"); giuliomoro@350: libpd_bind("bela_digitalOut23"); giuliomoro@350: libpd_bind("bela_digitalOut24"); giuliomoro@350: libpd_bind("bela_digitalOut25"); giuliomoro@350: libpd_bind("bela_digitalOut26"); giuliomoro@352: libpd_bind("bela_setDigital"); giuliomoro@350: giuliomoro@353: // open patch [; pd open file folder( giuliomoro@470: void* patch = libpd_openfile(file, folder); giuliomoro@470: if(patch == NULL){ giuliomoro@470: printf("Error: file %s/%s is corrupted.\n", folder, file); giuliomoro@470: return false; giuliomoro@470: } giuliomoro@340: libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles"); giuliomoro@340: Bela_scheduleAuxiliaryTask(libpdReadFilesTask); giuliomoro@340: giuliomoro@349: 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@341: static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize; giuliomoro@350: giuliomoro@350: // these are reset at every audio callback. Persistence across audio callbacks giuliomoro@350: // is handled by the core code. giuliomoro@350: // setDataOut = 0; giuliomoro@350: // clearDataOut = 0; giuliomoro@350: 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@353: // note: in multiple places below we assume that the number of digitals is same as number of audio giuliomoro@348: // digital in at message-rate giuliomoro@353: dcm.processInput(&context->digital[audioFrameBase], gLibpdBlockSize); giuliomoro@353: giuliomoro@348: // digital in at signal-rate giuliomoro@348: for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) { giuliomoro@353: unsigned int digitalFrame = audioFrameBase + j; giuliomoro@348: for (k = 0, p1 = p0 + gLibpdBlockSize * (context->audioChannels + 8); giuliomoro@348: k < 16; ++k, p1 += gLibpdBlockSize) { giuliomoro@353: if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate giuliomoro@353: *p1 = digitalRead(context, digitalFrame, k); giuliomoro@353: } giuliomoro@348: } giuliomoro@348: } giuliomoro@345: giuliomoro@342: libpd_process_sys(); // process the block giuliomoro@345: giuliomoro@348: //digital out giuliomoro@353: // digital out at signal-rate 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@353: if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate giuliomoro@353: digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5); giuliomoro@353: } giuliomoro@345: } giuliomoro@345: } giuliomoro@348: giuliomoro@350: // digital out at message-rate giuliomoro@353: dcm.processOutput(&context->digital[audioFrameBase], gLibpdBlockSize); giuliomoro@350: 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@354: 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@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@230: }