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.

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