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.

juce_AudioProcessor.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_AUDIOPROCESSOR_H_INCLUDED
  18. #define JUCE_AUDIOPROCESSOR_H_INCLUDED
  19. // #include "juce_AudioPlayHead.h"
  20. //==============================================================================
  21. /**
  22. Base class for audio processing filters or plugins.
  23. This is intended to act as a base class of audio filter that is general enough to
  24. be wrapped as a VST, AU, RTAS, etc, or used internally.
  25. It is also used by the plugin hosting code as the wrapper around an instance
  26. of a loaded plugin.
  27. Derive your filter class from this base class, and if you're building a plugin,
  28. you should implement a global function called createPluginFilter() which creates
  29. and returns a new instance of your subclass.
  30. */
  31. class JUCE_API AudioProcessor
  32. {
  33. protected:
  34. //==============================================================================
  35. /** Constructor.
  36. This constructor will create a main input and output bus which are diabled
  37. by default. If you need more fine grain control then use the other
  38. constructors.
  39. */
  40. AudioProcessor();
  41. public:
  42. //==============================================================================
  43. /** Destructor. */
  44. virtual ~AudioProcessor();
  45. //==============================================================================
  46. /** Returns the name of this processor. */
  47. virtual const String getName() const = 0;
  48. //==============================================================================
  49. /** Called before playback starts, to let the filter prepare itself.
  50. The sample rate is the target sample rate, and will remain constant until
  51. playback stops.
  52. You can call getTotalNumInputChannels and getTotalNumOutputChannels
  53. or query the busLayout member variable to find out the number of
  54. channels your processBlock callback must process.
  55. The maximumExpectedSamplesPerBlock value is a strong hint about the maximum
  56. number of samples that will be provided in each block. You may want to use
  57. this value to resize internal buffers. You should program defensively in
  58. case a buggy host exceeds this value. The actual block sizes that the host
  59. uses may be different each time the callback happens: completely variable
  60. block sizes can be expected from some hosts.
  61. @see busLayout, getTotalNumInputChannels, getTotalNumOutputChannels
  62. */
  63. virtual void prepareToPlay (double sampleRate,
  64. int maximumExpectedSamplesPerBlock) = 0;
  65. /** Called after playback has stopped, to let the filter free up any resources it
  66. no longer needs.
  67. */
  68. virtual void releaseResources() = 0;
  69. /** Renders the next block.
  70. When this method is called, the buffer contains a number of channels which is
  71. at least as great as the maximum number of input and output channels that
  72. this filter is using. It will be filled with the filter's input data and
  73. should be replaced with the filter's output.
  74. So for example if your filter has a total of 2 input channels and 4 output
  75. channels, then the buffer will contain 4 channels, the first two being filled
  76. with the input data. Your filter should read these, do its processing, and
  77. replace the contents of all 4 channels with its output.
  78. Or if your filter has a total of 5 inputs and 2 outputs, the buffer will have 5
  79. channels, all filled with data, and your filter should overwrite the first 2 of
  80. these with its output. But be VERY careful not to write anything to the last 3
  81. channels, as these might be mapped to memory that the host assumes is read-only!
  82. If your plug-in has more than one input or output buses then the buffer passed
  83. to the processBlock methods will contain a bundle of all channels of each bus.
  84. Use AudiobusLayout::getBusBuffer to obtain an audio buffer for a
  85. particular bus.
  86. Note that if you have more outputs than inputs, then only those channels that
  87. correspond to an input channel are guaranteed to contain sensible data - e.g.
  88. in the case of 2 inputs and 4 outputs, the first two channels contain the input,
  89. but the last two channels may contain garbage, so you should be careful not to
  90. let this pass through without being overwritten or cleared.
  91. Also note that the buffer may have more channels than are strictly necessary,
  92. but you should only read/write from the ones that your filter is supposed to
  93. be using.
  94. The number of samples in these buffers is NOT guaranteed to be the same for every
  95. callback, and may be more or less than the estimated value given to prepareToPlay().
  96. Your code must be able to cope with variable-sized blocks, or you're going to get
  97. clicks and crashes!
  98. Also note that some hosts will occasionally decide to pass a buffer containing
  99. zero samples, so make sure that your algorithm can deal with that!
  100. If the filter is receiving a midi input, then the midiMessages array will be filled
  101. with the midi messages for this block. Each message's timestamp will indicate the
  102. message's time, as a number of samples from the start of the block.
  103. Any messages left in the midi buffer when this method has finished are assumed to
  104. be the filter's midi output. This means that your filter should be careful to
  105. clear any incoming messages from the array if it doesn't want them to be passed-on.
  106. Be very careful about what you do in this callback - it's going to be called by
  107. the audio thread, so any kind of interaction with the UI is absolutely
  108. out of the question. If you change a parameter in here and need to tell your UI to
  109. update itself, the best way is probably to inherit from a ChangeBroadcaster, let
  110. the UI components register as listeners, and then call sendChangeMessage() inside the
  111. processBlock() method to send out an asynchronous message. You could also use
  112. the AsyncUpdater class in a similar way.
  113. @see AudiobusLayout::getBusBuffer
  114. */
  115. virtual void processBlock (AudioSampleBuffer& buffer,
  116. MidiBuffer& midiMessages) = 0;
  117. /** Renders the next block when the processor is being bypassed.
  118. The default implementation of this method will pass-through any incoming audio, but
  119. you may override this method e.g. to add latency compensation to the data to match
  120. the processor's latency characteristics. This will avoid situations where bypassing
  121. will shift the signal forward in time, possibly creating pre-echo effects and odd timings.
  122. Another use for this method would be to cross-fade or morph between the wet (not bypassed)
  123. and dry (bypassed) signals.
  124. */
  125. virtual void processBlockBypassed (AudioSampleBuffer& buffer,
  126. MidiBuffer& midiMessages);
  127. //==============================================================================
  128. /** Returns the current AudioPlayHead object that should be used to find
  129. out the state and position of the playhead.
  130. You can ONLY call this from your processBlock() method! Calling it at other
  131. times will produce undefined behaviour, as the host may not have any context
  132. in which a time would make sense, and some hosts will almost certainly have
  133. multithreading issues if it's not called on the audio thread.
  134. The AudioPlayHead object that is returned can be used to get the details about
  135. the time of the start of the block currently being processed. But do not
  136. store this pointer or use it outside of the current audio callback, because
  137. the host may delete or re-use it.
  138. If the host can't or won't provide any time info, this will return nullptr.
  139. */
  140. AudioPlayHead* getPlayHead() const noexcept { return playHead; }
  141. //==============================================================================
  142. /** Returns the total number of input channels.
  143. This method will return the total number of input channels by accumulating
  144. the number of channels on each input bus. The number of channels of the
  145. buffer passed to your processBlock callback will be equivalent to either
  146. getTotalNumInputChannels or getTotalNumOutputChannels - which ever
  147. is greater.
  148. Note that getTotalNumInputChannels is equivalent to
  149. getMainBusNumInputChannels if your processor does not have any sidechains
  150. or aux buses.
  151. */
  152. int getTotalNumInputChannels() const noexcept { return cachedTotalIns; }
  153. /** Returns the total number of output channels.
  154. This method will return the total number of output channels by accumulating
  155. the number of channels on each output bus. The number of channels of the
  156. buffer passed to your processBlock callback will be equivalent to either
  157. getTotalNumInputChannels or getTotalNumOutputChannels - which ever
  158. is greater.
  159. Note that getTotalNumOutputChannels is equivalent to
  160. getMainBusNumOutputChannels if your processor does not have any sidechains
  161. or aux buses.
  162. */
  163. int getTotalNumOutputChannels() const noexcept { return cachedTotalOuts; }
  164. //==============================================================================
  165. /** Returns the current sample rate.
  166. This can be called from your processBlock() method - it's not guaranteed
  167. to be valid at any other time, and may return 0 if it's unknown.
  168. */
  169. double getSampleRate() const noexcept { return currentSampleRate; }
  170. /** Returns the current typical block size that is being used.
  171. This can be called from your processBlock() method - it's not guaranteed
  172. to be valid at any other time.
  173. Remember it's not the ONLY block size that may be used when calling
  174. processBlock, it's just the normal one. The actual block sizes used may be
  175. larger or smaller than this, and will vary between successive calls.
  176. */
  177. int getBlockSize() const noexcept { return blockSize; }
  178. //==============================================================================
  179. /** This returns the number of samples delay that the filter imposes on the audio
  180. passing through it.
  181. The host will call this to find the latency - the filter itself should set this value
  182. by calling setLatencySamples() as soon as it can during its initialisation.
  183. */
  184. int getLatencySamples() const noexcept { return latencySamples; }
  185. /** The filter should call this to set the number of samples delay that it introduces.
  186. The filter should call this as soon as it can during initialisation, and can call it
  187. later if the value changes.
  188. */
  189. void setLatencySamples (int newLatency);
  190. /** Returns true if the processor wants midi messages. */
  191. virtual bool acceptsMidi() const = 0;
  192. /** Returns true if the processor produces midi messages. */
  193. virtual bool producesMidi() const = 0;
  194. /** Returns true if the processor supports MPE. */
  195. virtual bool supportsMPE() const { return false; }
  196. /** Returns true if this is a midi effect plug-in and does no audio processing. */
  197. virtual bool isMidiEffect() const { return false; }
  198. virtual const String getInputChannelName (int) const { return String(); }
  199. virtual const String getOutputChannelName (int) const { return String(); }
  200. //==============================================================================
  201. /** This returns a critical section that will automatically be locked while the host
  202. is calling the processBlock() method.
  203. Use it from your UI or other threads to lock access to variables that are used
  204. by the process callback, but obviously be careful not to keep it locked for
  205. too long, because that could cause stuttering playback. If you need to do something
  206. that'll take a long time and need the processing to stop while it happens, use the
  207. suspendProcessing() method instead.
  208. @see suspendProcessing
  209. */
  210. const CarlaRecursiveMutex& getCallbackLock() const noexcept { return callbackLock; }
  211. /** Enables and disables the processing callback.
  212. If you need to do something time-consuming on a thread and would like to make sure
  213. the audio processing callback doesn't happen until you've finished, use this
  214. to disable the callback and re-enable it again afterwards.
  215. E.g.
  216. @code
  217. void loadNewPatch()
  218. {
  219. suspendProcessing (true);
  220. ..do something that takes ages..
  221. suspendProcessing (false);
  222. }
  223. @endcode
  224. If the host tries to make an audio callback while processing is suspended, the
  225. filter will return an empty buffer, but won't block the audio thread like it would
  226. do if you use the getCallbackLock() critical section to synchronise access.
  227. Any code that calls processBlock() should call isSuspended() before doing so, and
  228. if the processor is suspended, it should avoid the call and emit silence or
  229. whatever is appropriate.
  230. @see getCallbackLock
  231. */
  232. void suspendProcessing (bool shouldBeSuspended);
  233. /** Returns true if processing is currently suspended.
  234. @see suspendProcessing
  235. */
  236. bool isSuspended() const noexcept { return suspended; }
  237. /** A plugin can override this to be told when it should reset any playing voices.
  238. The default implementation does nothing, but a host may call this to tell the
  239. plugin that it should stop any tails or sounds that have been left running.
  240. */
  241. virtual void reset();
  242. //==============================================================================
  243. /** Returns true if the processor is being run in an offline mode for rendering.
  244. If the processor is being run live on realtime signals, this returns false.
  245. If the mode is unknown, this will assume it's realtime and return false.
  246. This value may be unreliable until the prepareToPlay() method has been called,
  247. and could change each time prepareToPlay() is called.
  248. @see setNonRealtime()
  249. */
  250. bool isNonRealtime() const noexcept { return nonRealtime; }
  251. /** Called by the host to tell this processor whether it's being used in a non-realtime
  252. capacity for offline rendering or bouncing.
  253. */
  254. virtual void setNonRealtime (bool isNonRealtime) noexcept;
  255. //==============================================================================
  256. /** Tells the processor to use this playhead object.
  257. The processor will not take ownership of the object, so the caller must delete it when
  258. it is no longer being used.
  259. */
  260. virtual void setPlayHead (AudioPlayHead* newPlayHead);
  261. //==============================================================================
  262. /** This is called by the processor to specify its details before being played. Use this
  263. version of the function if you are not interested in any sidechain and/or aux buses
  264. and do not care about the layout of channels. Otherwise use setRateAndBufferSizeDetails.*/
  265. void setPlayConfigDetails (int numIns, int numOuts, double sampleRate, int blockSize);
  266. /** This is called by the processor to specify its details before being played. You
  267. should call this function after having informed the processor about the channel
  268. and bus layouts via setBusesLayout.
  269. @see setBusesLayout
  270. */
  271. void setRateAndBufferSizeDetails (double sampleRate, int blockSize) noexcept;
  272. private:
  273. //==============================================================================
  274. /** @internal */
  275. AudioPlayHead* playHead;
  276. //==============================================================================
  277. double currentSampleRate;
  278. int blockSize, latencySamples;
  279. bool suspended, nonRealtime;
  280. CarlaRecursiveMutex callbackLock;
  281. String cachedInputSpeakerArrString;
  282. String cachedOutputSpeakerArrString;
  283. int cachedTotalIns, cachedTotalOuts;
  284. void processBypassed (AudioSampleBuffer&, MidiBuffer&);
  285. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessor)
  286. };
  287. #endif // JUCE_AUDIOPROCESSOR_H_INCLUDED