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.

201 lines
7.5KB

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