OpenShot Library | OpenShotAudio  0.2.1
juce_BufferingAudioSource.h
1 
2 /** @weakgroup juce_audio_basics-sources
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  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
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 //==============================================================================
31 /**
32  An AudioSource which takes another source as input, and buffers it using a thread.
33 
34  Create this as a wrapper around another thread, and it will read-ahead with
35  a background thread to smooth out playback. You can either create one of these
36  directly, or use it indirectly using an AudioTransportSource.
37 
38  @see PositionableAudioSource, AudioTransportSource
39 
40  @tags{Audio}
41 */
43  private TimeSliceClient
44 {
45 public:
46  //==============================================================================
47  /** Creates a BufferingAudioSource.
48 
49  @param source the input source to read from
50  @param backgroundThread a background thread that will be used for the
51  background read-ahead. This object must not be deleted
52  until after any BufferingAudioSources that are using it
53  have been deleted!
54  @param deleteSourceWhenDeleted if true, then the input source object will
55  be deleted when this object is deleted
56  @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
57  @param numberOfChannels the number of channels that will be played
58  @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
59  block until the buffer has been filled
60  */
62  TimeSliceThread& backgroundThread,
63  bool deleteSourceWhenDeleted,
64  int numberOfSamplesToBuffer,
65  int numberOfChannels = 2,
66  bool prefillBufferOnPrepareToPlay = true);
67 
68  /** Destructor.
69 
70  The input source may be deleted depending on whether the deleteSourceWhenDeleted
71  flag was set in the constructor.
72  */
73  ~BufferingAudioSource() override;
74 
75  //==============================================================================
76  /** Implementation of the AudioSource method. */
77  void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
78 
79  /** Implementation of the AudioSource method. */
80  void releaseResources() override;
81 
82  /** Implementation of the AudioSource method. */
83  void getNextAudioBlock (const AudioSourceChannelInfo&) override;
84 
85  //==============================================================================
86  /** Implements the PositionableAudioSource method. */
87  void setNextReadPosition (int64 newPosition) override;
88 
89  /** Implements the PositionableAudioSource method. */
90  int64 getNextReadPosition() const override;
91 
92  /** Implements the PositionableAudioSource method. */
93  int64 getTotalLength() const override { return source->getTotalLength(); }
94 
95  /** Implements the PositionableAudioSource method. */
96  bool isLooping() const override { return source->isLooping(); }
97 
98  /** A useful function to block until the next the buffer info can be filled.
99 
100  This is useful for offline rendering.
101  */
102  bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
103 
104 private:
105  //==============================================================================
107  TimeSliceThread& backgroundThread;
108  int numberOfSamplesToBuffer, numberOfChannels;
109  AudioBuffer<float> buffer;
110  CriticalSection bufferStartPosLock;
111  WaitableEvent bufferReadyEvent;
112  std::atomic<int64> bufferValidStart { 0 }, bufferValidEnd { 0 }, nextPlayPos { 0 };
113  double sampleRate = 0;
114  bool wasSourceLooping = false, isPrepared = false, prefillBuffer;
115 
116  bool readNextBufferChunk();
117  void readBufferSection (int64 start, int length, int bufferOffset);
118  int useTimeSlice() override;
119 
120  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
121 };
122 
123 } // namespace juce
124 
125 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
Allows threads to wait for events triggered by other threads.
virtual int64 getTotalLength() const =0
Returns the total length of the stream (in samples).
int64 getTotalLength() const override
Implements the PositionableAudioSource method.
Used by the TimeSliceThread class.
Holds a pointer to an object which can optionally be deleted when this pointer goes out of scope...
An AudioSource which takes another source as input, and buffers it using a thread.
A thread that keeps a list of clients, and calls each one in turn, giving them all a chance to run so...
virtual bool isLooping() const =0
Returns true if this source is actually playing in a loop.
A type of AudioSource which can be repositioned.
A re-entrant mutex.
bool isLooping() const override
Implements the PositionableAudioSource method.
Used by AudioSource::getNextAudioBlock().