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.

160 lines
6.9KB

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