32 jassert (value >= 0 && value <= 127);
34 auto valueAs14Bit = value <= 64 ? value << 7
35 : int (jmap<float> (
float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
37 return { valueAs14Bit };
42 jassert (value >= 0 && value <= 16383);
53 return normalisedValue >> 7;
58 return normalisedValue;
64 return (normalisedValue < 8192)
65 ? jmap<float> (
float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
66 : jmap<float> (
float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
71 return jmap<float> (
float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
77 return normalisedValue == other.normalisedValue;
82 return ! operator== (other);
94 :
UnitTest (
"MPEValue class", UnitTestCategories::midi)
97 void runTest()
override
99 beginTest (
"comparison operator");
101 MPEValue value1 = MPEValue::from7BitInt (7);
102 MPEValue value2 = MPEValue::from7BitInt (7);
103 MPEValue value3 = MPEValue::from7BitInt (8);
105 expect (value1 == value1);
106 expect (value1 == value2);
107 expect (value1 != value3);
110 beginTest (
"special values");
112 expectEquals (MPEValue::minValue().as7BitInt(), 0);
113 expectEquals (MPEValue::minValue().as14BitInt(), 0);
115 expectEquals (MPEValue::centreValue().as7BitInt(), 64);
116 expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
118 expectEquals (MPEValue::maxValue().as7BitInt(), 127);
119 expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
122 beginTest (
"zero/minimum value");
124 expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
125 expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
128 beginTest (
"maximum value");
130 expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
131 expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
134 beginTest (
"centre value");
136 expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
137 expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
140 beginTest (
"value halfway between min and centre");
142 expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
143 expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
149 void expectValuesConsistent (MPEValue value,
150 int expectedValueAs7BitInt,
151 int expectedValueAs14BitInt,
152 float expectedValueAsSignedFloat,
153 float expectedValueAsUnsignedFloat)
155 expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
156 expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
157 expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
158 expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
162 void expectFloatWithinRelativeError (
float actualValue,
float expectedValue,
float maxRelativeError)
164 const float maxAbsoluteError = jmax (1.0f, std::abs (expectedValue)) * maxRelativeError;
165 expect (std::abs (expectedValue - actualValue) < maxAbsoluteError);
169static MPEValueTests MPEValueUnitTests;
float asSignedFloat() const noexcept
static MPEValue maxValue() noexcept
static MPEValue centreValue() noexcept
static MPEValue from14BitInt(int value) noexcept
bool operator==(const MPEValue &other) const noexcept
static MPEValue minValue() noexcept
float asUnsignedFloat() const noexcept
int as7BitInt() const noexcept
int as14BitInt() const noexcept
static MPEValue from7BitInt(int value) noexcept
bool operator!=(const MPEValue &other) const noexcept