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.

310 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_AUDIOIODEVICE_H_INCLUDED
  18. #define JUCE_AUDIOIODEVICE_H_INCLUDED
  19. class AudioIODevice;
  20. //==============================================================================
  21. /**
  22. One of these is passed to an AudioIODevice object to stream the audio data
  23. in and out.
  24. The AudioIODevice will repeatedly call this class's audioDeviceIOCallback()
  25. method on its own high-priority audio thread, when it needs to send or receive
  26. the next block of data.
  27. @see AudioIODevice, AudioDeviceManager
  28. */
  29. class JUCE_API AudioIODeviceCallback
  30. {
  31. public:
  32. /** Destructor. */
  33. virtual ~AudioIODeviceCallback() {}
  34. /** Processes a block of incoming and outgoing audio data.
  35. The subclass's implementation should use the incoming audio for whatever
  36. purposes it needs to, and must fill all the output channels with the next
  37. block of output data before returning.
  38. The channel data is arranged with the same array indices as the channel name
  39. array returned by AudioIODevice::getOutputChannelNames(), but those channels
  40. that aren't specified in AudioIODevice::open() will have a null pointer for their
  41. associated channel, so remember to check for this.
  42. @param inputChannelData a set of arrays containing the audio data for each
  43. incoming channel - this data is valid until the function
  44. returns. There will be one channel of data for each input
  45. channel that was enabled when the audio device was opened
  46. (see AudioIODevice::open())
  47. @param numInputChannels the number of pointers to channel data in the
  48. inputChannelData array.
  49. @param outputChannelData a set of arrays which need to be filled with the data
  50. that should be sent to each outgoing channel of the device.
  51. There will be one channel of data for each output channel
  52. that was enabled when the audio device was opened (see
  53. AudioIODevice::open())
  54. The initial contents of the array is undefined, so the
  55. callback function must fill all the channels with zeros if
  56. its output is silence. Failing to do this could cause quite
  57. an unpleasant noise!
  58. @param numOutputChannels the number of pointers to channel data in the
  59. outputChannelData array.
  60. @param numSamples the number of samples in each channel of the input and
  61. output arrays. The number of samples will depend on the
  62. audio device's buffer size and will usually remain constant,
  63. although this isn't guaranteed, so make sure your code can
  64. cope with reasonable changes in the buffer size from one
  65. callback to the next.
  66. */
  67. virtual void audioDeviceIOCallback (const float** inputChannelData,
  68. int numInputChannels,
  69. float** outputChannelData,
  70. int numOutputChannels,
  71. int numSamples) = 0;
  72. /** Called to indicate that the device is about to start calling back.
  73. This will be called just before the audio callbacks begin, either when this
  74. callback has just been added to an audio device, or after the device has been
  75. restarted because of a sample-rate or block-size change.
  76. You can use this opportunity to find out the sample rate and block size
  77. that the device is going to use by calling the AudioIODevice::getCurrentSampleRate()
  78. and AudioIODevice::getCurrentBufferSizeSamples() on the supplied pointer.
  79. @param device the audio IO device that will be used to drive the callback.
  80. Note that if you're going to store this this pointer, it is
  81. only valid until the next time that audioDeviceStopped is called.
  82. */
  83. virtual void audioDeviceAboutToStart (AudioIODevice* device) = 0;
  84. /** Called to indicate that the device has stopped. */
  85. virtual void audioDeviceStopped() = 0;
  86. /** This can be overridden to be told if the device generates an error while operating.
  87. Be aware that this could be called by any thread! And not all devices perform
  88. this callback.
  89. */
  90. virtual void audioDeviceError (const String& errorMessage);
  91. };
  92. //==============================================================================
  93. /**
  94. Base class for an audio device with synchronised input and output channels.
  95. Subclasses of this are used to implement different protocols such as DirectSound,
  96. ASIO, CoreAudio, etc.
  97. To create one of these, you'll need to use the AudioIODeviceType class - see the
  98. documentation for that class for more info.
  99. For an easier way of managing audio devices and their settings, have a look at the
  100. AudioDeviceManager class.
  101. @see AudioIODeviceType, AudioDeviceManager
  102. */
  103. class JUCE_API AudioIODevice
  104. {
  105. public:
  106. /** Destructor. */
  107. virtual ~AudioIODevice();
  108. //==============================================================================
  109. /** Returns the device's name, (as set in the constructor). */
  110. const String& getName() const noexcept { return name; }
  111. /** Returns the type of the device.
  112. E.g. "CoreAudio", "ASIO", etc. - this comes from the AudioIODeviceType that created it.
  113. */
  114. const String& getTypeName() const noexcept { return typeName; }
  115. //==============================================================================
  116. /** Returns the names of all the available output channels on this device.
  117. To find out which of these are currently in use, call getActiveOutputChannels().
  118. */
  119. virtual StringArray getOutputChannelNames() = 0;
  120. /** Returns the names of all the available input channels on this device.
  121. To find out which of these are currently in use, call getActiveInputChannels().
  122. */
  123. virtual StringArray getInputChannelNames() = 0;
  124. //==============================================================================
  125. /** Returns the set of sample-rates this device supports.
  126. @see getCurrentSampleRate
  127. */
  128. virtual Array<double> getAvailableSampleRates() = 0;
  129. /** Returns the set of buffer sizes that are available.
  130. @see getCurrentBufferSizeSamples, getDefaultBufferSize
  131. */
  132. virtual Array<int> getAvailableBufferSizes() = 0;
  133. /** Returns the default buffer-size to use.
  134. @returns a number of samples
  135. @see getAvailableBufferSizes
  136. */
  137. virtual int getDefaultBufferSize() = 0;
  138. //==============================================================================
  139. /** Tries to open the device ready to play.
  140. @param inputChannels a BigInteger in which a set bit indicates that the corresponding
  141. input channel should be enabled
  142. @param outputChannels a BigInteger in which a set bit indicates that the corresponding
  143. output channel should be enabled
  144. @param sampleRate the sample rate to try to use - to find out which rates are
  145. available, see getAvailableSampleRates()
  146. @param bufferSizeSamples the size of i/o buffer to use - to find out the available buffer
  147. sizes, see getAvailableBufferSizes()
  148. @returns an error description if there's a problem, or an empty string if it succeeds in
  149. opening the device
  150. @see close
  151. */
  152. virtual String open (const BigInteger& inputChannels,
  153. const BigInteger& outputChannels,
  154. double sampleRate,
  155. int bufferSizeSamples) = 0;
  156. /** Closes and releases the device if it's open. */
  157. virtual void close() = 0;
  158. /** Returns true if the device is still open.
  159. A device might spontaneously close itself if something goes wrong, so this checks if
  160. it's still open.
  161. */
  162. virtual bool isOpen() = 0;
  163. /** Starts the device actually playing.
  164. This must be called after the device has been opened.
  165. @param callback the callback to use for streaming the data.
  166. @see AudioIODeviceCallback, open
  167. */
  168. virtual void start (AudioIODeviceCallback* callback) = 0;
  169. /** Stops the device playing.
  170. Once a device has been started, this will stop it. Any pending calls to the
  171. callback class will be flushed before this method returns.
  172. */
  173. virtual void stop() = 0;
  174. /** Returns true if the device is still calling back.
  175. The device might mysteriously stop, so this checks whether it's
  176. still playing.
  177. */
  178. virtual bool isPlaying() = 0;
  179. /** Returns the last error that happened if anything went wrong. */
  180. virtual String getLastError() = 0;
  181. //==============================================================================
  182. /** Returns the buffer size that the device is currently using.
  183. If the device isn't actually open, this value doesn't really mean much.
  184. */
  185. virtual int getCurrentBufferSizeSamples() = 0;
  186. /** Returns the sample rate that the device is currently using.
  187. If the device isn't actually open, this value doesn't really mean much.
  188. */
  189. virtual double getCurrentSampleRate() = 0;
  190. /** Returns the device's current physical bit-depth.
  191. If the device isn't actually open, this value doesn't really mean much.
  192. */
  193. virtual int getCurrentBitDepth() = 0;
  194. /** Returns a mask showing which of the available output channels are currently
  195. enabled.
  196. @see getOutputChannelNames
  197. */
  198. virtual BigInteger getActiveOutputChannels() const = 0;
  199. /** Returns a mask showing which of the available input channels are currently
  200. enabled.
  201. @see getInputChannelNames
  202. */
  203. virtual BigInteger getActiveInputChannels() const = 0;
  204. /** Returns the device's output latency.
  205. This is the delay in samples between a callback getting a block of data, and
  206. that data actually getting played.
  207. */
  208. virtual int getOutputLatencyInSamples() = 0;
  209. /** Returns the device's input latency.
  210. This is the delay in samples between some audio actually arriving at the soundcard,
  211. and the callback getting passed this block of data.
  212. */
  213. virtual int getInputLatencyInSamples() = 0;
  214. //==============================================================================
  215. /** True if this device can show a pop-up control panel for editing its settings.
  216. This is generally just true of ASIO devices. If true, you can call showControlPanel()
  217. to display it.
  218. */
  219. virtual bool hasControlPanel() const;
  220. /** Shows a device-specific control panel if there is one.
  221. This should only be called for devices which return true from hasControlPanel().
  222. */
  223. virtual bool showControlPanel();
  224. /** On devices which support it, this allows automatic gain control or other
  225. mic processing to be disabled.
  226. If the device doesn't support this operation, it'll return false.
  227. */
  228. virtual bool setAudioPreprocessingEnabled (bool shouldBeEnabled);
  229. //==============================================================================
  230. protected:
  231. /** Creates a device, setting its name and type member variables. */
  232. AudioIODevice (const String& deviceName,
  233. const String& typeName);
  234. /** @internal */
  235. String name, typeName;
  236. };
  237. #endif // JUCE_AUDIOIODEVICE_H_INCLUDED