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.

185 lines
7.7KB

  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_AUDIOSOURCE_JUCEHEADER__
  19. #define __JUCE_AUDIOSOURCE_JUCEHEADER__
  20. #include "../buffers/juce_AudioSampleBuffer.h"
  21. //==============================================================================
  22. /**
  23. Used by AudioSource::getNextAudioBlock().
  24. */
  25. struct JUCE_API AudioSourceChannelInfo
  26. {
  27. /** Creates an uninitialised AudioSourceChannelInfo. */
  28. AudioSourceChannelInfo() noexcept
  29. {
  30. }
  31. /** Creates an AudioSourceChannelInfo. */
  32. AudioSourceChannelInfo (AudioSampleBuffer* bufferToUse,
  33. int startSampleOffset, int numSamplesToUse) noexcept
  34. : buffer (bufferToUse),
  35. startSample (startSampleOffset),
  36. numSamples (numSamplesToUse)
  37. {
  38. }
  39. /** Creates an AudioSourceChannelInfo that uses the whole of a buffer.
  40. Note that the buffer provided must not be deleted while the
  41. AudioSourceChannelInfo is still using it.
  42. */
  43. explicit AudioSourceChannelInfo (AudioSampleBuffer& bufferToUse) noexcept
  44. : buffer (&bufferToUse),
  45. startSample (0),
  46. numSamples (bufferToUse.getNumSamples())
  47. {
  48. }
  49. /** The destination buffer to fill with audio data.
  50. When the AudioSource::getNextAudioBlock() method is called, the active section
  51. of this buffer should be filled with whatever output the source produces.
  52. Only the samples specified by the startSample and numSamples members of this structure
  53. should be affected by the call.
  54. The contents of the buffer when it is passed to the the AudioSource::getNextAudioBlock()
  55. method can be treated as the input if the source is performing some kind of filter operation,
  56. but should be cleared if this is not the case - the clearActiveBufferRegion() is
  57. a handy way of doing this.
  58. The number of channels in the buffer could be anything, so the AudioSource
  59. must cope with this in whatever way is appropriate for its function.
  60. */
  61. AudioSampleBuffer* buffer;
  62. /** The first sample in the buffer from which the callback is expected
  63. to write data. */
  64. int startSample;
  65. /** The number of samples in the buffer which the callback is expected to
  66. fill with data. */
  67. int numSamples;
  68. /** Convenient method to clear the buffer if the source is not producing any data. */
  69. void clearActiveBufferRegion() const
  70. {
  71. if (buffer != nullptr)
  72. buffer->clear (startSample, numSamples);
  73. }
  74. };
  75. //==============================================================================
  76. /**
  77. Base class for objects that can produce a continuous stream of audio.
  78. An AudioSource has two states: 'prepared' and 'unprepared'.
  79. When a source needs to be played, it is first put into a 'prepared' state by a call to
  80. prepareToPlay(), and then repeated calls will be made to its getNextAudioBlock() method to
  81. process the audio data.
  82. Once playback has finished, the releaseResources() method is called to put the stream
  83. back into an 'unprepared' state.
  84. @see AudioFormatReaderSource, ResamplingAudioSource
  85. */
  86. class JUCE_API AudioSource
  87. {
  88. protected:
  89. //==============================================================================
  90. /** Creates an AudioSource. */
  91. AudioSource() noexcept {}
  92. public:
  93. /** Destructor. */
  94. virtual ~AudioSource() {}
  95. //==============================================================================
  96. /** Tells the source to prepare for playing.
  97. An AudioSource has two states: prepared and unprepared.
  98. The prepareToPlay() method is guaranteed to be called at least once on an 'unpreprared'
  99. source to put it into a 'prepared' state before any calls will be made to getNextAudioBlock().
  100. This callback allows the source to initialise any resources it might need when playing.
  101. Once playback has finished, the releaseResources() method is called to put the stream
  102. back into an 'unprepared' state.
  103. Note that this method could be called more than once in succession without
  104. a matching call to releaseResources(), so make sure your code is robust and
  105. can handle that kind of situation.
  106. @param samplesPerBlockExpected the number of samples that the source
  107. will be expected to supply each time its
  108. getNextAudioBlock() method is called. This
  109. number may vary slightly, because it will be dependent
  110. on audio hardware callbacks, and these aren't
  111. guaranteed to always use a constant block size, so
  112. the source should be able to cope with small variations.
  113. @param sampleRate the sample rate that the output will be used at - this
  114. is needed by sources such as tone generators.
  115. @see releaseResources, getNextAudioBlock
  116. */
  117. virtual void prepareToPlay (int samplesPerBlockExpected,
  118. double sampleRate) = 0;
  119. /** Allows the source to release anything it no longer needs after playback has stopped.
  120. This will be called when the source is no longer going to have its getNextAudioBlock()
  121. method called, so it should release any spare memory, etc. that it might have
  122. allocated during the prepareToPlay() call.
  123. Note that there's no guarantee that prepareToPlay() will actually have been called before
  124. releaseResources(), and it may be called more than once in succession, so make sure your
  125. code is robust and doesn't make any assumptions about when it will be called.
  126. @see prepareToPlay, getNextAudioBlock
  127. */
  128. virtual void releaseResources() = 0;
  129. /** Called repeatedly to fetch subsequent blocks of audio data.
  130. After calling the prepareToPlay() method, this callback will be made each
  131. time the audio playback hardware (or whatever other destination the audio
  132. data is going to) needs another block of data.
  133. It will generally be called on a high-priority system thread, or possibly even
  134. an interrupt, so be careful not to do too much work here, as that will cause
  135. audio glitches!
  136. @see AudioSourceChannelInfo, prepareToPlay, releaseResources
  137. */
  138. virtual void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) = 0;
  139. };
  140. #endif // __JUCE_AUDIOSOURCE_JUCEHEADER__