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.

174 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 : normalisedValue (8192)
  20. {
  21. }
  22. MPEValue::MPEValue (int value) : normalisedValue (value)
  23. {
  24. }
  25. //==============================================================================
  26. MPEValue MPEValue::from7BitInt (int value) noexcept
  27. {
  28. jassert (value >= 0 && value <= 127);
  29. const int valueAs14Bit = value <= 64 ? value << 7 : int (jmap<float> (float (value - 64), 0.0f, 63.0f, 0.0f, 8191.0f)) + 8192;
  30. return MPEValue (valueAs14Bit);
  31. }
  32. MPEValue MPEValue::from14BitInt (int value) noexcept
  33. {
  34. jassert (value >= 0 && value <= 16383);
  35. return MPEValue (value);
  36. }
  37. //==============================================================================
  38. MPEValue MPEValue::minValue() noexcept { return MPEValue::from7BitInt (0); }
  39. MPEValue MPEValue::centreValue() noexcept { return MPEValue::from7BitInt (64); }
  40. MPEValue MPEValue::maxValue() noexcept { return MPEValue::from7BitInt (127); }
  41. int MPEValue::as7BitInt() const noexcept
  42. {
  43. return normalisedValue >> 7;
  44. }
  45. int MPEValue::as14BitInt() const noexcept
  46. {
  47. return normalisedValue;
  48. }
  49. //==============================================================================
  50. float MPEValue::asSignedFloat() const noexcept
  51. {
  52. return (normalisedValue < 8192)
  53. ? jmap<float> (float (normalisedValue), 0.0f, 8192.0f, -1.0f, 0.0f)
  54. : jmap<float> (float (normalisedValue), 8192.0f, 16383.0f, 0.0f, 1.0f);
  55. }
  56. float MPEValue::asUnsignedFloat() const noexcept
  57. {
  58. return jmap<float> (float (normalisedValue), 0.0f, 16383.0f, 0.0f, 1.0f);
  59. }
  60. //==============================================================================
  61. bool MPEValue::operator== (const MPEValue& other) const noexcept
  62. {
  63. return normalisedValue == other.normalisedValue;
  64. }
  65. bool MPEValue::operator!= (const MPEValue& other) const noexcept
  66. {
  67. return ! operator== (other);
  68. }
  69. //==============================================================================
  70. //==============================================================================
  71. #if JUCE_UNIT_TESTS
  72. class MPEValueTests : public UnitTest
  73. {
  74. public:
  75. MPEValueTests() : UnitTest ("MPEValue class", "MIDI/MPE") {}
  76. void runTest() override
  77. {
  78. beginTest ("comparison operator");
  79. {
  80. MPEValue value1 = MPEValue::from7BitInt (7);
  81. MPEValue value2 = MPEValue::from7BitInt (7);
  82. MPEValue value3 = MPEValue::from7BitInt (8);
  83. expect (value1 == value1);
  84. expect (value1 == value2);
  85. expect (value1 != value3);
  86. }
  87. beginTest ("special values");
  88. {
  89. expectEquals (MPEValue::minValue().as7BitInt(), 0);
  90. expectEquals (MPEValue::minValue().as14BitInt(), 0);
  91. expectEquals (MPEValue::centreValue().as7BitInt(), 64);
  92. expectEquals (MPEValue::centreValue().as14BitInt(), 8192);
  93. expectEquals (MPEValue::maxValue().as7BitInt(), 127);
  94. expectEquals (MPEValue::maxValue().as14BitInt(), 16383);
  95. }
  96. beginTest ("zero/minimum value");
  97. {
  98. expectValuesConsistent (MPEValue::from7BitInt (0), 0, 0, -1.0f, 0.0f);
  99. expectValuesConsistent (MPEValue::from14BitInt (0), 0, 0, -1.0f, 0.0f);
  100. }
  101. beginTest ("maximum value");
  102. {
  103. expectValuesConsistent (MPEValue::from7BitInt (127), 127, 16383, 1.0f, 1.0f);
  104. expectValuesConsistent (MPEValue::from14BitInt (16383), 127, 16383, 1.0f, 1.0f);
  105. }
  106. beginTest ("centre value");
  107. {
  108. expectValuesConsistent (MPEValue::from7BitInt (64), 64, 8192, 0.0f, 0.5f);
  109. expectValuesConsistent (MPEValue::from14BitInt (8192), 64, 8192, 0.0f, 0.5f);
  110. }
  111. beginTest ("value halfway between min and centre");
  112. {
  113. expectValuesConsistent (MPEValue::from7BitInt (32), 32, 4096, -0.5f, 0.25f);
  114. expectValuesConsistent (MPEValue::from14BitInt (4096), 32, 4096, -0.5f, 0.25f);
  115. }
  116. }
  117. private:
  118. //==============================================================================
  119. void expectValuesConsistent (MPEValue value,
  120. int expectedValueAs7BitInt,
  121. int expectedValueAs14BitInt,
  122. float expectedValueAsSignedFloat,
  123. float expectedValueAsUnsignedFloat)
  124. {
  125. expectEquals (value.as7BitInt(), expectedValueAs7BitInt);
  126. expectEquals (value.as14BitInt(), expectedValueAs14BitInt);
  127. expectFloatWithinRelativeError (value.asSignedFloat(), expectedValueAsSignedFloat, 0.0001f);
  128. expectFloatWithinRelativeError (value.asUnsignedFloat(), expectedValueAsUnsignedFloat, 0.0001f);
  129. }
  130. //==============================================================================
  131. void expectFloatWithinRelativeError (float actualValue, float expectedValue, float maxRelativeError)
  132. {
  133. const float maxAbsoluteError = jmax (1.0f, std::fabs (expectedValue)) * maxRelativeError;
  134. expect (std::fabs (expectedValue - actualValue) < maxAbsoluteError);
  135. }
  136. };
  137. static MPEValueTests MPEValueUnitTests;
  138. #endif // JUCE_UNIT_TESTS
  139. } // namespace juce