The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

334 lines
14KB

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