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.

juce_MPEValue.cpp 6.2KB

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