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.

242 lines
9.3KB

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