The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

124 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /**
  26. An AudioSource which takes another source as input, and buffers it using a thread.
  27. Create this as a wrapper around another thread, and it will read-ahead with
  28. a background thread to smooth out playback. You can either create one of these
  29. directly, or use it indirectly using an AudioTransportSource.
  30. @see PositionableAudioSource, AudioTransportSource
  31. */
  32. class JUCE_API BufferingAudioSource : public PositionableAudioSource,
  33. private TimeSliceClient
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates a BufferingAudioSource.
  38. @param source the input source to read from
  39. @param backgroundThread a background thread that will be used for the
  40. background read-ahead. This object must not be deleted
  41. until after any BufferingAudioSources that are using it
  42. have been deleted!
  43. @param deleteSourceWhenDeleted if true, then the input source object will
  44. be deleted when this object is deleted
  45. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  46. @param numberOfChannels the number of channels that will be played
  47. @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
  48. block until the buffer has been filled
  49. */
  50. BufferingAudioSource (PositionableAudioSource* source,
  51. TimeSliceThread& backgroundThread,
  52. bool deleteSourceWhenDeleted,
  53. int numberOfSamplesToBuffer,
  54. int numberOfChannels = 2,
  55. bool prefillBufferOnPrepareToPlay = true);
  56. /** Destructor.
  57. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  58. flag was set in the constructor.
  59. */
  60. ~BufferingAudioSource();
  61. //==============================================================================
  62. /** Implementation of the AudioSource method. */
  63. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  64. /** Implementation of the AudioSource method. */
  65. void releaseResources() override;
  66. /** Implementation of the AudioSource method. */
  67. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  68. //==============================================================================
  69. /** Implements the PositionableAudioSource method. */
  70. void setNextReadPosition (int64 newPosition) override;
  71. /** Implements the PositionableAudioSource method. */
  72. int64 getNextReadPosition() const override;
  73. /** Implements the PositionableAudioSource method. */
  74. int64 getTotalLength() const override { return source->getTotalLength(); }
  75. /** Implements the PositionableAudioSource method. */
  76. bool isLooping() const override { return source->isLooping(); }
  77. /** A useful function to block until the next the buffer info can be filled.
  78. This is useful for offline rendering.
  79. */
  80. bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
  81. private:
  82. //==============================================================================
  83. OptionalScopedPointer<PositionableAudioSource> source;
  84. TimeSliceThread& backgroundThread;
  85. int numberOfSamplesToBuffer, numberOfChannels;
  86. AudioSampleBuffer buffer;
  87. CriticalSection bufferStartPosLock;
  88. WaitableEvent bufferReadyEvent;
  89. int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos;
  90. double volatile sampleRate;
  91. bool wasSourceLooping, isPrepared, prefillBuffer;
  92. bool readNextBufferChunk();
  93. void readBufferSection (int64 start, int length, int bufferOffset);
  94. int useTimeSlice() override;
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
  96. };