From 07bc151ce351f15a130a72fa22ed38f8ea59b919 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Wed, 25 May 2016 15:54:07 +0100 Subject: [PATCH] MidiMessage: moved two useful helper functions into the public interface; minor cleanup of some member function declarations. --- .../midi/juce_MidiMessage.cpp | 27 +++++++++++++------ .../juce_audio_basics/midi/juce_MidiMessage.h | 11 ++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/modules/juce_audio_basics/midi/juce_MidiMessage.cpp b/modules/juce_audio_basics/midi/juce_MidiMessage.cpp index a334dfe89e..14b92f22cd 100644 --- a/modules/juce_audio_basics/midi/juce_MidiMessage.cpp +++ b/modules/juce_audio_basics/midi/juce_MidiMessage.cpp @@ -33,11 +33,22 @@ namespace MidiHelpers { return (uint8) jlimit (0, 127, v); } +} - inline uint8 floatVelocityToByte (const float v) noexcept - { - return validVelocity (roundToInt (v * 127.0f)); - } +//============================================================================== +uint8 MidiMessage::floatValueToMidiByte (const float v) noexcept +{ + return MidiHelpers::validVelocity (roundToInt (v * 127.0f)); +} + +uint16 MidiMessage::pitchbendToPitchwheelPos (const float pitchbend, + const float pitchbendRange) noexcept +{ + // can't translate a pitchbend value that is outside of the given range! + jassert (std::abs (pitchbend) <= pitchbendRange); + + return pitchbend > 0.0f ? jmap (pitchbend, 0.0f, pitchbendRange, 8192.0f, 16383.0f) + : jmap (pitchbend, -pitchbendRange, 0.0f, 0.0f, 8192.0f); } //============================================================================== @@ -416,7 +427,7 @@ float MidiMessage::getFloatVelocity() const noexcept void MidiMessage::setVelocity (const float newVelocity) noexcept { if (isNoteOnOrOff()) - getData()[2] = MidiHelpers::floatVelocityToByte (newVelocity); + getData()[2] = floatValueToMidiByte (newVelocity); } void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept @@ -563,7 +574,7 @@ MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const MidiMessage MidiMessage::noteOn (const int channel, const int noteNumber, const float velocity) noexcept { - return noteOn (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity)); + return noteOn (channel, noteNumber, floatValueToMidiByte (velocity)); } MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 velocity) noexcept @@ -577,7 +588,7 @@ MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, uint8 MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber, float velocity) noexcept { - return noteOff (channel, noteNumber, MidiHelpers::floatVelocityToByte (velocity)); + return noteOff (channel, noteNumber, floatValueToMidiByte (velocity)); } MidiMessage MidiMessage::noteOff (const int channel, const int noteNumber) noexcept @@ -1010,7 +1021,7 @@ String MidiMessage::getMidiNoteName (int note, bool useSharps, bool includeOctav return String(); } -double MidiMessage::getMidiNoteInHertz (int noteNumber, const double frequencyOfA) noexcept +double MidiMessage::getMidiNoteInHertz (const int noteNumber, const double frequencyOfA) noexcept { return frequencyOfA * pow (2.0, (noteNumber - 69) / 12.0); } diff --git a/modules/juce_audio_basics/midi/juce_MidiMessage.h b/modules/juce_audio_basics/midi/juce_MidiMessage.h index 10fd0b2090..1349b7764a 100644 --- a/modules/juce_audio_basics/midi/juce_MidiMessage.h +++ b/modules/juce_audio_basics/midi/juce_MidiMessage.h @@ -851,7 +851,7 @@ public: The value passed in must be 0x80 or higher. */ - static int getMessageLengthFromFirstByte (const uint8 firstByte) noexcept; + static int getMessageLengthFromFirstByte (uint8 firstByte) noexcept; //============================================================================== /** Returns the name of a midi note number. @@ -878,7 +878,7 @@ public: The frequencyOfA parameter is an optional frequency for 'A', normally 440-444Hz for concert pitch. @see getMidiNoteName */ - static double getMidiNoteInHertz (int noteNumber, const double frequencyOfA = 440.0) noexcept; + static double getMidiNoteInHertz (int noteNumber, double frequencyOfA = 440.0) noexcept; /** Returns true if the given midi note number is a black key. */ static bool isMidiNoteBlack (int noteNumber) noexcept; @@ -905,6 +905,13 @@ public: */ static const char* getControllerName (int controllerNumber); + /** Converts a floating-point value between 0 and 1 to a MIDI 7-bit value between 0 and 127. */ + static uint8 floatValueToMidiByte (float valueBetween0and1) noexcept; + + /** Converts a pitchbend value in semitones to a MIDI 14-bit pitchwheel position value. */ + static uint16 pitchbendToPitchwheelPos (float pitchbendInSemitones, + float pitchbendRangeInSemitones) noexcept; + private: //============================================================================== double timeStamp;