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_AudioTransportSource.h 7.6KB

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