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.

198 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MidiBuffer MPEMessages::addZone (MPEZone zone)
  18. {
  19. MidiBuffer buffer (MidiRPNGenerator::generate (zone.getFirstNoteChannel(),
  20. zoneLayoutMessagesRpnNumber,
  21. zone.getNumNoteChannels(),
  22. false, false));
  23. buffer.addEvents (perNotePitchbendRange (zone), 0, -1, 0);
  24. buffer.addEvents (masterPitchbendRange (zone), 0, -1, 0);
  25. return buffer;
  26. }
  27. MidiBuffer MPEMessages::perNotePitchbendRange (MPEZone zone)
  28. {
  29. return MidiRPNGenerator::generate (zone.getFirstNoteChannel(), 0,
  30. zone.getPerNotePitchbendRange(),
  31. false, false);
  32. }
  33. MidiBuffer MPEMessages::masterPitchbendRange (MPEZone zone)
  34. {
  35. return MidiRPNGenerator::generate (zone.getMasterChannel(), 0,
  36. zone.getMasterPitchbendRange(),
  37. false, false);
  38. }
  39. MidiBuffer MPEMessages::clearAllZones()
  40. {
  41. return MidiRPNGenerator::generate (1, zoneLayoutMessagesRpnNumber, 16, false, false);
  42. }
  43. MidiBuffer MPEMessages::setZoneLayout (const MPEZoneLayout& layout)
  44. {
  45. MidiBuffer buffer;
  46. buffer.addEvents (clearAllZones(), 0, -1, 0);
  47. for (int i = 0; i < layout.getNumZones(); ++i)
  48. buffer.addEvents (addZone (*layout.getZoneByIndex (i)), 0, -1, 0);
  49. return buffer;
  50. }
  51. //==============================================================================
  52. //==============================================================================
  53. #if JUCE_UNIT_TESTS
  54. class MPEMessagesTests : public UnitTest
  55. {
  56. public:
  57. MPEMessagesTests() : UnitTest ("MPEMessages class") {}
  58. void runTest() override
  59. {
  60. beginTest ("add zone");
  61. {
  62. {
  63. MidiBuffer buffer = MPEMessages::addZone (MPEZone (1, 7));
  64. const uint8 expectedBytes[] =
  65. {
  66. 0xb1, 0x64, 0x06, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x07, // set up zone
  67. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x30, // per-note pbrange (default = 48)
  68. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x02 // master pbrange (default = 2)
  69. };
  70. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  71. }
  72. {
  73. MidiBuffer buffer = MPEMessages::addZone (MPEZone (11, 5, 96, 0));
  74. const uint8 expectedBytes[] =
  75. {
  76. 0xbb, 0x64, 0x06, 0xbb, 0x65, 0x00, 0xbb, 0x06, 0x05, // set up zone
  77. 0xbb, 0x64, 0x00, 0xbb, 0x65, 0x00, 0xbb, 0x06, 0x60, // per-note pbrange (custom)
  78. 0xba, 0x64, 0x00, 0xba, 0x65, 0x00, 0xba, 0x06, 0x00 // master pbrange (custom)
  79. };
  80. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  81. }
  82. }
  83. beginTest ("set per-note pitchbend range");
  84. {
  85. MPEZone zone (3, 7, 96);
  86. MidiBuffer buffer = MPEMessages::perNotePitchbendRange (zone);
  87. const uint8 expectedBytes[] = { 0xb3, 0x64, 0x00, 0xb3, 0x65, 0x00, 0xb3, 0x06, 0x60 };
  88. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  89. }
  90. beginTest ("set master pitchbend range");
  91. {
  92. MPEZone zone (3, 7, 48, 60);
  93. MidiBuffer buffer = MPEMessages::masterPitchbendRange (zone);
  94. const uint8 expectedBytes[] = { 0xb2, 0x64, 0x00, 0xb2, 0x65, 0x00, 0xb2, 0x06, 0x3c };
  95. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  96. }
  97. beginTest ("clear all zones");
  98. {
  99. MidiBuffer buffer = MPEMessages::clearAllZones();
  100. const uint8 expectedBytes[] = { 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x10 };
  101. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  102. }
  103. beginTest ("set complete state");
  104. {
  105. MPEZoneLayout layout;
  106. layout.addZone (MPEZone (1, 7, 96, 0));
  107. layout.addZone (MPEZone (9, 7));
  108. layout.addZone (MPEZone (5, 3));
  109. layout.addZone (MPEZone (5, 4));
  110. layout.addZone (MPEZone (6, 4));
  111. MidiBuffer buffer = MPEMessages::setZoneLayout (layout);
  112. const uint8 expectedBytes[] = {
  113. 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x10, // clear all zones
  114. 0xb1, 0x64, 0x06, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x03, // set zone 1 (1, 3)
  115. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x60, // per-note pbrange (custom)
  116. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x00, // master pbrange (custom)
  117. 0xb6, 0x64, 0x06, 0xb6, 0x65, 0x00, 0xb6, 0x06, 0x04, // set zone 2 (6, 4)
  118. 0xb6, 0x64, 0x00, 0xb6, 0x65, 0x00, 0xb6, 0x06, 0x30, // per-note pbrange (default = 48)
  119. 0xb5, 0x64, 0x00, 0xb5, 0x65, 0x00, 0xb5, 0x06, 0x02 // master pbrange (default = 2)
  120. };
  121. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  122. }
  123. }
  124. private:
  125. //==============================================================================
  126. void testMidiBuffer (MidiBuffer& buffer, const uint8* expectedBytes, int expectedBytesSize)
  127. {
  128. uint8 actualBytes[128] = { 0 };
  129. extractRawBinaryData (buffer, actualBytes, sizeof (actualBytes));
  130. expectEquals (std::memcmp (actualBytes, expectedBytes, (std::size_t) expectedBytesSize), 0);
  131. }
  132. //==============================================================================
  133. void extractRawBinaryData (const MidiBuffer& midiBuffer, const uint8* bufferToCopyTo, std::size_t maxBytes)
  134. {
  135. std::size_t pos = 0;
  136. MidiBuffer::Iterator iter (midiBuffer);
  137. MidiMessage midiMessage;
  138. int samplePosition; // Note: not actually used, so no need to initialise.
  139. while (iter.getNextEvent (midiMessage, samplePosition))
  140. {
  141. const uint8* data = midiMessage.getRawData();
  142. std::size_t dataSize = (std::size_t) midiMessage.getRawDataSize();
  143. if (pos + dataSize > maxBytes)
  144. return;
  145. std::memcpy ((void*) (bufferToCopyTo + pos), data, dataSize);
  146. pos += dataSize;
  147. }
  148. }
  149. };
  150. static MPEMessagesTests MPEMessagesUnitTests;
  151. #endif // JUCE_UNIT_TESTS