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.

217 lines
9.9KB

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