Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MPEValue::MPEValue() noexcept : normalisedValue (8192)
  18. {
  19. }
  20. MPEValue::MPEValue (int value) : normalisedValue (value)
  21. {
  22. }
  23. //==============================================================================
  24. MPEValue MPEValue::from7BitInt (int value) noexcept
  25. {
  26. jassert (value >= 0 && value <= 127);
  27. const int valueAs14Bit = value <= 64 ? value << 7 : int (jmap<float> (float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
  28. return MPEValue (valueAs14Bit);
  29. }
  30. MPEValue MPEValue::from14BitInt (int value) noexcept
  31. {
  32. jassert (value >= 0 && value <= 16383);
  33. return MPEValue (value);
  34. }
  35. //==============================================================================
  36. MPEValue MPEValue::minValue() noexcept { return MPEValue::from7BitInt (0); }
  37. MPEValue MPEValue::centreValue() noexcept { return MPEValue::from7BitInt (64); }
  38. MPEValue MPEValue::maxValue() noexcept { return MPEValue::from7BitInt (127); }
  39. int MPEValue::as7BitInt() const noexcept
  40. {
  41. return normalisedValue >> 7;
  42. }
  43. int MPEValue::as14BitInt() const noexcept
  44. {
  45. return normalisedValue;
  46. }
  47. //==============================================================================
  48. float MPEValue::asSignedFloat() const noexcept
  49. {
  50. return (normalisedValue < 8192)
  51. ? jmap<float> (float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
  52. : jmap<float> (float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
  53. }
  54. float MPEValue::asUnsignedFloat() const noexcept
  55. {
  56. return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
  57. }
  58. //==============================================================================
  59. bool MPEValue::operator== (const MPEValue& other) const noexcept
  60. {
  61. return normalisedValue == other.normalisedValue;
  62. }
  63. bool MPEValue::operator!= (const MPEValue& other) const noexcept
  64. {
  65. return ! operator== (other);
  66. }
  67. //==============================================================================
  68. //==============================================================================
  69. #if JUCE_UNIT_TESTS
  70. class MPEValueTests : public UnitTest
  71. {
  72. public:
  73. MPEValueTests() : UnitTest ("MPEValue class") {}
  74. void runTest() override
  75. {
  76. beginTest ("comparison operator");
  77. {
  78. MPEValue value1 = MPEValue::from7BitInt (7);
  79. MPEValue value2 = MPEValue::from7BitInt (7);
  80. MPEValue value3 = MPEValue::from7BitInt (8);
  81. expect (value1 == value1);
  82. expect (value1 == value2);
  83. expect (value1 != value3);
  84. }
  85. beginTest ("special values");
  86. {
  87. expectEquals (MPEValue::minValue().as7BitInt(), 0);
  88. expectEquals (MPEValue::minValue().as14BitInt(), 0);
  89. expectEquals (MPEValue::centreValue().as7BitInt(), 64);
  90. expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
  91. expectEquals (MPEValue::maxValue().as7BitInt(), 127);
  92. expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
  93. }
  94. beginTest ("zero/minimum value");
  95. {
  96. expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
  97. expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
  98. }
  99. beginTest ("maximum value");
  100. {
  101. expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
  102. expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
  103. }
  104. beginTest ("centre value");
  105. {
  106. expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
  107. expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
  108. }
  109. beginTest ("value halfway between min and centre");
  110. {
  111. expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
  112. expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
  113. }
  114. }
  115. private:
  116. //==============================================================================
  117. void expectValuesConsistent (MPEValue value,
  118. int expectedValueAs7BitInt,
  119. int expectedValueAs14BitInt,
  120. float expectedValueAsSignedFloat,
  121. float expectedValueAsUnsignedFloat)
  122. {
  123. expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
  124. expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
  125. expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
  126. expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
  127. }
  128. //==============================================================================
  129. void expectFloatWithinRelativeError (float actualValue, float expectedValue, float maxRelativeError)
  130. {
  131. const float maxAbsoluteError = jmax (1.0f, std::fabs (expectedValue)) * maxRelativeError;
  132. expect (std::fabs (expectedValue - actualValue) < maxAbsoluteError);
  133. }
  134. };
  135. static MPEValueTests MPEValueUnitTests;
  136. #endif // JUCE_UNIT_TESTS