OpenShot Audio Library | OpenShotAudio: juce_MPEValue.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
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
26MPEValue::MPEValue() noexcept {}
27 MPEValue::MPEValue (int value) : normalisedValue (value) {}
28
29//==============================================================================
30 MPEValue 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
40 MPEValue MPEValue::from14BitInt (int value) noexcept
41{
42 jassert (value >= 0 && value <= 16383);
43 return { value };
44}
45
46//==============================================================================
47 MPEValue MPEValue::minValue() noexcept { return MPEValue::from7BitInt (0); }
48 MPEValue MPEValue::centreValue() noexcept { return MPEValue::from7BitInt (64); }
49 MPEValue MPEValue::maxValue() noexcept { return MPEValue::from7BitInt (127); }
50
51 int MPEValue::as7BitInt() const noexcept
52{
53 return normalisedValue >> 7;
54}
55
56 int MPEValue::as14BitInt() const noexcept
57{
58 return normalisedValue;
59}
60
61//==============================================================================
62 float MPEValue::asSignedFloat() const noexcept
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
69 float MPEValue::asUnsignedFloat() const noexcept
70{
71 return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
72}
73
74//==============================================================================
75 bool MPEValue::operator== (const MPEValue& other) const noexcept
76{
77 return normalisedValue == other.normalisedValue;
78}
79
80 bool MPEValue::operator!= (const MPEValue& other) const noexcept
81{
82 return ! operator== (other);
83}
84
85
86//==============================================================================
87//==============================================================================
88#if JUCE_UNIT_TESTS
89
90class MPEValueTests : public UnitTest
91{
92public:
93 MPEValueTests()
94 : UnitTest ("MPEValue class", UnitTestCategories::midi)
95 {}
96
97 void runTest() override
98 {
99 beginTest ("comparison operator");
100 {
101 MPEValue value1 = MPEValue::from7BitInt (7);
102 MPEValue value2 = MPEValue::from7BitInt (7);
103 MPEValue value3 = MPEValue::from7BitInt (8);
104
105 expect (value1 == value1);
106 expect (value1 == value2);
107 expect (value1 != value3);
108 }
109
110 beginTest ("special values");
111 {
112 expectEquals (MPEValue::minValue().as7BitInt(), 0);
113 expectEquals (MPEValue::minValue().as14BitInt(), 0);
114
115 expectEquals (MPEValue::centreValue().as7BitInt(), 64);
116 expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
117
118 expectEquals (MPEValue::maxValue().as7BitInt(), 127);
119 expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
120 }
121
122 beginTest ("zero/minimum value");
123 {
124 expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
125 expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
126 }
127
128 beginTest ("maximum value");
129 {
130 expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
131 expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
132 }
133
134 beginTest ("centre value");
135 {
136 expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
137 expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
138 }
139
140 beginTest ("value halfway between min and centre");
141 {
142 expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
143 expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
144 }
145 }
146
147private:
148 //==============================================================================
149 void expectValuesConsistent (MPEValue value,
150 int expectedValueAs7BitInt,
151 int expectedValueAs14BitInt,
152 float expectedValueAsSignedFloat,
153 float expectedValueAsUnsignedFloat)
154 {
155 expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
156 expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
157 expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
158 expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
159 }
160
161 //==============================================================================
162 void expectFloatWithinRelativeError (float actualValue, float expectedValue, float maxRelativeError)
163 {
164 const float maxAbsoluteError = jmax (1.0f, std::abs (expectedValue)) * maxRelativeError;
165 expect (std::abs (expectedValue - actualValue) < maxAbsoluteError);
166 }
167};
168
169static MPEValueTests MPEValueUnitTests;
170
171#endif
172
173} // namespace juce
juce::MPEValue
Definition: juce_MPEValue.h:37
juce::MPEValue::asSignedFloat
float asSignedFloat() const noexcept
Definition: juce_MPEValue.cpp:62
juce::MPEValue::maxValue
static MPEValue maxValue() noexcept
Definition: juce_MPEValue.cpp:49
juce::MPEValue::centreValue
static MPEValue centreValue() noexcept
Definition: juce_MPEValue.cpp:48
juce::MPEValue::from14BitInt
static MPEValue from14BitInt(int value) noexcept
Definition: juce_MPEValue.cpp:40
juce::MPEValue::operator==
bool operator==(const MPEValue &other) const noexcept
Definition: juce_MPEValue.cpp:75
juce::MPEValue::minValue
static MPEValue minValue() noexcept
Definition: juce_MPEValue.cpp:47
juce::MPEValue::asUnsignedFloat
float asUnsignedFloat() const noexcept
Definition: juce_MPEValue.cpp:69
juce::MPEValue::MPEValue
MPEValue() noexcept
Definition: juce_MPEValue.cpp:26
juce::MPEValue::as7BitInt
int as7BitInt() const noexcept
Definition: juce_MPEValue.cpp:51
juce::MPEValue::as14BitInt
int as14BitInt() const noexcept
Definition: juce_MPEValue.cpp:56
juce::MPEValue::from7BitInt
static MPEValue from7BitInt(int value) noexcept
Definition: juce_MPEValue.cpp:30
juce::MPEValue::operator!=
bool operator!=(const MPEValue &other) const noexcept
Definition: juce_MPEValue.cpp:80
juce::UnitTest
Definition: juce_UnitTest.h:70