# HG changeset patch # User Giulio Moro # Date 1464961438 -3600 # Node ID a9905703219dfd0c308c3934dc7076d02b269a29 # Parent 9151fe15c1943783bde72ca9ff961ba7bc595c36 More extensive support for MIDI types for libpd. Untested diff -r 9151fe15c194 -r a9905703219d examples/basic_libpd/render.cpp --- a/examples/basic_libpd/render.cpp Thu Jun 02 02:17:08 2016 +0100 +++ b/examples/basic_libpd/render.cpp Fri Jun 03 14:43:58 2016 +0100 @@ -49,14 +49,18 @@ usleep(1000); } } - +#define PARSE_MIDI AuxiliaryTask udpReadTask; Midi midi; 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); @@ -110,33 +114,78 @@ void render(BelaContext *context, void *userData) { int num; +#ifdef PARSE_MIDI while((num = midi.getParser()->numAvailableMessages()) > 0){ static MidiChannelMessage message; message = midi.getParser()->getNextChannelMessage(); - message.prettyPrint(); +// message.prettyPrint(); // use this to print beautified message (channel, data bytes) switch(message.getType()){ - case kmmNoteOn: { + case kmmNoteOn: + { int noteNumber = message.getDataByte(0); int velocity = message.getDataByte(1); int channel = message.getChannel(); - rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel); libpd_noteon(channel, noteNumber, velocity); + break; } - break; - case kmmControlChange: { - int channel = message.getChannel(); - int controller = message.getDataByte(0); - int value = message.getDataByte(1); - libpd_controlchange(channel, controller, value); + 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; } - break; - case kmmProgramChange: - int channel = message.getChannel(); - int program = message.getDataByte(0); - libpd_programchange(channel, program); - 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; + } } } +#else + int input; + while((input = midi.getInput()) >= 0){ + libpd_midibyte(0, input); + rt_printf("input: %d\n", input); + } +#endif /* PARSE_MIDI */ static int inW = 0; static int outR = 0; /* diff -r 9151fe15c194 -r a9905703219d include/Midi.h --- a/include/Midi.h Thu Jun 02 02:17:08 2016 +0100 +++ b/include/Midi.h Fri Jun 03 14:43:58 2016 +0100 @@ -268,6 +268,7 @@ * @return 1 on success, -1 on failure */ int readFrom(int port); + /** * Open the specified output Midi port and prepares to write to it. * @param port Midi port to open