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.

118 lines
5.1KB

  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 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. } // namespace juce