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.

188 lines
7.9KB

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