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.

128 lines
5.7KB

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