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.

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