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.

AudioProcessor.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2015 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the GNU
  7. General Public License as published by the Free Software Foundation;
  8. either version 2 of the License, or any later version.
  9. This program is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. For a full copy of the GNU General Public License see the doc/GPL.txt file.
  13. ==============================================================================
  14. */
  15. #ifndef WATER_AUDIOPROCESSOR_H_INCLUDED
  16. #define WATER_AUDIOPROCESSOR_H_INCLUDED
  17. #include "../text/String.h"
  18. #include "../buffers/AudioSampleBuffer.h"
  19. #include "CarlaMutex.hpp"
  20. namespace water {
  21. //==============================================================================
  22. /**
  23. Base class for audio processing filters or plugins.
  24. This is intended to act as a base class of audio filter that is general enough to
  25. be wrapped as a VST, AU, RTAS, etc, or used internally.
  26. It is also used by the plugin hosting code as the wrapper around an instance
  27. of a loaded plugin.
  28. Derive your filter class from this base class, and if you're building a plugin,
  29. you should implement a global function called createPluginFilter() which creates
  30. and returns a new instance of your subclass.
  31. */
  32. class AudioProcessor
  33. {
  34. protected:
  35. //==============================================================================
  36. /** Constructor.
  37. This constructor will create a main input and output bus which are diabled
  38. by default. If you need more fine grain control then use the other
  39. constructors.
  40. */
  41. AudioProcessor();
  42. public:
  43. enum ChannelType {
  44. ChannelTypeAudio,
  45. ChannelTypeCV,
  46. ChannelTypeMIDI,
  47. };
  48. //==============================================================================
  49. /** Destructor. */
  50. virtual ~AudioProcessor();
  51. //==============================================================================
  52. /** Returns the name of this processor. */
  53. virtual const String getName() const = 0;
  54. //==============================================================================
  55. /** Called before playback starts, to let the filter prepare itself.
  56. The sample rate is the target sample rate, and will remain constant until
  57. playback stops.
  58. You can call getTotalNumInputChannels and getTotalNumOutputChannels
  59. or query the busLayout member variable to find out the number of
  60. channels your processBlock callback must process.
  61. The maximumExpectedSamplesPerBlock value is a strong hint about the maximum
  62. number of samples that will be provided in each block. You may want to use
  63. this value to resize internal buffers. You should program defensively in
  64. case a buggy host exceeds this value. The actual block sizes that the host
  65. uses may be different each time the callback happens: completely variable
  66. block sizes can be expected from some hosts.
  67. @see busLayout, getTotalNumInputChannels, getTotalNumOutputChannels
  68. */
  69. virtual void prepareToPlay (double sampleRate,
  70. int maximumExpectedSamplesPerBlock) = 0;
  71. /** Called after playback has stopped, to let the filter free up any resources it
  72. no longer needs.
  73. */
  74. virtual void releaseResources() = 0;
  75. /** Renders the next block.
  76. When this method is called, the buffer contains a number of channels which is
  77. at least as great as the maximum number of input and output channels that
  78. this filter is using. It will be filled with the filter's input data and
  79. should be replaced with the filter's output.
  80. So for example if your filter has a total of 2 input channels and 4 output
  81. channels, then the buffer will contain 4 channels, the first two being filled
  82. with the input data. Your filter should read these, do its processing, and
  83. replace the contents of all 4 channels with its output.
  84. Or if your filter has a total of 5 inputs and 2 outputs, the buffer will have 5
  85. channels, all filled with data, and your filter should overwrite the first 2 of
  86. these with its output. But be VERY careful not to write anything to the last 3
  87. channels, as these might be mapped to memory that the host assumes is read-only!
  88. If your plug-in has more than one input or output buses then the buffer passed
  89. to the processBlock methods will contain a bundle of all channels of each bus.
  90. Use AudiobusLayout::getBusBuffer to obtain an audio buffer for a
  91. particular bus.
  92. Note that if you have more outputs than inputs, then only those channels that
  93. correspond to an input channel are guaranteed to contain sensible data - e.g.
  94. in the case of 2 inputs and 4 outputs, the first two channels contain the input,
  95. but the last two channels may contain garbage, so you should be careful not to
  96. let this pass through without being overwritten or cleared.
  97. Also note that the buffer may have more channels than are strictly necessary,
  98. but you should only read/write from the ones that your filter is supposed to
  99. be using.
  100. The number of samples in these buffers is NOT guaranteed to be the same for every
  101. callback, and may be more or less than the estimated value given to prepareToPlay().
  102. Your code must be able to cope with variable-sized blocks, or you're going to get
  103. clicks and crashes!
  104. Also note that some hosts will occasionally decide to pass a buffer containing
  105. zero samples, so make sure that your algorithm can deal with that!
  106. If the filter is receiving a midi input, then the midiMessages array will be filled
  107. with the midi messages for this block. Each message's timestamp will indicate the
  108. message's time, as a number of samples from the start of the block.
  109. Any messages left in the midi buffer when this method has finished are assumed to
  110. be the filter's midi output. This means that your filter should be careful to
  111. clear any incoming messages from the array if it doesn't want them to be passed-on.
  112. Be very careful about what you do in this callback - it's going to be called by
  113. the audio thread, so any kind of interaction with the UI is absolutely
  114. out of the question. If you change a parameter in here and need to tell your UI to
  115. update itself, the best way is probably to inherit from a ChangeBroadcaster, let
  116. the UI components register as listeners, and then call sendChangeMessage() inside the
  117. processBlock() method to send out an asynchronous message. You could also use
  118. the AsyncUpdater class in a similar way.
  119. @see AudiobusLayout::getBusBuffer
  120. */
  121. virtual void processBlockWithCV (AudioSampleBuffer& audioBuffer,
  122. const AudioSampleBuffer& cvInBuffer,
  123. AudioSampleBuffer& cvOutBuffer,
  124. MidiBuffer& midiMessages) = 0;
  125. //==============================================================================
  126. /** Returns the total number of input channels. */
  127. uint getTotalNumInputChannels(ChannelType t) const noexcept;
  128. /** Returns the total number of output channels. */
  129. uint getTotalNumOutputChannels(ChannelType t) const noexcept;
  130. //==============================================================================
  131. /** Returns the current sample rate.
  132. This can be called from your processBlock() method - it's not guaranteed
  133. to be valid at any other time, and may return 0 if it's unknown.
  134. */
  135. double getSampleRate() const noexcept { return currentSampleRate; }
  136. /** Returns the current typical block size that is being used.
  137. This can be called from your processBlock() method - it's not guaranteed
  138. to be valid at any other time.
  139. Remember it's not the ONLY block size that may be used when calling
  140. processBlock, it's just the normal one. The actual block sizes used may be
  141. larger or smaller than this, and will vary between successive calls.
  142. */
  143. int getBlockSize() const noexcept { return blockSize; }
  144. //==============================================================================
  145. /** This returns the number of samples delay that the filter imposes on the audio
  146. passing through it.
  147. The host will call this to find the latency - the filter itself should set this value
  148. by calling setLatencySamples() as soon as it can during its initialisation.
  149. */
  150. int getLatencySamples() const noexcept { return latencySamples; }
  151. /** The filter should call this to set the number of samples delay that it introduces.
  152. The filter should call this as soon as it can during initialisation, and can call it
  153. later if the value changes.
  154. */
  155. void setLatencySamples (int newLatency);
  156. /** Returns true if the processor wants midi messages. */
  157. virtual bool acceptsMidi() const = 0;
  158. /** Returns true if the processor produces midi messages. */
  159. virtual bool producesMidi() const = 0;
  160. /** Returns true if the processor supports MPE. */
  161. virtual bool supportsMPE() const { return false; }
  162. virtual const String getInputChannelName (ChannelType, uint) const;
  163. virtual const String getOutputChannelName (ChannelType, uint) const;
  164. //==============================================================================
  165. /** This returns a critical section that will automatically be locked while the host
  166. is calling the processBlock() method.
  167. Use it from your UI or other threads to lock access to variables that are used
  168. by the process callback, but obviously be careful not to keep it locked for
  169. too long, because that could cause stuttering playback. If you need to do something
  170. that'll take a long time and need the processing to stop while it happens, use the
  171. suspendProcessing() method instead.
  172. @see suspendProcessing
  173. */
  174. const CarlaRecursiveMutex& getCallbackLock() const noexcept { return callbackLock; }
  175. /** Enables and disables the processing callback.
  176. If you need to do something time-consuming on a thread and would like to make sure
  177. the audio processing callback doesn't happen until you've finished, use this
  178. to disable the callback and re-enable it again afterwards.
  179. E.g.
  180. @code
  181. void loadNewPatch()
  182. {
  183. suspendProcessing (true);
  184. ..do something that takes ages..
  185. suspendProcessing (false);
  186. }
  187. @endcode
  188. If the host tries to make an audio callback while processing is suspended, the
  189. filter will return an empty buffer, but won't block the audio thread like it would
  190. do if you use the getCallbackLock() critical section to synchronise access.
  191. Any code that calls processBlock() should call isSuspended() before doing so, and
  192. if the processor is suspended, it should avoid the call and emit silence or
  193. whatever is appropriate.
  194. @see getCallbackLock
  195. */
  196. void suspendProcessing (bool shouldBeSuspended);
  197. /** Returns true if processing is currently suspended.
  198. @see suspendProcessing
  199. */
  200. bool isSuspended() const noexcept { return suspended; }
  201. /** A plugin can override this to be told when it should reset any playing voices.
  202. The default implementation does nothing, but a host may call this to tell the
  203. plugin that it should stop any tails or sounds that have been left running.
  204. */
  205. virtual void reset();
  206. /** A plugin can override this to be told when it should reconfigure itself.
  207. The default implementation does nothing, but a host may call this to tell the
  208. plugin that it should call setPlayConfigDetails again.
  209. */
  210. virtual void reconfigure();
  211. //==============================================================================
  212. /** Returns true if the processor is being run in an offline mode for rendering.
  213. If the processor is being run live on realtime signals, this returns false.
  214. If the mode is unknown, this will assume it's realtime and return false.
  215. This value may be unreliable until the prepareToPlay() method has been called,
  216. and could change each time prepareToPlay() is called.
  217. @see setNonRealtime()
  218. */
  219. bool isNonRealtime() const noexcept { return nonRealtime; }
  220. /** Called by the host to tell this processor whether it's being used in a non-realtime
  221. capacity for offline rendering or bouncing.
  222. */
  223. virtual void setNonRealtime (bool isNonRealtime) noexcept;
  224. //==============================================================================
  225. /** This is called by the processor to specify its details before being played. Use this
  226. version of the function if you are not interested in any sidechain and/or aux buses
  227. and do not care about the layout of channels. Otherwise use setRateAndBufferSizeDetails.*/
  228. void setPlayConfigDetails (uint numAudioIns, uint numAudioOuts,
  229. uint numCVIns, uint numCVOuts,
  230. uint numMIDIIns, uint numMIDIOuts,
  231. double sampleRate, int blockSize);
  232. /** This is called by the processor to specify its details before being played. You
  233. should call this function after having informed the processor about the channel
  234. and bus layouts via setBusesLayout.
  235. @see setBusesLayout
  236. */
  237. void setRateAndBufferSizeDetails (double sampleRate, int blockSize) noexcept;
  238. private:
  239. //==============================================================================
  240. double currentSampleRate;
  241. int blockSize, latencySamples;
  242. bool suspended, nonRealtime;
  243. CarlaRecursiveMutex callbackLock;
  244. uint numAudioIns, numAudioOuts;
  245. uint numCVIns, numCVOuts;
  246. uint numMIDIIns, numMIDIOuts;
  247. CARLA_DECLARE_NON_COPYABLE (AudioProcessor)
  248. };
  249. }
  250. #endif // WATER_AUDIOPROCESSOR_H_INCLUDED