OpenShot Audio Library | OpenShotAudio  0.6.0
juce_BufferingAudioFormatReader.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 //==============================================================================
38 class JUCE_API BufferingAudioReader : public AudioFormatReader,
39  private TimeSliceClient
40 {
41 public:
53  TimeSliceThread& timeSliceThread,
54  int samplesToBuffer);
55 
56  ~BufferingAudioReader() override;
57 
63  void setReadTimeout (int timeoutMilliseconds) noexcept;
64 
65  //==============================================================================
66  bool readSamples (int* const* destSamples, int numDestChannels, int startOffsetInDestBuffer,
67  int64 startSampleInFile, int numSamples) override;
68 
69 private:
70  struct BufferedBlock
71  {
72  BufferedBlock (AudioFormatReader& reader, int64 pos, int numSamples);
73 
74  Range<int64> range;
75  AudioBuffer<float> buffer;
76  bool allSamplesRead = false;
77  };
78 
79  int useTimeSlice() override;
80  BufferedBlock* getBlockContaining (int64 pos) const noexcept;
81  bool readNextBufferChunk();
82 
83  static constexpr int samplesPerBlock = 32768;
84 
85  std::unique_ptr<AudioFormatReader> source;
86  TimeSliceThread& thread;
87  std::atomic<int64> nextReadPosition { 0 };
88  const int numBlocks;
89  int timeoutMs = 0;
90 
91  CriticalSection lock;
93 
94  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioReader)
95 };
96 
97 } // namespace juce