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_AudioSource.h 7.4KB

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