OpenShot Library | libopenshot-audio 0.2.0
juce_MPEValue.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27MPEValue::MPEValue (int value) : normalisedValue (value) {}
28
29//==============================================================================
30MPEValue MPEValue::from7BitInt (int value) noexcept
31{
32 jassert (value >= 0 && value <= 127);
33
34 auto valueAs14Bit = value <= 64 ? value << 7
35 : int (jmap<float> (float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
36
37 return { valueAs14Bit };
38}
39
40MPEValue MPEValue::from14BitInt (int value) noexcept
41{
42 jassert (value >= 0 && value <= 16383);
43 return { value };
44}
45
46//==============================================================================
50
52{
53 return normalisedValue >> 7;
54}
55
57{
58 return normalisedValue;
59}
60
61//==============================================================================
63{
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);
67}
68
70{
71 return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
72}
73
74//==============================================================================
75bool MPEValue::operator== (const MPEValue& other) const noexcept
76{
77 return normalisedValue == other.normalisedValue;
78}
79
80bool MPEValue::operator!= (const MPEValue& other) const noexcept
81{
82 return ! operator== (other);
83}
84
85//==============================================================================
86//==============================================================================
87#if JUCE_UNIT_TESTS
88
89class MPEValueTests : public UnitTest
90{
91public:
92 MPEValueTests() : UnitTest ("MPEValue class", "MIDI/MPE") {}
93
94 void runTest() override
95 {
96 beginTest ("comparison operator");
97 {
98 MPEValue value1 = MPEValue::from7BitInt (7);
99 MPEValue value2 = MPEValue::from7BitInt (7);
100 MPEValue value3 = MPEValue::from7BitInt (8);
101
102 expect (value1 == value1);
103 expect (value1 == value2);
104 expect (value1 != value3);
105 }
106
107 beginTest ("special values");
108 {
109 expectEquals (MPEValue::minValue().as7BitInt(), 0);
110 expectEquals (MPEValue::minValue().as14BitInt(), 0);
111
112 expectEquals (MPEValue::centreValue().as7BitInt(), 64);
113 expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
114
115 expectEquals (MPEValue::maxValue().as7BitInt(), 127);
116 expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
117 }
118
119 beginTest ("zero/minimum value");
120 {
121 expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
122 expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
123 }
124
125 beginTest ("maximum value");
126 {
127 expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
128 expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
129 }
130
131 beginTest ("centre value");
132 {
133 expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
134 expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
135 }
136
137 beginTest ("value halfway between min and centre");
138 {
139 expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
140 expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
141 }
142 }
143
144private:
145 //==============================================================================
146 void expectValuesConsistent (MPEValue value,
151 {
152 expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
153 expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
154 expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
155 expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
156 }
157
158 //==============================================================================
160 {
161 const float maxAbsoluteError = jmax (1.0f, std::abs (expectedValue)) * maxRelativeError;
162 expect (std::abs (expectedValue - actualValue) < maxAbsoluteError);
163 }
164};
165
166static MPEValueTests MPEValueUnitTests;
167
168#endif // JUCE_UNIT_TESTS
169
170} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
Array()=default
Creates an empty array.
This class represents a single value for any of the MPE dimensions of control.
float asSignedFloat() const noexcept
Retrieves the current value mapped to a float between -1.0f and 1.0f.
static MPEValue maxValue() noexcept
Constructs an MPEValue corresponding to the maximum value.
static MPEValue centreValue() noexcept
Constructs an MPEValue corresponding to the centre value.
static MPEValue from14BitInt(int value) noexcept
Constructs an MPEValue from an integer between 0 and 16383 (using 14-bit precision).
bool operator==(const MPEValue &other) const noexcept
Returns true if two values are equal.
static MPEValue minValue() noexcept
Constructs an MPEValue corresponding to the minimum value.
float asUnsignedFloat() const noexcept
Retrieves the current value mapped to a float between 0.0f and 1.0f.
MPEValue() noexcept
Default constructor.
int as7BitInt() const noexcept
Retrieves the current value as an integer between 0 and 127.
int as14BitInt() const noexcept
Retrieves the current value as an integer between 0 and 16383.
static MPEValue from7BitInt(int value) noexcept
Constructs an MPEValue from an integer between 0 and 127 (using 7-bit precision).
bool operator!=(const MPEValue &other) const noexcept
Returns true if two values are not equal.
This is a base class for classes that perform a unit test.