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_AudioIODevice.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  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. /** Returns the number of under- or over runs reported by the OS since
  231. playback/recording has started.
  232. This number may be different than determining the Xrun count manually (by
  233. measuring the time spent in the audio callback) as the OS may be doing
  234. some buffering internally - especially on mobile devices.
  235. Returns -1 if playback/recording has not started yet or if getting the underrun
  236. count is not supported for this device (Android SDK 23 and lower).
  237. */
  238. virtual int getXRunCount() const noexcept;
  239. //==============================================================================
  240. protected:
  241. /** Creates a device, setting its name and type member variables. */
  242. AudioIODevice (const String& deviceName,
  243. const String& typeName);
  244. /** @internal */
  245. String name, typeName;
  246. };
  247. } // namespace juce