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.

187 lines
8.0KB

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