From 3f1e5c474a0062eda89d0a59d84e98ae8d0d5aac Mon Sep 17 00:00:00 2001 From: Francois Best Date: Wed, 12 Mar 2014 21:34:46 +0100 Subject: [PATCH] Handling null-velocity NoteOn as NoteOff (with setting), inlined some methods. --- src/MIDI.h | 9 +++++---- src/MIDI.hpp | 20 ++++++++++++++++---- src/midi_Settings.h | 5 +++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/MIDI.h b/src/MIDI.h index ea54251..2a92b80 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -99,8 +99,8 @@ private: #if MIDI_BUILD_INPUT public: - bool read(); - bool read(Channel inChannel); + inline bool read(); + inline bool read(Channel inChannel); public: inline MidiType getType() const; @@ -120,9 +120,10 @@ public: static inline bool isChannelMessage(MidiType inType); private: - bool inputFilter(Channel inChannel); bool parse(); - void resetInput(); + inline void handleNullVelocityNoteOnAsNoteOff(); + inline bool inputFilter(Channel inChannel); + inline void resetInput(); private: StatusByte mRunningStatus_RX; diff --git a/src/MIDI.hpp b/src/MIDI.hpp index 60983c4..084daaa 100644 --- a/src/MIDI.hpp +++ b/src/MIDI.hpp @@ -457,7 +457,7 @@ StatusByte MidiInterface::getStatus(MidiType inType, @see see setInputChannel() */ template -bool MidiInterface::read() +inline bool MidiInterface::read() { return read(mInputChannel); } @@ -465,13 +465,14 @@ bool MidiInterface::read() /*! \brief Read messages on a specified channel. */ template -bool MidiInterface::read(Channel inChannel) +inline bool MidiInterface::read(Channel inChannel) { if (inChannel >= MIDI_CHANNEL_OFF) return false; // MIDI Input disabled. if (parse()) { + handleNullVelocityNoteOnAsNoteOff(); if (inputFilter(inChannel)) { @@ -770,9 +771,20 @@ bool MidiInterface::parse() return false; } +// Private method, see midi_Settings.h for documentation +inline void MidiInterface::handleNullVelocityNoteOnAsNoteOff() +{ + #if MIDI_HANDLE_NULL_VELOCITY_NOTE_ON_AS_NOTE_OFF + if (getType() == NoteOn && getData2() == 0) + { + mMessage.type = NoteOff; + } + #endif +} + // Private method: check if the received message is on the listened channel template -bool MidiInterface::inputFilter(Channel inChannel) +inline bool MidiInterface::inputFilter(Channel inChannel) { // This method handles recognition of channel // (to know if the message is destinated to the Arduino) @@ -804,7 +816,7 @@ bool MidiInterface::inputFilter(Channel inChannel) // Private method: reset input attributes template -void MidiInterface::resetInput() +inline void MidiInterface::resetInput() { mPendingMessageIndex = 0; mPendingMessageExpectedLenght = 0; diff --git a/src/midi_Settings.h b/src/midi_Settings.h index e85a2ba..eae8555 100644 --- a/src/midi_Settings.h +++ b/src/midi_Settings.h @@ -69,6 +69,11 @@ // Set to 0 if you have troubles controlling your hardware. #define MIDI_USE_RUNNING_STATUS 1 +// NoteOn with 0 velocity should be handled as NoteOf. +// Set to 1 to get NoteOff events when receiving null-velocity NoteOn messages. +// Set to 0 to get NoteOn events when receiving null-velocity NoteOn messages. +#define MIDI_HANDLE_NULL_VELOCITY_NOTE_ON_AS_NOTE_OFF 1 + // Setting this to 1 will make MIDI.read parse only one byte of data for each // call when data is available. This can speed up your application if receiving // a lot of traffic, but might induce MIDI Thru and treatment latency.