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.

239 lines
8.7KB

  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. MidiBuffer MPEMessages::setLowerZone (int numMemberChannels, int perNotePitchbendRange, int masterPitchbendRange)
  20. {
  21. auto buffer = MidiRPNGenerator::generate (1, zoneLayoutMessagesRpnNumber, numMemberChannels, false, false);
  22. buffer.addEvents (setLowerZonePerNotePitchbendRange (perNotePitchbendRange), 0, -1, 0);
  23. buffer.addEvents (setLowerZoneMasterPitchbendRange (masterPitchbendRange), 0, -1, 0);
  24. return buffer;
  25. }
  26. MidiBuffer MPEMessages::setUpperZone (int numMemberChannels, int perNotePitchbendRange, int masterPitchbendRange)
  27. {
  28. auto buffer = MidiRPNGenerator::generate (16, zoneLayoutMessagesRpnNumber, numMemberChannels, false, false);
  29. buffer.addEvents (setUpperZonePerNotePitchbendRange (perNotePitchbendRange), 0, -1, 0);
  30. buffer.addEvents (setUpperZoneMasterPitchbendRange (masterPitchbendRange), 0, -1, 0);
  31. return buffer;
  32. }
  33. MidiBuffer MPEMessages::setLowerZonePerNotePitchbendRange (int perNotePitchbendRange)
  34. {
  35. return MidiRPNGenerator::generate (2, 0, perNotePitchbendRange, false, false);
  36. }
  37. MidiBuffer MPEMessages::setUpperZonePerNotePitchbendRange (int perNotePitchbendRange)
  38. {
  39. return MidiRPNGenerator::generate (15, 0, perNotePitchbendRange, false, false);
  40. }
  41. MidiBuffer MPEMessages::setLowerZoneMasterPitchbendRange (int masterPitchbendRange)
  42. {
  43. return MidiRPNGenerator::generate (1, 0, masterPitchbendRange, false, false);
  44. }
  45. MidiBuffer MPEMessages::setUpperZoneMasterPitchbendRange (int masterPitchbendRange)
  46. {
  47. return MidiRPNGenerator::generate (16, 0, masterPitchbendRange, false, false);
  48. }
  49. MidiBuffer MPEMessages::clearLowerZone()
  50. {
  51. return MidiRPNGenerator::generate (1, zoneLayoutMessagesRpnNumber, 0, false, false);
  52. }
  53. MidiBuffer MPEMessages::clearUpperZone()
  54. {
  55. return MidiRPNGenerator::generate (16, zoneLayoutMessagesRpnNumber, 0, false, false);
  56. }
  57. MidiBuffer MPEMessages::clearAllZones()
  58. {
  59. MidiBuffer buffer;
  60. buffer.addEvents (clearLowerZone(), 0, -1, 0);
  61. buffer.addEvents (clearUpperZone(), 0, -1, 0);
  62. return buffer;
  63. }
  64. MidiBuffer MPEMessages::setZoneLayout (MPEZoneLayout layout)
  65. {
  66. MidiBuffer buffer;
  67. buffer.addEvents (clearAllZones(), 0, -1, 0);
  68. auto lowerZone = layout.getLowerZone();
  69. if (lowerZone.isActive())
  70. buffer.addEvents (setLowerZone (lowerZone.numMemberChannels,
  71. lowerZone.perNotePitchbendRange,
  72. lowerZone.masterPitchbendRange),
  73. 0, -1, 0);
  74. auto upperZone = layout.getUpperZone();
  75. if (upperZone.isActive())
  76. buffer.addEvents (setUpperZone (upperZone.numMemberChannels,
  77. upperZone.perNotePitchbendRange,
  78. upperZone.masterPitchbendRange),
  79. 0, -1, 0);
  80. return buffer;
  81. }
  82. //==============================================================================
  83. //==============================================================================
  84. #if JUCE_UNIT_TESTS
  85. class MPEMessagesTests : public UnitTest
  86. {
  87. public:
  88. MPEMessagesTests()
  89. : UnitTest ("MPEMessages class", UnitTestCategories::midi)
  90. {}
  91. void runTest() override
  92. {
  93. beginTest ("add zone");
  94. {
  95. {
  96. MidiBuffer buffer = MPEMessages::setLowerZone (7);
  97. const uint8 expectedBytes[] =
  98. {
  99. 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x07, // set up zone
  100. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x30, // per-note pbrange (default = 48)
  101. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x02 // master pbrange (default = 2)
  102. };
  103. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  104. }
  105. {
  106. MidiBuffer buffer = MPEMessages::setUpperZone (5, 96, 0);
  107. const uint8 expectedBytes[] =
  108. {
  109. 0xbf, 0x64, 0x06, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x05, // set up zone
  110. 0xbe, 0x64, 0x00, 0xbe, 0x65, 0x00, 0xbe, 0x06, 0x60, // per-note pbrange (custom)
  111. 0xbf, 0x64, 0x00, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x00 // master pbrange (custom)
  112. };
  113. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  114. }
  115. }
  116. beginTest ("set per-note pitchbend range");
  117. {
  118. MidiBuffer buffer = MPEMessages::setLowerZonePerNotePitchbendRange (96);
  119. const uint8 expectedBytes[] = { 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x60 };
  120. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  121. }
  122. beginTest ("set master pitchbend range");
  123. {
  124. MidiBuffer buffer = MPEMessages::setUpperZoneMasterPitchbendRange (60);
  125. const uint8 expectedBytes[] = { 0xbf, 0x64, 0x00, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x3c };
  126. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  127. }
  128. beginTest ("clear all zones");
  129. {
  130. MidiBuffer buffer = MPEMessages::clearAllZones();
  131. const uint8 expectedBytes[] = { 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x00, // clear lower zone
  132. 0xbf, 0x64, 0x06, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x00 // clear upper zone
  133. };
  134. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  135. }
  136. beginTest ("set complete state");
  137. {
  138. MPEZoneLayout layout;
  139. layout.setLowerZone (7, 96, 0);
  140. layout.setUpperZone (7);
  141. MidiBuffer buffer = MPEMessages::setZoneLayout (layout);
  142. const uint8 expectedBytes[] = {
  143. 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x00, // clear lower zone
  144. 0xbf, 0x64, 0x06, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x00, // clear upper zone
  145. 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x07, // set lower zone
  146. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x60, // per-note pbrange (custom)
  147. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x00, // master pbrange (custom)
  148. 0xbf, 0x64, 0x06, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x07, // set upper zone
  149. 0xbe, 0x64, 0x00, 0xbe, 0x65, 0x00, 0xbe, 0x06, 0x30, // per-note pbrange (default = 48)
  150. 0xbf, 0x64, 0x00, 0xbf, 0x65, 0x00, 0xbf, 0x06, 0x02 // master pbrange (default = 2)
  151. };
  152. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  153. }
  154. }
  155. private:
  156. //==============================================================================
  157. void testMidiBuffer (MidiBuffer& buffer, const uint8* expectedBytes, int expectedBytesSize)
  158. {
  159. uint8 actualBytes[128] = { 0 };
  160. extractRawBinaryData (buffer, actualBytes, sizeof (actualBytes));
  161. expectEquals (std::memcmp (actualBytes, expectedBytes, (std::size_t) expectedBytesSize), 0);
  162. }
  163. //==============================================================================
  164. void extractRawBinaryData (const MidiBuffer& midiBuffer, const uint8* bufferToCopyTo, std::size_t maxBytes)
  165. {
  166. std::size_t pos = 0;
  167. for (const auto metadata : midiBuffer)
  168. {
  169. const uint8* data = metadata.data;
  170. std::size_t dataSize = (std::size_t) metadata.numBytes;
  171. if (pos + dataSize > maxBytes)
  172. return;
  173. std::memcpy ((void*) (bufferToCopyTo + pos), data, dataSize);
  174. pos += dataSize;
  175. }
  176. }
  177. };
  178. static MPEMessagesTests MPEMessagesUnitTests;
  179. #endif
  180. } // namespace juce