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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. @tags{Audio}
  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() override;
  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 a nullptr
  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. @param newPosition the new playback position in seconds
  72. @see getCurrentPosition
  73. */
  74. void setPosition (double newPosition);
  75. /** Returns the position that the next data block will be read from
  76. This is a time in seconds.
  77. */
  78. double getCurrentPosition() const;
  79. /** Returns the stream's length in seconds. */
  80. double getLengthInSeconds() const;
  81. /** Returns true if the player has stopped because its input stream ran out of data. */
  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) override;
  109. /** Implementation of the AudioSource method. */
  110. void releaseResources() override;
  111. /** Implementation of the AudioSource method. */
  112. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  113. //==============================================================================
  114. /** Implements the PositionableAudioSource method. */
  115. void setNextReadPosition (int64 newPosition) override;
  116. /** Implements the PositionableAudioSource method. */
  117. int64 getNextReadPosition() const override;
  118. /** Implements the PositionableAudioSource method. */
  119. int64 getTotalLength() const override;
  120. /** Implements the PositionableAudioSource method. */
  121. bool isLooping() const override;
  122. private:
  123. //==============================================================================
  124. PositionableAudioSource* source = nullptr;
  125. ResamplingAudioSource* resamplerSource = nullptr;
  126. BufferingAudioSource* bufferingSource = nullptr;
  127. PositionableAudioSource* positionableSource = nullptr;
  128. AudioSource* masterSource = nullptr;
  129. CriticalSection callbackLock;
  130. float gain = 1.0f, lastGain = 1.0f;
  131. std::atomic<bool> playing { false }, stopped { true };
  132. double sampleRate = 44100.0, sourceSampleRate = 0;
  133. int blockSize = 128, readAheadBufferSize = 0;
  134. bool isPrepared = false, inputStreamEOF = false;
  135. void releaseMasterResources();
  136. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioTransportSource)
  137. };
  138. } // namespace juce