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.

393 lines
18KB

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