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.

194 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. MPEValue::MPEValue() noexcept {}
  20. MPEValue::MPEValue (int value) : normalisedValue (value) {}
  21. //==============================================================================
  22. MPEValue MPEValue::from7BitInt (int value) noexcept
  23. {
  24. jassert (value >= 0 && value <= 127);
  25. auto valueAs14Bit = value <= 64 ? value << 7
  26. : int (jmap<float> (float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
  27. return { valueAs14Bit };
  28. }
  29. MPEValue MPEValue::from14BitInt (int value) noexcept
  30. {
  31. jassert (value >= 0 && value <= 16383);
  32. return { value };
  33. }
  34. MPEValue MPEValue::fromUnsignedFloat (float value) noexcept
  35. {
  36. jassert (0.0f <= value && value <= 1.0f);
  37. return { roundToInt (value * 16383.0f) };
  38. }
  39. MPEValue MPEValue::fromSignedFloat (float value) noexcept
  40. {
  41. jassert (-1.0f <= value && value <= 1.0f);
  42. return { roundToInt (((value + 1.0f) * 16383.0f) / 2.0f) };
  43. }
  44. //==============================================================================
  45. MPEValue MPEValue::minValue() noexcept { return MPEValue::from7BitInt (0); }
  46. MPEValue MPEValue::centreValue() noexcept { return MPEValue::from7BitInt (64); }
  47. MPEValue MPEValue::maxValue() noexcept { return MPEValue::from7BitInt (127); }
  48. int MPEValue::as7BitInt() const noexcept
  49. {
  50. return normalisedValue >> 7;
  51. }
  52. int MPEValue::as14BitInt() const noexcept
  53. {
  54. return normalisedValue;
  55. }
  56. //==============================================================================
  57. float MPEValue::asSignedFloat() const noexcept
  58. {
  59. return (normalisedValue < 8192)
  60. ? jmap<float> (float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
  61. : jmap<float> (float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
  62. }
  63. float MPEValue::asUnsignedFloat() const noexcept
  64. {
  65. return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
  66. }
  67. //==============================================================================
  68. bool MPEValue::operator== (const MPEValue& other) const noexcept
  69. {
  70. return normalisedValue == other.normalisedValue;
  71. }
  72. bool MPEValue::operator!= (const MPEValue& other) const noexcept
  73. {
  74. return ! operator== (other);
  75. }
  76. //==============================================================================
  77. //==============================================================================
  78. #if JUCE_UNIT_TESTS
  79. class MPEValueTests : public UnitTest
  80. {
  81. public:
  82. MPEValueTests()
  83. : UnitTest ("MPEValue class", UnitTestCategories::midi)
  84. {}
  85. void runTest() override
  86. {
  87. beginTest ("comparison operator");
  88. {
  89. MPEValue value1 = MPEValue::from7BitInt (7);
  90. MPEValue value2 = MPEValue::from7BitInt (7);
  91. MPEValue value3 = MPEValue::from7BitInt (8);
  92. expect (value1 == value1);
  93. expect (value1 == value2);
  94. expect (value1 != value3);
  95. }
  96. beginTest ("special values");
  97. {
  98. expectEquals (MPEValue::minValue().as7BitInt(), 0);
  99. expectEquals (MPEValue::minValue().as14BitInt(), 0);
  100. expectEquals (MPEValue::centreValue().as7BitInt(), 64);
  101. expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
  102. expectEquals (MPEValue::maxValue().as7BitInt(), 127);
  103. expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
  104. }
  105. beginTest ("zero/minimum value");
  106. {
  107. expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
  108. expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
  109. expectValuesConsistent (MPEValue::fromUnsignedFloat (0.0f), 0, 0, -1.0f, 0.0f);
  110. expectValuesConsistent (MPEValue::fromSignedFloat (-1.0f), 0, 0, -1.0f, 0.0f);
  111. }
  112. beginTest ("maximum value");
  113. {
  114. expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
  115. expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
  116. expectValuesConsistent (MPEValue::fromUnsignedFloat (1.0f), 127, 16383, 1.0f, 1.0f);
  117. expectValuesConsistent (MPEValue::fromSignedFloat (1.0f), 127, 16383, 1.0f, 1.0f);
  118. }
  119. beginTest ("centre value");
  120. {
  121. expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
  122. expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
  123. expectValuesConsistent (MPEValue::fromUnsignedFloat (0.5f), 64, 8192, 0.0f, 0.5f);
  124. expectValuesConsistent (MPEValue::fromSignedFloat (0.0f), 64, 8192, 0.0f, 0.5f);
  125. }
  126. beginTest ("value halfway between min and centre");
  127. {
  128. expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
  129. expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
  130. expectValuesConsistent (MPEValue::fromUnsignedFloat (0.25f), 32, 4096, -0.5f, 0.25f);
  131. expectValuesConsistent (MPEValue::fromSignedFloat (-0.5f), 32, 4096, -0.5f, 0.25f);
  132. }
  133. }
  134. private:
  135. //==============================================================================
  136. void expectValuesConsistent (MPEValue value,
  137. int expectedValueAs7BitInt,
  138. int expectedValueAs14BitInt,
  139. float expectedValueAsSignedFloat,
  140. float expectedValueAsUnsignedFloat)
  141. {
  142. expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
  143. expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
  144. expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
  145. expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
  146. }
  147. //==============================================================================
  148. void expectFloatWithinRelativeError (float actualValue, float expectedValue, float maxRelativeError)
  149. {
  150. const float maxAbsoluteError = jmax (1.0f, std::abs (expectedValue)) * maxRelativeError;
  151. expect (std::abs (expectedValue - actualValue) < maxAbsoluteError);
  152. }
  153. };
  154. static MPEValueTests MPEValueUnitTests;
  155. #endif
  156. } // namespace juce