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.

177 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MPEValue::MPEValue() noexcept : normalisedValue (8192)
  24. {
  25. }
  26. MPEValue::MPEValue (int value) : normalisedValue (value)
  27. {
  28. }
  29. //==============================================================================
  30. MPEValue MPEValue::from7BitInt (int value) noexcept
  31. {
  32. jassert (value >= 0 && value <= 127);
  33. const int valueAs14Bit = value <= 64 ? value << 7 : int (jmap<float> (float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
  34. return MPEValue (valueAs14Bit);
  35. }
  36. MPEValue MPEValue::from14BitInt (int value) noexcept
  37. {
  38. jassert (value >= 0 && value <= 16383);
  39. return MPEValue (value);
  40. }
  41. //==============================================================================
  42. MPEValue MPEValue::minValue() noexcept { return MPEValue::from7BitInt (0); }
  43. MPEValue MPEValue::centreValue() noexcept { return MPEValue::from7BitInt (64); }
  44. MPEValue MPEValue::maxValue() noexcept { return MPEValue::from7BitInt (127); }
  45. int MPEValue::as7BitInt() const noexcept
  46. {
  47. return normalisedValue >> 7;
  48. }
  49. int MPEValue::as14BitInt() const noexcept
  50. {
  51. return normalisedValue;
  52. }
  53. //==============================================================================
  54. float MPEValue::asSignedFloat() const noexcept
  55. {
  56. return (normalisedValue < 8192)
  57. ? jmap<float> (float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
  58. : jmap<float> (float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
  59. }
  60. float MPEValue::asUnsignedFloat() const noexcept
  61. {
  62. return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
  63. }
  64. //==============================================================================
  65. bool MPEValue::operator== (const MPEValue& other) const noexcept
  66. {
  67. return normalisedValue == other.normalisedValue;
  68. }
  69. bool MPEValue::operator!= (const MPEValue& other) const noexcept
  70. {
  71. return ! operator== (other);
  72. }
  73. //==============================================================================
  74. //==============================================================================
  75. #if JUCE_UNIT_TESTS
  76. class MPEValueTests : public UnitTest
  77. {
  78. public:
  79. MPEValueTests() : UnitTest ("MPEValue class") {}
  80. void runTest() override
  81. {
  82. beginTest ("comparison operator");
  83. {
  84. MPEValue value1 = MPEValue::from7BitInt (7);
  85. MPEValue value2 = MPEValue::from7BitInt (7);
  86. MPEValue value3 = MPEValue::from7BitInt (8);
  87. expect (value1 == value1);
  88. expect (value1 == value2);
  89. expect (value1 != value3);
  90. }
  91. beginTest ("special values");
  92. {
  93. expectEquals (MPEValue::minValue().as7BitInt(), 0);
  94. expectEquals (MPEValue::minValue().as14BitInt(), 0);
  95. expectEquals (MPEValue::centreValue().as7BitInt(), 64);
  96. expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
  97. expectEquals (MPEValue::maxValue().as7BitInt(), 127);
  98. expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
  99. }
  100. beginTest ("zero/minimum value");
  101. {
  102. expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
  103. expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
  104. }
  105. beginTest ("maximum value");
  106. {
  107. expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
  108. expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
  109. }
  110. beginTest ("centre value");
  111. {
  112. expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
  113. expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
  114. }
  115. beginTest ("value halfway between min and centre");
  116. {
  117. expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
  118. expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
  119. }
  120. }
  121. private:
  122. //==============================================================================
  123. void expectValuesConsistent (MPEValue value,
  124. int expectedValueAs7BitInt,
  125. int expectedValueAs14BitInt,
  126. float expectedValueAsSignedFloat,
  127. float expectedValueAsUnsignedFloat)
  128. {
  129. expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
  130. expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
  131. expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
  132. expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
  133. }
  134. //==============================================================================
  135. void expectFloatWithinRelativeError (float actualValue, float expectedValue, float maxRelativeError)
  136. {
  137. const float maxAbsoluteError = jmax (1.0f, std::fabs (expectedValue)) * maxRelativeError;
  138. expect (std::fabs (expectedValue - actualValue) < maxAbsoluteError);
  139. }
  140. };
  141. static MPEValueTests MPEValueUnitTests;
  142. #endif // JUCE_UNIT_TESTS