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 "AudioParam.h"
31#include "AudioScheduledSourceNode.h"
32#include "PannerNode.h"
33#include <wtf/OwnArrayPtr.h>
34#include <wtf/PassRefPtr.h>
35#include <wtf/RefPtr.h>
36#include <wtf/Threading.h>
37
38namespace WebCore {
39
40class AudioContext;
41
42// AudioBufferSourceNode is an AudioNode representing an audio source from an in-memory audio asset represented by an AudioBuffer.
43// It generally will be used for short sounds which require a high degree of scheduling flexibility (can playback in rhythmically perfect ways).
44
45class AudioBufferSourceNode : public AudioScheduledSourceNode {
46public:
47    static PassRefPtr<AudioBufferSourceNode> create(AudioContext*, float sampleRate);
48
49    virtual ~AudioBufferSourceNode();
50
51    // AudioNode
52    virtual void process(size_t framesToProcess);
53    virtual void reset();
54
55    // setBuffer() is called on the main thread.  This is the buffer we use for playback.
56    // returns true on success.
57    bool setBuffer(AudioBuffer*);
58    AudioBuffer* buffer() { return m_buffer.get(); }
59
60    // numberOfChannels() returns the number of output channels.  This value equals the number of channels from the buffer.
61    // If a new buffer is set with a different number of channels, then this value will dynamically change.
62    unsigned numberOfChannels();
63
64    // Play-state
65    void startGrain(double when, double grainOffset);
66    void startGrain(double when, double grainOffset, double grainDuration);
67
68#if ENABLE(LEGACY_WEB_AUDIO)
69    void noteGrainOn(double when, double grainOffset, double grainDuration);
70#endif
71
72    // Note: the attribute was originally exposed as .looping, but to be more consistent in naming with <audio>
73    // and with how it's described in the specification, the proper attribute name is .loop
74    // The old attribute is kept for backwards compatibility.
75    bool loop() const { return m_isLooping; }
76    void setLoop(bool looping) { m_isLooping = looping; }
77
78    // Loop times in seconds.
79    double loopStart() const { return m_loopStart; }
80    double loopEnd() const { return m_loopEnd; }
81    void setLoopStart(double loopStart) { m_loopStart = loopStart; }
82    void setLoopEnd(double loopEnd) { m_loopEnd = loopEnd; }
83
84    // Deprecated.
85    bool looping();
86    void setLooping(bool);
87
88    AudioParam* gain() { return m_gain.get(); }
89    AudioParam* playbackRate() { return m_playbackRate.get(); }
90
91    // If a panner node is set, then we can incorporate doppler shift into the playback pitch rate.
92    void setPannerNode(PannerNode*);
93    void clearPannerNode();
94
95    // If we are no longer playing, propogate silence ahead to downstream nodes.
96    virtual bool propagatesSilence() const;
97
98    // AudioScheduledSourceNode
99    virtual void finish() OVERRIDE;
100
101private:
102    AudioBufferSourceNode(AudioContext*, float sampleRate);
103
104    virtual double tailTime() const OVERRIDE { return 0; }
105    virtual double latencyTime() const OVERRIDE { return 0; }
106
107    // Returns true on success.
108    bool renderFromBuffer(AudioBus*, unsigned destinationFrameOffset, size_t numberOfFrames);
109
110    // Render silence starting from "index" frame in AudioBus.
111    inline bool renderSilenceAndFinishIfNotLooping(AudioBus*, unsigned index, size_t framesToProcess);
112
113    // m_buffer holds the sample data which this node outputs.
114    RefPtr<AudioBuffer> m_buffer;
115
116    // Pointers for the buffer and destination.
117    OwnArrayPtr<const float*> m_sourceChannels;
118    OwnArrayPtr<float*> m_destinationChannels;
119
120    // Used for the "gain" and "playbackRate" attributes.
121    RefPtr<AudioParam> m_gain;
122    RefPtr<AudioParam> m_playbackRate;
123
124    // 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.
125    // If true, it will wrap around to the start of the buffer each time it reaches the end.
126    bool m_isLooping;
127
128    double m_loopStart;
129    double m_loopEnd;
130
131    // m_virtualReadIndex is a sample-frame index into our buffer representing the current playback position.
132    // Since it's floating-point, it has sub-sample accuracy.
133    double m_virtualReadIndex;
134
135    // Granular playback
136    bool m_isGrain;
137    double m_grainOffset; // in seconds
138    double m_grainDuration; // in seconds
139
140    // totalPitchRate() returns the instantaneous pitch rate (non-time preserving).
141    // It incorporates the base pitch rate, any sample-rate conversion factor from the buffer, and any doppler shift from an associated panner node.
142    double totalPitchRate();
143
144    // m_lastGain provides continuity when we dynamically adjust the gain.
145    float m_lastGain;
146
147    // We optionally keep track of a panner node which has a doppler shift that is incorporated into
148    // the pitch rate. We manually manage ref-counting because we want to use RefTypeConnection.
149    PannerNode* m_pannerNode;
150
151    // This synchronizes process() with setBuffer() which can cause dynamic channel count changes.
152    mutable Mutex m_processLock;
153};
154
155} // namespace WebCore
156
157#endif // AudioBufferSourceNode_h
158