OpenShot Library | libopenshot-audio  0.2.0
juce_BufferingAudioFormatReader.h
1 
2 /** @weakgroup juce_audio_formats-format
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16  27th April 2017).
17 
18  End User License Agreement: www.juce.com/juce-5-licence
19  Privacy Policy: www.juce.com/juce-5-privacy-policy
20 
21  Or: You may also use this code under the terms of the GPL v3 (see
22  www.gnu.org/licenses).
23 
24  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26  DISCLAIMED.
27 
28  ==============================================================================
29 */
30 
31 namespace juce
32 {
33 
34 //==============================================================================
35 /**
36  An AudioFormatReader that uses a background thread to pre-read data from
37  another reader.
38 
39  @see AudioFormatReader
40 
41  @tags{Audio}
42 */
44  private TimeSliceClient
45 {
46 public:
47  /** Creates a reader.
48 
49  @param sourceReader the source reader to wrap. This BufferingAudioReader
50  takes ownership of this object and will delete it later
51  when no longer needed
52  @param timeSliceThread the thread that should be used to do the background reading.
53  Make sure that the thread you supply is running, and won't
54  be deleted while the reader object still exists.
55  @param samplesToBuffer the total number of samples to buffer ahead.
56  */
58  TimeSliceThread& timeSliceThread,
59  int samplesToBuffer);
60 
61  ~BufferingAudioReader() override;
62 
63  /** Sets a number of milliseconds that the reader can block for in its readSamples()
64  method before giving up and returning silence.
65  A value of less that 0 means "wait forever".
66  The default timeout is 0.
67  */
68  void setReadTimeout (int timeoutMilliseconds) noexcept;
69 
70  bool readSamples (int** destSamples, int numDestChannels, int startOffsetInDestBuffer,
71  int64 startSampleInFile, int numSamples) override;
72 
73 private:
74  std::unique_ptr<AudioFormatReader> source;
75  TimeSliceThread& thread;
76  std::atomic<int64> nextReadPosition { 0 };
77  const int numBlocks;
78  int timeoutMs = 0;
79 
80  enum { samplesPerBlock = 32768 };
81 
82  struct BufferedBlock
83  {
84  BufferedBlock (AudioFormatReader& reader, int64 pos, int numSamples);
85 
86  Range<int64> range;
87  AudioBuffer<float> buffer;
88  };
89 
90  CriticalSection lock;
92 
93  BufferedBlock* getBlockContaining (int64 pos) const noexcept;
94  int useTimeSlice() override;
95  bool readNextBufferChunk();
96 
97  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioReader)
98 };
99 
100 } // namespace juce
101 
102 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
An AudioFormatReader that uses a background thread to pre-read data from another reader.
Used by the TimeSliceThread class.
A thread that keeps a list of clients, and calls each one in turn, giving them all a chance to run so...
A re-entrant mutex.
Reads samples from an audio file stream.