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.

125 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 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. @tags{Audio}
  27. */
  28. class JUCE_API BufferingAudioSource : public PositionableAudioSource,
  29. private TimeSliceClient
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a BufferingAudioSource.
  34. @param source the input source to read from
  35. @param backgroundThread a background thread that will be used for the
  36. background read-ahead. This object must not be deleted
  37. until after any BufferingAudioSources that are using it
  38. have been deleted!
  39. @param deleteSourceWhenDeleted if true, then the input source object will
  40. be deleted when this object is deleted
  41. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  42. @param numberOfChannels the number of channels that will be played
  43. @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
  44. block until the buffer has been filled
  45. */
  46. BufferingAudioSource (PositionableAudioSource* source,
  47. TimeSliceThread& backgroundThread,
  48. bool deleteSourceWhenDeleted,
  49. int numberOfSamplesToBuffer,
  50. int numberOfChannels = 2,
  51. bool prefillBufferOnPrepareToPlay = true);
  52. /** Destructor.
  53. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  54. flag was set in the constructor.
  55. */
  56. ~BufferingAudioSource() override;
  57. //==============================================================================
  58. /** Implementation of the AudioSource method. */
  59. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  60. /** Implementation of the AudioSource method. */
  61. void releaseResources() override;
  62. /** Implementation of the AudioSource method. */
  63. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  64. //==============================================================================
  65. /** Implements the PositionableAudioSource method. */
  66. void setNextReadPosition (int64 newPosition) override;
  67. /** Implements the PositionableAudioSource method. */
  68. int64 getNextReadPosition() const override;
  69. /** Implements the PositionableAudioSource method. */
  70. int64 getTotalLength() const override { return source->getTotalLength(); }
  71. /** Implements the PositionableAudioSource method. */
  72. bool isLooping() const override { return source->isLooping(); }
  73. /** A useful function to block until the next the buffer info can be filled.
  74. This is useful for offline rendering.
  75. */
  76. bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
  77. private:
  78. //==============================================================================
  79. Range<int> getValidBufferRange (int numSamples) const;
  80. bool readNextBufferChunk();
  81. void readBufferSection (int64 start, int length, int bufferOffset);
  82. int useTimeSlice() override;
  83. //==============================================================================
  84. OptionalScopedPointer<PositionableAudioSource> source;
  85. TimeSliceThread& backgroundThread;
  86. int numberOfSamplesToBuffer, numberOfChannels;
  87. AudioBuffer<float> buffer;
  88. CriticalSection callbackLock, bufferRangeLock;
  89. WaitableEvent bufferReadyEvent;
  90. int64 bufferValidStart = 0, bufferValidEnd = 0;
  91. std::atomic<int64> nextPlayPos { 0 };
  92. double sampleRate = 0;
  93. bool wasSourceLooping = false, isPrepared = false;
  94. const bool prefillBuffer;
  95. //==============================================================================
  96. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
  97. };
  98. } // namespace juce