The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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