2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_AUDIOTRANSPORTSOURCE_JUCEHEADER__
27 #define __JUCE_AUDIOTRANSPORTSOURCE_JUCEHEADER__
29 #include "juce_BufferingAudioSource.h"
30 #include "juce_ResamplingAudioSource.h"
31 #include "../../events/juce_ChangeBroadcaster.h"
34 //==============================================================================
36 An AudioSource that takes a PositionableAudioSource and allows it to be
37 played, stopped, started, etc.
39 This can also be told use a buffer and background thread to read ahead, and
40 if can correct for different sample-rates.
42 You may want to use one of these along with an AudioSourcePlayer and AudioIODevice
43 to control playback of an audio file.
45 @see AudioSource, AudioSourcePlayer
47 class JUCE_API AudioTransportSource
: public PositionableAudioSource
,
48 public ChangeBroadcaster
51 //==============================================================================
52 /** Creates an AudioTransportSource.
54 After creating one of these, use the setSource() method to select an input source.
56 AudioTransportSource();
59 ~AudioTransportSource();
61 //==============================================================================
62 /** Sets the reader that is being used as the input source.
64 This will stop playback, reset the position to 0 and change to the new reader.
66 The source passed in will not be deleted by this object, so must be managed by
69 @param newSource the new input source to use. This may be zero
70 @param readAheadBufferSize a size of buffer to use for reading ahead. If this
71 is zero, no reading ahead will be done; if it's
72 greater than zero, a BufferingAudioSource will be used
73 to do the reading-ahead
74 @param sourceSampleRateToCorrectFor if this is non-zero, it specifies the sample
75 rate of the source, and playback will be sample-rate
76 adjusted to maintain playback at the correct pitch. If
77 this is 0, no sample-rate adjustment will be performed
78 @param maxNumChannels the maximum number of channels that may need to be played
80 void setSource (PositionableAudioSource
* newSource
,
81 int readAheadBufferSize
= 0,
82 double sourceSampleRateToCorrectFor
= 0.0,
83 int maxNumChannels
= 2);
85 //==============================================================================
86 /** Changes the current playback position in the source stream.
88 The next time the getNextAudioBlock() method is called, this
89 is the time from which it'll read data.
93 void setPosition (double newPosition
);
95 /** Returns the position that the next data block will be read from
97 This is a time in seconds.
99 double getCurrentPosition() const;
101 /** Returns the stream's length in seconds. */
102 double getLengthInSeconds() const;
104 /** Returns true if the player has stopped because its input stream ran out of data.
106 bool hasStreamFinished() const noexcept
{ return inputStreamEOF
; }
108 //==============================================================================
109 /** Starts playing (if a source has been selected).
111 If it starts playing, this will send a message to any ChangeListeners
112 that are registered with this object.
118 If it's actually playing, this will send a message to any ChangeListeners
119 that are registered with this object.
123 /** Returns true if it's currently playing. */
124 bool isPlaying() const noexcept
{ return playing
; }
126 //==============================================================================
127 /** Changes the gain to apply to the output.
129 @param newGain a factor by which to multiply the outgoing samples,
130 so 1.0 = 0dB, 0.5 = -6dB, 2.0 = 6dB, etc.
132 void setGain (float newGain
) noexcept
;
134 /** Returns the current gain setting.
138 float getGain() const noexcept
{ return gain
; }
141 //==============================================================================
142 /** Implementation of the AudioSource method. */
143 void prepareToPlay (int samplesPerBlockExpected
, double sampleRate
);
145 /** Implementation of the AudioSource method. */
146 void releaseResources();
148 /** Implementation of the AudioSource method. */
149 void getNextAudioBlock (const AudioSourceChannelInfo
& bufferToFill
);
151 //==============================================================================
152 /** Implements the PositionableAudioSource method. */
153 void setNextReadPosition (int64 newPosition
);
155 /** Implements the PositionableAudioSource method. */
156 int64
getNextReadPosition() const;
158 /** Implements the PositionableAudioSource method. */
159 int64
getTotalLength() const;
161 /** Implements the PositionableAudioSource method. */
162 bool isLooping() const;
165 //==============================================================================
166 PositionableAudioSource
* source
;
167 ResamplingAudioSource
* resamplerSource
;
168 BufferingAudioSource
* bufferingSource
;
169 PositionableAudioSource
* positionableSource
;
170 AudioSource
* masterSource
;
172 CriticalSection callbackLock
;
173 float volatile gain
, lastGain
;
174 bool volatile playing
, stopped
;
175 double sampleRate
, sourceSampleRate
;
176 int blockSize
, readAheadBufferSize
;
177 bool isPrepared
, inputStreamEOF
;
179 void releaseMasterResources();
181 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioTransportSource
);
185 #endif // __JUCE_AUDIOTRANSPORTSOURCE_JUCEHEADER__