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);
48 jassert (0.0f <= value && value <= 1.0f);
49 return { roundToInt (value * 16383.0f) };
54 jassert (-1.0f <= value && value <= 1.0f);
55 return { roundToInt (((value + 1.0f) * 16383.0f) / 2.0f) };
65 return normalisedValue >> 7;
70 return normalisedValue;
76 return (normalisedValue < 8192)
77 ? jmap<float> (
float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
78 : jmap<float> (
float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
83 return jmap<float> (
float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
89 return normalisedValue == other.normalisedValue;
94 return ! operator== (other);
102class MPEValueTests final :
public UnitTest
106 :
UnitTest (
"MPEValue class", UnitTestCategories::midi)
109 void runTest()
override
111 beginTest (
"comparison operator");
113 MPEValue value1 = MPEValue::from7BitInt (7);
114 MPEValue value2 = MPEValue::from7BitInt (7);
115 MPEValue value3 = MPEValue::from7BitInt (8);
117 expect (value1 == value1);
118 expect (value1 == value2);
119 expect (value1 != value3);
122 beginTest (
"special values");
124 expectEquals (MPEValue::minValue().as7BitInt(), 0);
125 expectEquals (MPEValue::minValue().as14BitInt(), 0);
127 expectEquals (MPEValue::centreValue().as7BitInt(), 64);
128 expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
130 expectEquals (MPEValue::maxValue().as7BitInt(), 127);
131 expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
134 beginTest (
"zero/minimum value");
136 expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
137 expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
138 expectValuesConsistent (MPEValue::fromUnsignedFloat (0.0f), 0, 0, -1.0f, 0.0f);
139 expectValuesConsistent (MPEValue::fromSignedFloat (-1.0f), 0, 0, -1.0f, 0.0f);
142 beginTest (
"maximum value");
144 expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
145 expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
146 expectValuesConsistent (MPEValue::fromUnsignedFloat (1.0f), 127, 16383, 1.0f, 1.0f);
147 expectValuesConsistent (MPEValue::fromSignedFloat (1.0f), 127, 16383, 1.0f, 1.0f);
150 beginTest (
"centre value");
152 expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
153 expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
154 expectValuesConsistent (MPEValue::fromUnsignedFloat (0.5f), 64, 8192, 0.0f, 0.5f);
155 expectValuesConsistent (MPEValue::fromSignedFloat (0.0f), 64, 8192, 0.0f, 0.5f);
158 beginTest (
"value halfway between min and centre");
160 expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
161 expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
162 expectValuesConsistent (MPEValue::fromUnsignedFloat (0.25f), 32, 4096, -0.5f, 0.25f);
163 expectValuesConsistent (MPEValue::fromSignedFloat (-0.5f), 32, 4096, -0.5f, 0.25f);
169 void expectValuesConsistent (MPEValue value,
170 int expectedValueAs7BitInt,
171 int expectedValueAs14BitInt,
172 float expectedValueAsSignedFloat,
173 float expectedValueAsUnsignedFloat)
175 expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
176 expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
177 expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
178 expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
182 void expectFloatWithinRelativeError (
float actualValue,
float expectedValue,
float maxRelativeError)
184 const float maxAbsoluteError = jmax (1.0f, std::abs (expectedValue)) * maxRelativeError;
185 expect (std::abs (expectedValue - actualValue) < maxAbsoluteError);
189static 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 fromUnsignedFloat(float value) noexcept
static MPEValue fromSignedFloat(float value) 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