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.

122 lines
5.3KB

  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_BUFFERINGAUDIOSOURCE_H_INCLUDED
  18. #define JUCE_BUFFERINGAUDIOSOURCE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. An AudioSource which takes another source as input, and buffers it using a thread.
  22. Create this as a wrapper around another thread, and it will read-ahead with
  23. a background thread to smooth out playback. You can either create one of these
  24. directly, or use it indirectly using an AudioTransportSource.
  25. @see PositionableAudioSource, AudioTransportSource
  26. */
  27. class JUCE_API BufferingAudioSource : public PositionableAudioSource,
  28. private TimeSliceClient
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a BufferingAudioSource.
  33. @param source the input source to read from
  34. @param backgroundThread a background thread that will be used for the
  35. background read-ahead. This object must not be deleted
  36. until after any BufferingAudioSources that are using it
  37. have been deleted!
  38. @param deleteSourceWhenDeleted if true, then the input source object will
  39. be deleted when this object is deleted
  40. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  41. @param numberOfChannels the number of channels that will be played
  42. @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
  43. block until the buffer has been filled
  44. */
  45. BufferingAudioSource (PositionableAudioSource* source,
  46. TimeSliceThread& backgroundThread,
  47. bool deleteSourceWhenDeleted,
  48. int numberOfSamplesToBuffer,
  49. int numberOfChannels = 2,
  50. bool prefillBufferOnPrepareToPlay = true);
  51. /** Destructor.
  52. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  53. flag was set in the constructor.
  54. */
  55. ~BufferingAudioSource();
  56. //==============================================================================
  57. /** Implementation of the AudioSource method. */
  58. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  59. /** Implementation of the AudioSource method. */
  60. void releaseResources() override;
  61. /** Implementation of the AudioSource method. */
  62. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  63. //==============================================================================
  64. /** Implements the PositionableAudioSource method. */
  65. void setNextReadPosition (int64 newPosition) override;
  66. /** Implements the PositionableAudioSource method. */
  67. int64 getNextReadPosition() const override;
  68. /** Implements the PositionableAudioSource method. */
  69. int64 getTotalLength() const override { return source->getTotalLength(); }
  70. /** Implements the PositionableAudioSource method. */
  71. bool isLooping() const override { return source->isLooping(); }
  72. /** A useful function to block until the next the buffer info can be filled.
  73. This is useful for offline rendering.
  74. */
  75. bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
  76. private:
  77. //==============================================================================
  78. OptionalScopedPointer<PositionableAudioSource> source;
  79. TimeSliceThread& backgroundThread;
  80. int numberOfSamplesToBuffer, numberOfChannels;
  81. AudioSampleBuffer buffer;
  82. CriticalSection bufferStartPosLock;
  83. WaitableEvent bufferReadyEvent;
  84. int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos;
  85. double volatile sampleRate;
  86. bool wasSourceLooping, isPrepared, prefillBuffer;
  87. bool readNextBufferChunk();
  88. void readBufferSection (int64 start, int length, int bufferOffset);
  89. int useTimeSlice() override;
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
  91. };
  92. #endif // JUCE_BUFFERINGAUDIOSOURCE_H_INCLUDED