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.

226 lines
8.6KB

  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. //==============================================================================
  20. /**
  21. This class represents the current MPE zone layout of a device capable of handling MPE.
  22. An MPE device can have up to two zones: a lower zone with master channel 1 and
  23. allocated MIDI channels increasing from channel 2, and an upper zone with master
  24. channel 16 and allocated MIDI channels decreasing from channel 15. MPE mode is
  25. enabled on a device when one of these zones is active and disabled when both
  26. are inactive.
  27. Use the MPEMessages helper class to convert the zone layout represented
  28. by this object to MIDI message sequences that you can send to an Expressive
  29. MIDI device to set its zone layout, add zones etc.
  30. @see MPEInstrument
  31. @tags{Audio}
  32. */
  33. class JUCE_API MPEZoneLayout
  34. {
  35. public:
  36. /** Default constructor.
  37. This will create a layout with inactive lower and upper zones, representing
  38. a device with MPE mode disabled.
  39. You can set the lower or upper MPE zones using the setZone() method.
  40. @see setZone
  41. */
  42. MPEZoneLayout() noexcept;
  43. /** Copy constuctor.
  44. This will not copy the listeners registered to the MPEZoneLayout.
  45. */
  46. MPEZoneLayout (const MPEZoneLayout& other);
  47. /** Copy assignment operator.
  48. This will not copy the listeners registered to the MPEZoneLayout.
  49. */
  50. MPEZoneLayout& operator= (const MPEZoneLayout& other);
  51. //==============================================================================
  52. /**
  53. This struct represents an MPE zone.
  54. It can either be a lower or an upper zone, where:
  55. - A lower zone encompasses master channel 1 and an arbitrary number of ascending
  56. MIDI channels, increasing from channel 2.
  57. - An upper zone encompasses master channel 16 and an arbitrary number of descending
  58. MIDI channels, decreasing from channel 15.
  59. It also defines a pitchbend range (in semitones) to be applied for per-note pitchbends and
  60. master pitchbends, respectively.
  61. */
  62. struct Zone
  63. {
  64. Zone (const Zone& other) = default;
  65. bool isLowerZone() const noexcept { return lowerZone; }
  66. bool isUpperZone() const noexcept { return ! lowerZone; }
  67. bool isActive() const noexcept { return numMemberChannels > 0; }
  68. int getMasterChannel() const noexcept { return lowerZone ? 1 : 16; }
  69. int getFirstMemberChannel() const noexcept { return lowerZone ? 2 : 15; }
  70. int getLastMemberChannel() const noexcept { return lowerZone ? (1 + numMemberChannels)
  71. : (16 - numMemberChannels); }
  72. bool isUsingChannelAsMemberChannel (int channel) const noexcept
  73. {
  74. return lowerZone ? (channel > 1 && channel <= 1 + numMemberChannels)
  75. : (channel < 16 && channel >= 16 - numMemberChannels);
  76. }
  77. bool isUsing (int channel) const noexcept
  78. {
  79. return isUsingChannelAsMemberChannel (channel) || channel == getMasterChannel();
  80. }
  81. bool operator== (const Zone& other) const noexcept { return lowerZone == other.lowerZone
  82. && numMemberChannels == other.numMemberChannels
  83. && perNotePitchbendRange == other.perNotePitchbendRange
  84. && masterPitchbendRange == other.masterPitchbendRange; }
  85. bool operator!= (const Zone& other) const noexcept { return ! operator== (other); }
  86. int numMemberChannels;
  87. int perNotePitchbendRange;
  88. int masterPitchbendRange;
  89. private:
  90. friend class MPEZoneLayout;
  91. Zone (bool lower, int memberChans = 0, int perNotePb = 48, int masterPb = 2) noexcept
  92. : numMemberChannels (memberChans),
  93. perNotePitchbendRange (perNotePb),
  94. masterPitchbendRange (masterPb),
  95. lowerZone (lower)
  96. {
  97. }
  98. bool lowerZone;
  99. };
  100. /** Sets the lower zone of this layout. */
  101. void setLowerZone (int numMemberChannels = 0,
  102. int perNotePitchbendRange = 48,
  103. int masterPitchbendRange = 2) noexcept;
  104. /** Sets the upper zone of this layout. */
  105. void setUpperZone (int numMemberChannels = 0,
  106. int perNotePitchbendRange = 48,
  107. int masterPitchbendRange = 2) noexcept;
  108. /** Returns a struct representing the lower MPE zone. */
  109. const Zone getLowerZone() const noexcept { return lowerZone; }
  110. /** Returns a struct representing the upper MPE zone. */
  111. const Zone getUpperZone() const noexcept { return upperZone; }
  112. /** Clears the lower and upper zones of this layout, making them both inactive
  113. and disabling MPE mode.
  114. */
  115. void clearAllZones();
  116. //==============================================================================
  117. /** Pass incoming MIDI messages to an object of this class if you want the
  118. zone layout to properly react to MPE RPN messages like an
  119. MPE device.
  120. MPEMessages::rpnNumber will add or remove zones; RPN 0 will
  121. set the per-note or master pitchbend ranges.
  122. Any other MIDI messages will be ignored by this class.
  123. @see MPEMessages
  124. */
  125. void processNextMidiEvent (const MidiMessage& message);
  126. /** Pass incoming MIDI buffers to an object of this class if you want the
  127. zone layout to properly react to MPE RPN messages like an
  128. MPE device.
  129. MPEMessages::rpnNumber will add or remove zones; RPN 0 will
  130. set the per-note or master pitchbend ranges.
  131. Any other MIDI messages will be ignored by this class.
  132. @see MPEMessages
  133. */
  134. void processNextMidiBuffer (const MidiBuffer& buffer);
  135. //==============================================================================
  136. /** Listener class. Derive from this class to allow your class to be
  137. notified about changes to the zone layout.
  138. */
  139. class Listener
  140. {
  141. public:
  142. /** Destructor. */
  143. virtual ~Listener() = default;
  144. /** Implement this callback to be notified about any changes to this
  145. MPEZoneLayout. Will be called whenever a zone is added, zones are
  146. removed, or any zone's master or note pitchbend ranges change.
  147. */
  148. virtual void zoneLayoutChanged (const MPEZoneLayout& layout) = 0;
  149. };
  150. //==============================================================================
  151. /** Adds a listener. */
  152. void addListener (Listener* const listenerToAdd) noexcept;
  153. /** Removes a listener. */
  154. void removeListener (Listener* const listenerToRemove) noexcept;
  155. private:
  156. //==============================================================================
  157. Zone lowerZone { true, 0 };
  158. Zone upperZone { false, 0 };
  159. MidiRPNDetector rpnDetector;
  160. ListenerList<Listener> listeners;
  161. //==============================================================================
  162. void setZone (bool, int, int, int) noexcept;
  163. void processRpnMessage (MidiRPNMessage);
  164. void processZoneLayoutRpnMessage (MidiRPNMessage);
  165. void processPitchbendRangeRpnMessage (MidiRPNMessage);
  166. void updateMasterPitchbend (Zone&, int);
  167. void updatePerNotePitchbendRange (Zone&, int);
  168. void sendLayoutChangeMessage();
  169. void checkAndLimitZoneParameters (int, int, int&) noexcept;
  170. };
  171. } // namespace juce