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.

87 lines
3.5KB

  1. /*
  2. Copyright (C) 2017 Xenakios
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of version 3 of the GNU General Public License
  5. as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License (version 3) for more details.
  10. www.gnu.org/licenses
  11. */
  12. #pragma once
  13. #include "../JuceLibraryCode/JuceHeader.h"
  14. class JUCE_API MyBufferingAudioSource : public PositionableAudioSource,
  15. private TimeSliceClient
  16. {
  17. public:
  18. //==============================================================================
  19. /** Creates a BufferingAudioSource.
  20. @param source the input source to read from
  21. @param backgroundThread a background thread that will be used for the
  22. background read-ahead. This object must not be deleted
  23. until after any BufferingAudioSources that are using it
  24. have been deleted!
  25. @param deleteSourceWhenDeleted if true, then the input source object will
  26. be deleted when this object is deleted
  27. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  28. @param numberOfChannels the number of channels that will be played
  29. @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
  30. block until the buffer has been filled
  31. */
  32. MyBufferingAudioSource(PositionableAudioSource* source,
  33. TimeSliceThread& backgroundThread,
  34. bool deleteSourceWhenDeleted,
  35. int numberOfSamplesToBuffer,
  36. int numberOfChannels = 2,
  37. bool prefillBufferOnPrepareToPlay = true);
  38. ~MyBufferingAudioSource();
  39. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  40. void releaseResources() override;
  41. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  42. void setNextReadPosition (int64 newPosition) override;
  43. int64 getNextReadPosition() const override;
  44. int64 getTotalLength() const override { return source->getTotalLength(); }
  45. bool isLooping() const override { return source->isLooping(); }
  46. bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
  47. [[nodiscard]] double getPercentReady() const;
  48. int getNumberOfChannels() { return numberOfChannels; }
  49. private:
  50. //==============================================================================
  51. OptionalScopedPointer<PositionableAudioSource> source;
  52. TimeSliceThread& backgroundThread;
  53. int numberOfSamplesToBuffer, numberOfChannels;
  54. AudioBuffer<float> buffer;
  55. CriticalSection bufferStartPosLock;
  56. WaitableEvent bufferReadyEvent;
  57. std::atomic<int64> bufferValidStart { 0 }, bufferValidEnd { 0 }, nextPlayPos { 0 };
  58. double sampleRate = 0;
  59. bool wasSourceLooping = false, isPrepared = false, prefillBuffer;
  60. bool readNextBufferChunk();
  61. void readBufferSection (int64 start, int length, int bufferOffset);
  62. int useTimeSlice() override;
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyBufferingAudioSource)
  64. };