• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #ifndef AudioBufferSourceNode_h
26 #define AudioBufferSourceNode_h
27 
28 #include "AudioBuffer.h"
29 #include "AudioBus.h"
30 #include "AudioGain.h"
31 #include "AudioPannerNode.h"
32 #include "AudioResampler.h"
33 #include "AudioSourceNode.h"
34 #include "AudioSourceProvider.h"
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefPtr.h>
37 #include <wtf/Threading.h>
38 
39 namespace WebCore {
40 
41 class AudioContext;
42 
43 // AudioBufferSourceNode is an AudioNode representing an audio source from an in-memory audio asset represented by an AudioBuffer.
44 // It generally will be used for short sounds which require a high degree of scheduling flexibility (can playback in rhythmically perfect ways).
45 
46 class AudioBufferSourceNode : public AudioSourceNode, public AudioSourceProvider {
47 public:
48     static PassRefPtr<AudioBufferSourceNode> create(AudioContext*, double sampleRate);
49 
50     virtual ~AudioBufferSourceNode();
51 
52     // AudioNode
53     virtual void process(size_t framesToProcess);
54     virtual void reset();
55 
56     // AudioSourceProvider
57     // When process() is called, the resampler calls provideInput (in the audio thread) to gets its input stream.
58     virtual void provideInput(AudioBus*, size_t numberOfFrames);
59 
60     // setBuffer() is called on the main thread.  This is the buffer we use for playback.
61     void setBuffer(AudioBuffer*);
buffer()62     AudioBuffer* buffer() { return m_buffer.get(); }
63 
64     // numberOfChannels() returns the number of output channels.  This value equals the number of channels from the buffer.
65     // If a new buffer is set with a different number of channels, then this value will dynamically change.
66     unsigned numberOfChannels();
67 
68     // Play-state
69     // noteOn(), noteGrainOn(), and noteOff() must all be called from the main thread.
70     void noteOn(double when);
71     void noteGrainOn(double when, double grainOffset, double grainDuration);
72     void noteOff(double when);
73 
looping()74     bool looping() const { return m_isLooping; }
setLooping(bool looping)75     void setLooping(bool looping) { m_isLooping = looping; }
76 
gain()77     AudioGain* gain() { return m_gain.get(); }
playbackRate()78     AudioParam* playbackRate() { return m_playbackRate.get(); }
79 
80     // If a panner node is set, then we can incorporate doppler shift into the playback pitch rate.
setPannerNode(PassRefPtr<AudioPannerNode> pannerNode)81     void setPannerNode(PassRefPtr<AudioPannerNode> pannerNode) { m_pannerNode = pannerNode; }
82 
83 private:
84     AudioBufferSourceNode(AudioContext*, double sampleRate);
85 
86     // m_buffer holds the sample data which this node outputs.
87     RefPtr<AudioBuffer> m_buffer;
88 
89     // Used for the "gain" and "playbackRate" attributes.
90     RefPtr<AudioGain> m_gain;
91     RefPtr<AudioParam> m_playbackRate;
92 
93     // m_isPlaying is set to true when noteOn() or noteGrainOn() is called.
94     bool m_isPlaying;
95 
96     // If m_isLooping is false, then this node will be done playing and become inactive after it reaches the end of the sample data in the buffer.
97     // If true, it will wrap around to the start of the buffer each time it reaches the end.
98     bool m_isLooping;
99 
100     // This node is considered finished when it reaches the end of the buffer's sample data after noteOn() has been called.
101     // This will only be set to true if m_isLooping == false.
102     bool m_hasFinished;
103 
104     // m_startTime is the time to start playing based on the context's timeline (0.0 or a time less than the context's current time means "now").
105     double m_startTime; // in seconds
106 
107     // m_schedulingFrameDelay is the sample-accurate scheduling offset.
108     // It's used so that we start rendering audio samples at a very precise point in time.
109     // It will only be a non-zero value the very first render quantum that we render from the buffer.
110     int m_schedulingFrameDelay;
111 
112     // m_readIndex is a sample-frame index into our buffer representing the current playback position.
113     unsigned m_readIndex;
114 
115     // Granular playback
116     bool m_isGrain;
117     double m_grainOffset; // in seconds
118     double m_grainDuration; // in seconds
119     int m_grainFrameCount; // keeps track of which frame in the grain we're currently rendering
120 
121     // totalPitchRate() returns the instantaneous pitch rate (non-time preserving).
122     // It incorporates the base pitch rate, any sample-rate conversion factor from the buffer, and any doppler shift from an associated panner node.
123     double totalPitchRate();
124 
125     // m_resampler performs the pitch rate changes to the buffer playback.
126     AudioResampler m_resampler;
127 
128     // m_lastGain provides continuity when we dynamically adjust the gain.
129     double m_lastGain;
130 
131     // We optionally keep track of a panner node which has a doppler shift that is incorporated into the pitch rate.
132     RefPtr<AudioPannerNode> m_pannerNode;
133 
134     // This synchronizes process() with setBuffer() which can cause dynamic channel count changes.
135     mutable Mutex m_processLock;
136 
137     // Reads the next framesToProcess sample-frames from the AudioBuffer into destinationBus.
138     // A grain envelope will be applied if m_isGrain is set to true.
139     void readFromBuffer(AudioBus* destinationBus, size_t framesToProcess);
140 
141     // readFromBufferWithGrainEnvelope() is a low-level blitter which reads from the AudioBuffer and applies a grain envelope.
142     void readFromBufferWithGrainEnvelope(float* sourceL, float* sourceR, float* destinationL, float* destinationR, size_t framesToProcess);
143 };
144 
145 } // namespace WebCore
146 
147 #endif // AudioBufferSourceNode_h
148