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.

177 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  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 a nullptr
  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 = nullptr;
  123. ResamplingAudioSource* resamplerSource = nullptr;
  124. BufferingAudioSource* bufferingSource = nullptr;
  125. PositionableAudioSource* positionableSource = nullptr;
  126. AudioSource* masterSource = nullptr;
  127. CriticalSection callbackLock;
  128. float volatile gain = 1.0f, lastGain = 1.0f;
  129. bool volatile playing = false, stopped = true;
  130. double sampleRate = 44100.0, sourceSampleRate = 0;
  131. int blockSize = 128, readAheadBufferSize = 0;
  132. bool volatile isPrepared = false, inputStreamEOF = false;
  133. void releaseMasterResources();
  134. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioTransportSource)
  135. };
  136. } // namespace juce