From 3f8b213525b48ae000b3ab0e01c939f03d03ca78 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 14 Sep 2015 20:03:42 +0100 Subject: [PATCH] Added a MidiMessage::noteOff method that takes a float velocity parameter. --- .../midi/juce_MidiMessage.cpp | 30 +++++++++++++++---- .../juce_audio_basics/midi/juce_MidiMessage.h | 19 +++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/modules/juce_audio_basics/midi/juce_MidiMessage.cpp b/modules/juce_audio_basics/midi/juce_MidiMessage.cpp index 5638afeb05..e8adb9a80d 100644 --- a/modules/juce_audio_basics/midi/juce_MidiMessage.cpp +++ b/modules/juce_audio_basics/midi/juce_MidiMessage.cpp @@ -33,6 +33,11 @@ namespace MidiHelpers { return (uint8) jlimit (0, 127, v); } + + inline uint8 floatVelocityToByte (const float v) noexcept + { + return validVelocity (roundToInt (v * 127.0f)); + } } //============================================================================== @@ -386,7 +391,7 @@ float MidiMessage::getFloatVelocity() const noexcept void MidiMessage::setVelocity (const float newVelocity) noexcept { if (isNoteOnOrOff()) - getData()[2] = MidiHelpers::validVelocity (roundToInt (newVelocity * 127.0f)); + getData()[2] = MidiHelpers::floatVelocityToByte (newVelocity); } void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept @@ -522,11 +527,6 @@ MidiMessage MidiMessage::controllerEvent (const int channel, const int controlle controllerType & 127, value & 127); } -MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept -{ - return noteOn (channel, noteNumber, (uint8) (velocity * 127.0f + 0.5f)); -} - MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const uint8 velocity) noexcept { jassert (channel > 0 && channel <= 16); @@ -536,6 +536,11 @@ MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const noteNumber & 127, MidiHelpers::validVelocity (velocity)); } +MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept +{ + return noteOn (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity)); +} + MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept { jassert (channel > 0 && channel <= 16); @@ -545,6 +550,19 @@ MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 noteNumber & 127, MidiHelpers::validVelocity (velocity)); } +MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept +{ + return noteOff (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity)); +} + +MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept +{ + jassert (channel > 0 && channel <= 16); + jassert (isPositiveAndBelow (noteNumber, (int) 128)); + + return MidiMessage (MidiHelpers::initialByte (0x80, channel), noteNumber & 127, 0); +} + MidiMessage MidiMessage::allNotesOff (const int channel) noexcept { return controllerEvent (channel, 123, 0); diff --git a/modules/juce_audio_basics/midi/juce_MidiMessage.h b/modules/juce_audio_basics/midi/juce_MidiMessage.h index 6d59c59715..1ab7a4c9ae 100644 --- a/modules/juce_audio_basics/midi/juce_MidiMessage.h +++ b/modules/juce_audio_basics/midi/juce_MidiMessage.h @@ -241,7 +241,24 @@ public: @param velocity in the range 0 to 127 @see isNoteOff */ - static MidiMessage noteOff (int channel, int noteNumber, uint8 velocity = 0) noexcept; + static MidiMessage noteOff (int channel, int noteNumber, float velocity) noexcept; + + /** Creates a key-up message. + + @param channel the midi channel, in the range 1 to 16 + @param noteNumber the key number, 0 to 127 + @param velocity in the range 0 to 1 + @see isNoteOff + */ + static MidiMessage noteOff (int channel, int noteNumber, uint8 velocity) noexcept; + + /** Creates a key-up message. + + @param channel the midi channel, in the range 1 to 16 + @param noteNumber the key number, 0 to 127 + @see isNoteOff + */ + static MidiMessage noteOff (int channel, int noteNumber) noexcept; /** Returns true if this message is a 'key-down' or 'key-up' event.