Browse Source

MidiMessage: moved two useful helper functions into the public interface; minor cleanup of some member function declarations.

tags/2021-05-28
Timur Doumler 9 years ago
parent
commit
07bc151ce3
2 changed files with 28 additions and 10 deletions
  1. +19
    -8
      modules/juce_audio_basics/midi/juce_MidiMessage.cpp
  2. +9
    -2
      modules/juce_audio_basics/midi/juce_MidiMessage.h

+ 19
- 8
modules/juce_audio_basics/midi/juce_MidiMessage.cpp View File

@@ -33,11 +33,22 @@ namespace MidiHelpers
{ {
return (uint8) jlimit (0, 127, v); 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 void MidiMessage::setVelocity (const float newVelocity) noexcept
{ {
if (isNoteOnOrOff()) if (isNoteOnOrOff())
getData()[2] = MidiHelpers::floatVelocityToByte (newVelocity);
getData()[2] = floatValueToMidiByte (newVelocity);
} }
void MidiMessage::multiplyVelocity (const float scaleFactor) noexcept 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 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 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 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 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(); 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); return frequencyOfA * pow (2.0, (noteNumber - 69) / 12.0);
} }


+ 9
- 2
modules/juce_audio_basics/midi/juce_MidiMessage.h View File

@@ -851,7 +851,7 @@ public:
The value passed in must be 0x80 or higher. 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. /** 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. The frequencyOfA parameter is an optional frequency for 'A', normally 440-444Hz for concert pitch.
@see getMidiNoteName @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. */ /** Returns true if the given midi note number is a black key. */
static bool isMidiNoteBlack (int noteNumber) noexcept; static bool isMidiNoteBlack (int noteNumber) noexcept;
@@ -905,6 +905,13 @@ public:
*/ */
static const char* getControllerName (int controllerNumber); 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: private:
//============================================================================== //==============================================================================
double timeStamp; double timeStamp;


Loading…
Cancel
Save