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.

188 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_AUDIOTRANSPORTSOURCE_JUCEHEADER__
  19. #define __JUCE_AUDIOTRANSPORTSOURCE_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. An AudioSource that takes a PositionableAudioSource and allows it to be
  23. played, stopped, started, etc.
  24. This can also be told use a buffer and background thread to read ahead, and
  25. if can correct for different sample-rates.
  26. You may want to use one of these along with an AudioSourcePlayer and AudioIODevice
  27. to control playback of an audio file.
  28. @see AudioSource, AudioSourcePlayer
  29. */
  30. class JUCE_API AudioTransportSource : public PositionableAudioSource,
  31. public ChangeBroadcaster
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an AudioTransportSource.
  36. After creating one of these, use the setSource() method to select an input source.
  37. */
  38. AudioTransportSource();
  39. /** Destructor. */
  40. ~AudioTransportSource();
  41. //==============================================================================
  42. /** Sets the reader that is being used as the input source.
  43. This will stop playback, reset the position to 0 and change to the new reader.
  44. The source passed in will not be deleted by this object, so must be managed by
  45. the caller.
  46. @param newSource the new input source to use. This may be zero
  47. @param readAheadBufferSize a size of buffer to use for reading ahead. If this
  48. is zero, no reading ahead will be done; if it's
  49. greater than zero, a BufferingAudioSource will be used
  50. to do the reading-ahead. If you set a non-zero value here,
  51. you'll also need to set the readAheadThread parameter.
  52. @param readAheadThread if you set readAheadBufferSize to a non-zero value, then
  53. you'll also need to supply this TimeSliceThread object for
  54. the background reader to use. The thread object must not be
  55. deleted while the AudioTransport source is still using it.
  56. @param sourceSampleRateToCorrectFor if this is non-zero, it specifies the sample
  57. rate of the source, and playback will be sample-rate
  58. adjusted to maintain playback at the correct pitch. If
  59. this is 0, no sample-rate adjustment will be performed
  60. @param maxNumChannels the maximum number of channels that may need to be played
  61. */
  62. void setSource (PositionableAudioSource* newSource,
  63. int readAheadBufferSize = 0,
  64. TimeSliceThread* readAheadThread = nullptr,
  65. double sourceSampleRateToCorrectFor = 0.0,
  66. int maxNumChannels = 2);
  67. //==============================================================================
  68. /** Changes the current playback position in the source stream.
  69. The next time the getNextAudioBlock() method is called, this
  70. is the time from which it'll read data.
  71. @see getPosition
  72. */
  73. void setPosition (double newPosition);
  74. /** Returns the position that the next data block will be read from
  75. This is a time in seconds.
  76. */
  77. double getCurrentPosition() const;
  78. /** Returns the stream's length in seconds. */
  79. double getLengthInSeconds() const;
  80. /** Returns true if the player has stopped because its input stream ran out of data.
  81. */
  82. bool hasStreamFinished() const noexcept { return inputStreamEOF; }
  83. //==============================================================================
  84. /** Starts playing (if a source has been selected).
  85. If it starts playing, this will send a message to any ChangeListeners
  86. that are registered with this object.
  87. */
  88. void start();
  89. /** Stops playing.
  90. If it's actually playing, this will send a message to any ChangeListeners
  91. that are registered with this object.
  92. */
  93. void stop();
  94. /** Returns true if it's currently playing. */
  95. bool isPlaying() const noexcept { return playing; }
  96. //==============================================================================
  97. /** Changes the gain to apply to the output.
  98. @param newGain a factor by which to multiply the outgoing samples,
  99. so 1.0 = 0dB, 0.5 = -6dB, 2.0 = 6dB, etc.
  100. */
  101. void setGain (float newGain) noexcept;
  102. /** Returns the current gain setting.
  103. @see setGain
  104. */
  105. float getGain() const noexcept { return gain; }
  106. //==============================================================================
  107. /** Implementation of the AudioSource method. */
  108. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  109. /** Implementation of the AudioSource method. */
  110. void releaseResources();
  111. /** Implementation of the AudioSource method. */
  112. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill);
  113. //==============================================================================
  114. /** Implements the PositionableAudioSource method. */
  115. void setNextReadPosition (int64 newPosition);
  116. /** Implements the PositionableAudioSource method. */
  117. int64 getNextReadPosition() const;
  118. /** Implements the PositionableAudioSource method. */
  119. int64 getTotalLength() const;
  120. /** Implements the PositionableAudioSource method. */
  121. bool isLooping() const;
  122. private:
  123. //==============================================================================
  124. PositionableAudioSource* source;
  125. ResamplingAudioSource* resamplerSource;
  126. BufferingAudioSource* bufferingSource;
  127. PositionableAudioSource* positionableSource;
  128. AudioSource* masterSource;
  129. CriticalSection callbackLock;
  130. float volatile gain, lastGain;
  131. bool volatile playing, stopped;
  132. double sampleRate, sourceSampleRate;
  133. int blockSize, readAheadBufferSize;
  134. bool isPrepared, inputStreamEOF;
  135. void releaseMasterResources();
  136. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioTransportSource);
  137. };
  138. #endif // __JUCE_AUDIOTRANSPORTSOURCE_JUCEHEADER__