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.

211 lines
9.6KB

  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. //==============================================================================
  20. /**
  21. Derive from this class to create a basic audio generator capable of MPE.
  22. Implement the callbacks of MPEInstrument::Listener (noteAdded, notePressureChanged
  23. etc.) to let your audio generator know that MPE notes were triggered, modulated,
  24. or released. What to do inside them, and how that influences your audio generator,
  25. is up to you!
  26. This class uses an instance of MPEInstrument internally to handle the MPE
  27. note state logic.
  28. This class is a very low-level base class for an MPE instrument. If you need
  29. something more sophisticated, have a look at MPESynthesiser. This class extends
  30. MPESynthesiserBase by adding the concept of voices that can play notes,
  31. a voice stealing algorithm, and much more.
  32. @see MPESynthesiser, MPEInstrument
  33. @tags{Audio}
  34. */
  35. struct JUCE_API MPESynthesiserBase : public MPEInstrument::Listener
  36. {
  37. public:
  38. //==============================================================================
  39. /** Constructor. */
  40. MPESynthesiserBase();
  41. /** Constructor.
  42. If you use this constructor, the synthesiser will take ownership of the
  43. provided instrument object, and will use it internally to handle the
  44. MPE note state logic.
  45. This is useful if you want to use an instance of your own class derived
  46. from MPEInstrument for the MPE logic.
  47. */
  48. MPESynthesiserBase (MPEInstrument* instrument);
  49. //==============================================================================
  50. /** Returns the synthesiser's internal MPE zone layout.
  51. This happens by value, to enforce thread-safety and class invariants.
  52. */
  53. MPEZoneLayout getZoneLayout() const noexcept;
  54. /** Re-sets the synthesiser's internal MPE zone layout to the one passed in.
  55. As a side effect, this will discard all currently playing notes,
  56. call noteReleased for all of them, and disable legacy mode (if previously enabled).
  57. */
  58. void setZoneLayout (MPEZoneLayout newLayout);
  59. //==============================================================================
  60. /** Tells the synthesiser what the sample rate is for the audio it's being
  61. used to render.
  62. */
  63. virtual void setCurrentPlaybackSampleRate (double sampleRate);
  64. /** Returns the current target sample rate at which rendering is being done.
  65. Subclasses may need to know this so that they can pitch things correctly.
  66. */
  67. double getSampleRate() const noexcept { return sampleRate; }
  68. //==============================================================================
  69. /** Creates the next block of audio output.
  70. Call this to make sound. This will chop up the AudioBuffer into subBlock
  71. pieces separated by events in the MIDI buffer, and then call
  72. processNextSubBlock on each one of them. In between you will get calls
  73. to noteAdded/Changed/Finished, where you can update parameters that
  74. depend on those notes to use for your audio rendering.
  75. */
  76. template <typename floatType>
  77. void renderNextBlock (AudioBuffer<floatType>& outputAudio,
  78. const MidiBuffer& inputMidi,
  79. int startSample,
  80. int numSamples);
  81. //==============================================================================
  82. /** Handle incoming MIDI events (called from renderNextBlock).
  83. The default implementation provided here simply forwards everything
  84. to MPEInstrument::processNextMidiEvent, where it is used to update the
  85. MPE notes, zones etc. MIDI messages not relevant for MPE are ignored.
  86. This method can be overridden if you need to do custom MIDI handling
  87. on top of MPE. The MPESynthesiser class overrides this to implement
  88. callbacks for MIDI program changes and non-MPE-related MIDI controller
  89. messages.
  90. */
  91. virtual void handleMidiEvent (const MidiMessage&);
  92. //==============================================================================
  93. /** Sets a minimum limit on the size to which audio sub-blocks will be divided when rendering.
  94. When rendering, the audio blocks that are passed into renderNextBlock() will be split up
  95. into smaller blocks that lie between all the incoming midi messages, and it is these smaller
  96. sub-blocks that are rendered with multiple calls to renderVoices().
  97. Obviously in a pathological case where there are midi messages on every sample, then
  98. renderVoices() could be called once per sample and lead to poor performance, so this
  99. setting allows you to set a lower limit on the block size.
  100. The default setting is 32, which means that midi messages are accurate to about < 1ms
  101. accuracy, which is probably fine for most purposes, but you may want to increase or
  102. decrease this value for your synth.
  103. If shouldBeStrict is true, the audio sub-blocks will strictly never be smaller than numSamples.
  104. If shouldBeStrict is false (default), the first audio sub-block in the buffer is allowed
  105. to be smaller, to make sure that the first MIDI event in a buffer will always be sample-accurate
  106. (this can sometimes help to avoid quantisation or phasing issues).
  107. */
  108. void setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict = false) noexcept;
  109. //==============================================================================
  110. /** Puts the synthesiser into legacy mode.
  111. @param pitchbendRange The note pitchbend range in semitones to use when in legacy mode.
  112. Must be between 0 and 96, otherwise behaviour is undefined.
  113. The default pitchbend range in legacy mode is +/- 2 semitones.
  114. @param channelRange The range of MIDI channels to use for notes when in legacy mode.
  115. The default is to use all MIDI channels (1-16).
  116. To get out of legacy mode, set a new MPE zone layout using setZoneLayout.
  117. */
  118. void enableLegacyMode (int pitchbendRange = 2,
  119. Range<int> channelRange = Range<int> (1, 17));
  120. /** Returns true if the instrument is in legacy mode, false otherwise. */
  121. bool isLegacyModeEnabled() const noexcept;
  122. /** Returns the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  123. Range<int> getLegacyModeChannelRange() const noexcept;
  124. /** Re-sets the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  125. void setLegacyModeChannelRange (Range<int> channelRange);
  126. /** Returns the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  127. int getLegacyModePitchbendRange() const noexcept;
  128. /** Re-sets the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  129. void setLegacyModePitchbendRange (int pitchbendRange);
  130. //==============================================================================
  131. using TrackingMode = MPEInstrument::TrackingMode;
  132. /** Set the MPE tracking mode for the pressure dimension. */
  133. void setPressureTrackingMode (TrackingMode modeToUse);
  134. /** Set the MPE tracking mode for the pitchbend dimension. */
  135. void setPitchbendTrackingMode (TrackingMode modeToUse);
  136. /** Set the MPE tracking mode for the timbre dimension. */
  137. void setTimbreTrackingMode (TrackingMode modeToUse);
  138. protected:
  139. //==============================================================================
  140. /** Implement this method to render your audio inside.
  141. @see renderNextBlock
  142. */
  143. virtual void renderNextSubBlock (AudioBuffer<float>& outputAudio,
  144. int startSample,
  145. int numSamples) = 0;
  146. /** Implement this method if you want to render 64-bit audio as well;
  147. otherwise leave blank.
  148. */
  149. virtual void renderNextSubBlock (AudioBuffer<double>& /*outputAudio*/,
  150. int /*startSample*/,
  151. int /*numSamples*/) {}
  152. protected:
  153. //==============================================================================
  154. /** @internal */
  155. std::unique_ptr<MPEInstrument> instrument;
  156. private:
  157. //==============================================================================
  158. CriticalSection noteStateLock;
  159. double sampleRate = 0.0;
  160. int minimumSubBlockSize = 32;
  161. bool subBlockSubdivisionIsStrict = false;
  162. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MPESynthesiserBase)
  163. };
  164. } // namespace juce