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.

195 lines
8.8KB

  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. */
  103. void setMinimumRenderingSubdivisionSize (int numSamples) noexcept;
  104. //==============================================================================
  105. /** Puts the synthesiser into legacy mode.
  106. @param pitchbendRange The note pitchbend range in semitones to use when in legacy mode.
  107. Must be between 0 and 96, otherwise behaviour is undefined.
  108. The default pitchbend range in legacy mode is +/- 2 semitones.
  109. @param channelRange The range of MIDI channels to use for notes when in legacy mode.
  110. The default is to use all MIDI channels (1-16).
  111. To get out of legacy mode, set a new MPE zone layout using setZoneLayout.
  112. */
  113. void enableLegacyMode (int pitchbendRange = 2,
  114. Range<int> channelRange = Range<int> (1, 17));
  115. /** Returns true if the instrument is in legacy mode, false otherwise. */
  116. bool isLegacyModeEnabled() const noexcept;
  117. /** Returns the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  118. Range<int> getLegacyModeChannelRange() const noexcept;
  119. /** Re-sets the range of MIDI channels (1-16) to be used for notes when in legacy mode. */
  120. void setLegacyModeChannelRange (Range<int> channelRange);
  121. /** Returns the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  122. int getLegacyModePitchbendRange() const noexcept;
  123. /** Re-sets the pitchbend range in semitones (0-96) to be used for notes when in legacy mode. */
  124. void setLegacyModePitchbendRange (int pitchbendRange);
  125. protected:
  126. //==============================================================================
  127. /** Implement this method to render your audio inside.
  128. @see renderNextBlock
  129. */
  130. virtual void renderNextSubBlock (AudioBuffer<float>& outputAudio,
  131. int startSample,
  132. int numSamples) = 0;
  133. /** Implement this method if you want to render 64-bit audio as well;
  134. otherwise leave blank.
  135. */
  136. virtual void renderNextSubBlock (AudioBuffer<double>& /*outputAudio*/,
  137. int /*startSample*/,
  138. int /*numSamples*/) {}
  139. protected:
  140. //==============================================================================
  141. /** @internal */
  142. ScopedPointer<MPEInstrument> instrument;
  143. /** @internal */
  144. CriticalSection renderAudioLock;
  145. private:
  146. //==============================================================================
  147. double sampleRate;
  148. int minimumSubBlockSize;
  149. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MPESynthesiserBase)
  150. };
  151. #endif // JUCE_MPESynthesiserBase_H_INCLUDED