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