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.

184 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /**
  26. Used by AudioSource::getNextAudioBlock().
  27. */
  28. struct JUCE_API AudioSourceChannelInfo
  29. {
  30. /** Creates an uninitialised AudioSourceChannelInfo. */
  31. AudioSourceChannelInfo() noexcept
  32. {
  33. }
  34. /** Creates an AudioSourceChannelInfo. */
  35. AudioSourceChannelInfo (AudioSampleBuffer* bufferToUse,
  36. int startSampleOffset, int numSamplesToUse) noexcept
  37. : buffer (bufferToUse),
  38. startSample (startSampleOffset),
  39. numSamples (numSamplesToUse)
  40. {
  41. }
  42. /** Creates an AudioSourceChannelInfo that uses the whole of a buffer.
  43. Note that the buffer provided must not be deleted while the
  44. AudioSourceChannelInfo is still using it.
  45. */
  46. explicit AudioSourceChannelInfo (AudioSampleBuffer& bufferToUse) noexcept
  47. : buffer (&bufferToUse),
  48. startSample (0),
  49. numSamples (bufferToUse.getNumSamples())
  50. {
  51. }
  52. /** The destination buffer to fill with audio data.
  53. When the AudioSource::getNextAudioBlock() method is called, the active section
  54. of this buffer should be filled with whatever output the source produces.
  55. Only the samples specified by the startSample and numSamples members of this structure
  56. should be affected by the call.
  57. The contents of the buffer when it is passed to the AudioSource::getNextAudioBlock()
  58. method can be treated as the input if the source is performing some kind of filter operation,
  59. but should be cleared if this is not the case - the clearActiveBufferRegion() is
  60. a handy way of doing this.
  61. The number of channels in the buffer could be anything, so the AudioSource
  62. must cope with this in whatever way is appropriate for its function.
  63. */
  64. AudioSampleBuffer* buffer;
  65. /** The first sample in the buffer from which the callback is expected
  66. to write data. */
  67. int startSample;
  68. /** The number of samples in the buffer which the callback is expected to
  69. fill with data. */
  70. int numSamples;
  71. /** Convenient method to clear the buffer if the source is not producing any data. */
  72. void clearActiveBufferRegion() const
  73. {
  74. if (buffer != nullptr)
  75. buffer->clear (startSample, numSamples);
  76. }
  77. };
  78. //==============================================================================
  79. /**
  80. Base class for objects that can produce a continuous stream of audio.
  81. An AudioSource has two states: 'prepared' and 'unprepared'.
  82. When a source needs to be played, it is first put into a 'prepared' state by a call to
  83. prepareToPlay(), and then repeated calls will be made to its getNextAudioBlock() method to
  84. process the audio data.
  85. Once playback has finished, the releaseResources() method is called to put the stream
  86. back into an 'unprepared' state.
  87. @see AudioFormatReaderSource, ResamplingAudioSource
  88. */
  89. class JUCE_API AudioSource
  90. {
  91. protected:
  92. //==============================================================================
  93. /** Creates an AudioSource. */
  94. AudioSource() noexcept {}
  95. public:
  96. /** Destructor. */
  97. virtual ~AudioSource() {}
  98. //==============================================================================
  99. /** Tells the source to prepare for playing.
  100. An AudioSource has two states: prepared and unprepared.
  101. The prepareToPlay() method is guaranteed to be called at least once on an 'unpreprared'
  102. source to put it into a 'prepared' state before any calls will be made to getNextAudioBlock().
  103. This callback allows the source to initialise any resources it might need when playing.
  104. Once playback has finished, the releaseResources() method is called to put the stream
  105. back into an 'unprepared' state.
  106. Note that this method could be called more than once in succession without
  107. a matching call to releaseResources(), so make sure your code is robust and
  108. can handle that kind of situation.
  109. @param samplesPerBlockExpected the number of samples that the source
  110. will be expected to supply each time its
  111. getNextAudioBlock() method is called. This
  112. number may vary slightly, because it will be dependent
  113. on audio hardware callbacks, and these aren't
  114. guaranteed to always use a constant block size, so
  115. the source should be able to cope with small variations.
  116. @param sampleRate the sample rate that the output will be used at - this
  117. is needed by sources such as tone generators.
  118. @see releaseResources, getNextAudioBlock
  119. */
  120. virtual void prepareToPlay (int samplesPerBlockExpected,
  121. double sampleRate) = 0;
  122. /** Allows the source to release anything it no longer needs after playback has stopped.
  123. This will be called when the source is no longer going to have its getNextAudioBlock()
  124. method called, so it should release any spare memory, etc. that it might have
  125. allocated during the prepareToPlay() call.
  126. Note that there's no guarantee that prepareToPlay() will actually have been called before
  127. releaseResources(), and it may be called more than once in succession, so make sure your
  128. code is robust and doesn't make any assumptions about when it will be called.
  129. @see prepareToPlay, getNextAudioBlock
  130. */
  131. virtual void releaseResources() = 0;
  132. /** Called repeatedly to fetch subsequent blocks of audio data.
  133. After calling the prepareToPlay() method, this callback will be made each
  134. time the audio playback hardware (or whatever other destination the audio
  135. data is going to) needs another block of data.
  136. It will generally be called on a high-priority system thread, or possibly even
  137. an interrupt, so be careful not to do too much work here, as that will cause
  138. audio glitches!
  139. @see AudioSourceChannelInfo, prepareToPlay, releaseResources
  140. */
  141. virtual void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) = 0;
  142. };