Browse Source

MidiMessage: Added VariableLengthValue::isValid() and removed assertion from MidiMessage::readVariableLengthValue()

tags/2021-05-28
ed 4 years ago
parent
commit
2fb3637e25
3 changed files with 7 additions and 4 deletions
  1. +1
    -1
      modules/juce_audio_basics/midi/juce_MidiFile.cpp
  2. +2
    -1
      modules/juce_audio_basics/midi/juce_MidiMessage.cpp
  3. +4
    -2
      modules/juce_audio_basics/midi/juce_MidiMessage.h

+ 1
- 1
modules/juce_audio_basics/midi/juce_MidiFile.cpp View File

@@ -231,7 +231,7 @@ namespace MidiFileHelpers
{
const auto delay = MidiMessage::readVariableLengthValue (data, (int) size);
if (delay.bytesUsed == 0)
if (! delay.isValid())
break;
data += delay.bytesUsed;


+ 2
- 1
modules/juce_audio_basics/midi/juce_MidiMessage.cpp View File

@@ -79,7 +79,6 @@ MidiMessage::VariableLengthValue MidiMessage::readVariableLengthValue (const uin
// bytes of input to construct a full value, or no terminating byte was
// found. This implementation only supports variable-length values of up
// to four bytes.
jassertfalse;
return {};
}
@@ -1225,6 +1224,7 @@ struct MidiMessageTest : public UnitTest
const auto result = MidiMessage::readVariableLengthValue (copy.data(),
(int) copy.size());
expect (result.isValid());
expectEquals (result.value, outputs[index]);
expectEquals (result.bytesUsed, (int) inputs[index].size());
@@ -1252,6 +1252,7 @@ struct MidiMessageTest : public UnitTest
const auto result = MidiMessage::readVariableLengthValue (input.data(),
(int) input.size());
expect (! result.isValid());
expectEquals (result.value, 0);
expectEquals (result.bytesUsed, 0);
}


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

@@ -872,7 +872,6 @@ public:
from a stream of bytes.
A valid value requires that `bytesUsed` is greater than 0.
If `bytesUsed <= 0` this object should be considered invalid.
*/
struct VariableLengthValue
{
@@ -881,6 +880,8 @@ public:
VariableLengthValue (int valueIn, int bytesUsedIn)
: value (valueIn), bytesUsed (bytesUsedIn) {}
bool isValid() const noexcept { return bytesUsed > 0; }
int value = 0;
int bytesUsed = 0;
};
@@ -891,7 +892,8 @@ public:
@param maxBytesToUse the number of bytes in the region following `data`
@returns a struct containing the parsed value, and the number
of bytes that were read. If parsing fails, both the
`value` and `bytesUsed` fields will be set to 0.
`value` and `bytesUsed` fields will be set to 0 and
`isValid()` will return false
*/
static VariableLengthValue readVariableLengthValue (const uint8* data,
int maxBytesToUse) noexcept;


Loading…
Cancel
Save