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.

204 lines
7.9KB

  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. MidiBuffer MPEMessages::addZone (MPEZone zone)
  24. {
  25. MidiBuffer buffer (MidiRPNGenerator::generate (zone.getFirstNoteChannel(),
  26. zoneLayoutMessagesRpnNumber,
  27. zone.getNumNoteChannels(),
  28. false, false));
  29. buffer.addEvents (perNotePitchbendRange (zone), 0, -1, 0);
  30. buffer.addEvents (masterPitchbendRange (zone), 0, -1, 0);
  31. return buffer;
  32. }
  33. MidiBuffer MPEMessages::perNotePitchbendRange (MPEZone zone)
  34. {
  35. return MidiRPNGenerator::generate (zone.getFirstNoteChannel(), 0,
  36. zone.getPerNotePitchbendRange(),
  37. false, false);
  38. }
  39. MidiBuffer MPEMessages::masterPitchbendRange (MPEZone zone)
  40. {
  41. return MidiRPNGenerator::generate (zone.getMasterChannel(), 0,
  42. zone.getMasterPitchbendRange(),
  43. false, false);
  44. }
  45. MidiBuffer MPEMessages::clearAllZones()
  46. {
  47. return MidiRPNGenerator::generate (1, zoneLayoutMessagesRpnNumber, 16, false, false);
  48. }
  49. MidiBuffer MPEMessages::setZoneLayout (const MPEZoneLayout& layout)
  50. {
  51. MidiBuffer buffer;
  52. buffer.addEvents (clearAllZones(), 0, -1, 0);
  53. for (int i = 0; i < layout.getNumZones(); ++i)
  54. buffer.addEvents (addZone (*layout.getZoneByIndex (i)), 0, -1, 0);
  55. return buffer;
  56. }
  57. //==============================================================================
  58. //==============================================================================
  59. #if JUCE_UNIT_TESTS
  60. class MPEMessagesTests : public UnitTest
  61. {
  62. public:
  63. MPEMessagesTests() : UnitTest ("MPEMessages class") {}
  64. void runTest() override
  65. {
  66. beginTest ("add zone");
  67. {
  68. {
  69. MidiBuffer buffer = MPEMessages::addZone (MPEZone (1, 7));
  70. const uint8 expectedBytes[] =
  71. {
  72. 0xb1, 0x64, 0x06, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x07, // set up zone
  73. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x30, // per-note pbrange (default = 48)
  74. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x02 // master pbrange (default = 2)
  75. };
  76. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  77. }
  78. {
  79. MidiBuffer buffer = MPEMessages::addZone (MPEZone (11, 5, 96, 0));
  80. const uint8 expectedBytes[] =
  81. {
  82. 0xbb, 0x64, 0x06, 0xbb, 0x65, 0x00, 0xbb, 0x06, 0x05, // set up zone
  83. 0xbb, 0x64, 0x00, 0xbb, 0x65, 0x00, 0xbb, 0x06, 0x60, // per-note pbrange (custom)
  84. 0xba, 0x64, 0x00, 0xba, 0x65, 0x00, 0xba, 0x06, 0x00 // master pbrange (custom)
  85. };
  86. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  87. }
  88. }
  89. beginTest ("set per-note pitchbend range");
  90. {
  91. MPEZone zone (3, 7, 96);
  92. MidiBuffer buffer = MPEMessages::perNotePitchbendRange (zone);
  93. const uint8 expectedBytes[] = { 0xb3, 0x64, 0x00, 0xb3, 0x65, 0x00, 0xb3, 0x06, 0x60 };
  94. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  95. }
  96. beginTest ("set master pitchbend range");
  97. {
  98. MPEZone zone (3, 7, 48, 60);
  99. MidiBuffer buffer = MPEMessages::masterPitchbendRange (zone);
  100. const uint8 expectedBytes[] = { 0xb2, 0x64, 0x00, 0xb2, 0x65, 0x00, 0xb2, 0x06, 0x3c };
  101. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  102. }
  103. beginTest ("clear all zones");
  104. {
  105. MidiBuffer buffer = MPEMessages::clearAllZones();
  106. const uint8 expectedBytes[] = { 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x10 };
  107. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  108. }
  109. beginTest ("set complete state");
  110. {
  111. MPEZoneLayout layout;
  112. layout.addZone (MPEZone (1, 7, 96, 0));
  113. layout.addZone (MPEZone (9, 7));
  114. layout.addZone (MPEZone (5, 3));
  115. layout.addZone (MPEZone (5, 4));
  116. layout.addZone (MPEZone (6, 4));
  117. MidiBuffer buffer = MPEMessages::setZoneLayout (layout);
  118. const uint8 expectedBytes[] = {
  119. 0xb0, 0x64, 0x06, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x10, // clear all zones
  120. 0xb1, 0x64, 0x06, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x03, // set zone 1 (1, 3)
  121. 0xb1, 0x64, 0x00, 0xb1, 0x65, 0x00, 0xb1, 0x06, 0x60, // per-note pbrange (custom)
  122. 0xb0, 0x64, 0x00, 0xb0, 0x65, 0x00, 0xb0, 0x06, 0x00, // master pbrange (custom)
  123. 0xb6, 0x64, 0x06, 0xb6, 0x65, 0x00, 0xb6, 0x06, 0x04, // set zone 2 (6, 4)
  124. 0xb6, 0x64, 0x00, 0xb6, 0x65, 0x00, 0xb6, 0x06, 0x30, // per-note pbrange (default = 48)
  125. 0xb5, 0x64, 0x00, 0xb5, 0x65, 0x00, 0xb5, 0x06, 0x02 // master pbrange (default = 2)
  126. };
  127. testMidiBuffer (buffer, expectedBytes, sizeof (expectedBytes));
  128. }
  129. }
  130. private:
  131. //==============================================================================
  132. void testMidiBuffer (MidiBuffer& buffer, const uint8* expectedBytes, int expectedBytesSize)
  133. {
  134. uint8 actualBytes[128] = { 0 };
  135. extractRawBinaryData (buffer, actualBytes, sizeof (actualBytes));
  136. expectEquals (std::memcmp (actualBytes, expectedBytes, (std::size_t) expectedBytesSize), 0);
  137. }
  138. //==============================================================================
  139. void extractRawBinaryData (const MidiBuffer& midiBuffer, const uint8* bufferToCopyTo, std::size_t maxBytes)
  140. {
  141. std::size_t pos = 0;
  142. MidiBuffer::Iterator iter (midiBuffer);
  143. MidiMessage midiMessage;
  144. int samplePosition; // Note: not actually used, so no need to initialise.
  145. while (iter.getNextEvent (midiMessage, samplePosition))
  146. {
  147. const uint8* data = midiMessage.getRawData();
  148. std::size_t dataSize = (std::size_t) midiMessage.getRawDataSize();
  149. if (pos + dataSize > maxBytes)
  150. return;
  151. std::memcpy ((void*) (bufferToCopyTo + pos), data, dataSize);
  152. pos += dataSize;
  153. }
  154. }
  155. };
  156. static MPEMessagesTests MPEMessagesUnitTests;
  157. #endif // JUCE_UNIT_TESTS