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.

201 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MPESynthesiserBase_H_INCLUDED
  18. #define JUCE_MPESynthesiserBase_H_INCLUDED
  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. */
  34. struct JUCE_API MPESynthesiserBase : public MPEInstrument::Listener
  35. {
  36. public:
  37. //==============================================================================
  38. /** Constructor. */
  39. MPESynthesiserBase();
  40. /** Constructor.
  41. If you use this constructor, the synthesiser will take ownership of the
  42. provided instrument object, and will use it internally to handle the
  43. 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. processNextSubBlock 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. */
  75. template <typename floatType>
  76. void renderNextBlock (AudioBuffer<floatType>& outputAudio,
  77. const MidiBuffer& inputMidi,
  78. int startSample,
  79. int numSamples);
  80. //==============================================================================
  81. /** Handle incoming MIDI events (called from renderNextBlock).
  82. The default implementation provided here simply forwards everything
  83. to MPEInstrument::processNextMidiEvent, where it is used to update the
  84. MPE notes, zones etc. MIDI messages not relevant for MPE are ignored.
  85. This method can be overridden if you need to do custom MIDI handling
  86. on top of MPE. The MPESynthesiser class overrides this to implement
  87. callbacks for MIDI program changes and non-MPE-related MIDI controller
  88. messages.
  89. */
  90. virtual void handleMidiEvent (const MidiMessage&);
  91. //==============================================================================
  92. /** Sets a minimum limit on the size to which audio sub-blocks will be divided when rendering.
  93. When rendering, the audio blocks that are passed into renderNextBlock() will be split up
  94. into smaller blocks that lie between all the incoming midi messages, and it is these smaller
  95. sub-blocks that are rendered with multiple calls to renderVoices().
  96. Obviously in a pathological case where there are midi messages on every sample, then
  97. renderVoices() could be called once per sample and lead to poor performance, so this
  98. setting allows you to set a lower limit on the block size.
  99. The default setting is 32, which means that midi messages are accurate to about < 1ms
  100. accuracy, which is probably fine for most purposes, but you may want to increase or
  101. decrease this value for your synth.
  102. If shouldBeStrict is true, the audio sub-blocks will strictly never be smaller than numSamples.
  103. If shouldBeStrict is false (default), the first audio sub-block in the buffer is allowed
  104. to be smaller, to make sure that the first MIDI event in a buffer will always be sample-accurate
  105. (this can sometimes help to avoid quantisation or phasing issues).
  106. */
  107. void setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict = false) noexcept;
  108. //==============================================================================
  109. /** Puts the synthesiser into legacy mode.
  110. @param pitchbendRange The note pitchbend range in semitones to use when in legacy mode.
  111. Must be between 0 and 96, otherwise behaviour is undefined.
  112. The default pitchbend range in legacy mode is +/- 2 semitones.
  113. @param channelRange The range of MIDI channels to use for notes when in legacy mode.
  114. The default is to use all MIDI channels (1-16).
  115. To get out of legacy mode, set a new MPE zone layout using setZoneLayout.
  116. */
  117. void enableLegacyMode (int pitchbendRange = 2,
  118. Range<int> channelRange = Range<int> (1, 17));
  119. /** Returns true if the instrument is in legacy mode, false otherwise. */
  120. bool isLegacyModeEnabled() const noexcept;
  121. /** Returns the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  122. Range<int> getLegacyModeChannelRange() const noexcept;
  123. /** Re-sets the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  124. void setLegacyModeChannelRange (Range<int> channelRange);
  125. /** Returns the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  126. int getLegacyModePitchbendRange() const noexcept;
  127. /** Re-sets the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  128. void setLegacyModePitchbendRange (int pitchbendRange);
  129. protected:
  130. //==============================================================================
  131. /** Implement this method to render your audio inside.
  132. @see renderNextBlock
  133. */
  134. virtual void renderNextSubBlock (AudioBuffer<float>& outputAudio,
  135. int startSample,
  136. int numSamples) = 0;
  137. /** Implement this method if you want to render 64-bit audio as well;
  138. otherwise leave blank.
  139. */
  140. virtual void renderNextSubBlock (AudioBuffer<double>& /*outputAudio*/,
  141. int /*startSample*/,
  142. int /*numSamples*/) {}
  143. protected:
  144. //==============================================================================
  145. /** @internal */
  146. ScopedPointer<MPEInstrument> instrument;
  147. private:
  148. //==============================================================================
  149. CriticalSection noteStateLock;
  150. double sampleRate;
  151. int minimumSubBlockSize;
  152. bool subBlockSubdivisionIsStrict;
  153. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MPESynthesiserBase)
  154. };
  155. #endif // JUCE_MPESynthesiserBase_H_INCLUDED