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 "platform/audio/AudioBus.h" 29 #include "modules/webaudio/AudioBuffer.h" 30 #include "modules/webaudio/AudioParam.h" 31 #include "modules/webaudio/AudioScheduledSourceNode.h" 32 #include "modules/webaudio/PannerNode.h" 33 #include "wtf/OwnPtr.h" 34 #include "wtf/PassRefPtr.h" 35 #include "wtf/RefPtr.h" 36 #include "wtf/Threading.h" 37 38 namespace blink { 39 40 class 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 45 class AudioBufferSourceNode FINAL : public AudioScheduledSourceNode { 46 DEFINE_WRAPPERTYPEINFO(); 47 public: 48 static AudioBufferSourceNode* create(AudioContext*, float sampleRate); 49 50 virtual ~AudioBufferSourceNode(); 51 52 // AudioNode 53 virtual void dispose() OVERRIDE; 54 virtual void process(size_t framesToProcess) OVERRIDE; 55 56 // setBuffer() is called on the main thread. This is the buffer we use for playback. 57 void setBuffer(AudioBuffer*, ExceptionState&); buffer()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 start(ExceptionState & exceptionState)65 void start(ExceptionState& exceptionState) { start(0, exceptionState); } 66 void start(double when, ExceptionState&); 67 void start(double when, double grainOffset, ExceptionState&); 68 void start(double when, double grainOffset, double grainDuration, ExceptionState&); 69 70 // Note: the attribute was originally exposed as .looping, but to be more consistent in naming with <audio> 71 // and with how it's described in the specification, the proper attribute name is .loop 72 // The old attribute is kept for backwards compatibility. loop()73 bool loop() const { return m_isLooping; } setLoop(bool looping)74 void setLoop(bool looping) { m_isLooping = looping; } 75 76 // Loop times in seconds. loopStart()77 double loopStart() const { return m_loopStart; } loopEnd()78 double loopEnd() const { return m_loopEnd; } setLoopStart(double loopStart)79 void setLoopStart(double loopStart) { m_loopStart = loopStart; } setLoopEnd(double loopEnd)80 void setLoopEnd(double loopEnd) { m_loopEnd = loopEnd; } 81 playbackRate()82 AudioParam* playbackRate() { return m_playbackRate.get(); } 83 84 // If a panner node is set, then we can incorporate doppler shift into the playback pitch rate. 85 void setPannerNode(PannerNode*); 86 void clearPannerNode(); 87 88 // If we are no longer playing, propogate silence ahead to downstream nodes. 89 virtual bool propagatesSilence() const OVERRIDE; 90 91 // AudioScheduledSourceNode 92 virtual void finish() OVERRIDE; 93 94 virtual void trace(Visitor*) OVERRIDE; 95 96 private: 97 AudioBufferSourceNode(AudioContext*, float sampleRate); 98 99 // Returns true on success. 100 bool renderFromBuffer(AudioBus*, unsigned destinationFrameOffset, size_t numberOfFrames); 101 102 // Render silence starting from "index" frame in AudioBus. 103 inline bool renderSilenceAndFinishIfNotLooping(AudioBus*, unsigned index, size_t framesToProcess); 104 105 // m_buffer holds the sample data which this node outputs. 106 Member<AudioBuffer> m_buffer; 107 108 // Pointers for the buffer and destination. 109 OwnPtr<const float*[]> m_sourceChannels; 110 OwnPtr<float*[]> m_destinationChannels; 111 112 // Used for the "playbackRate" attributes. 113 Member<AudioParam> m_playbackRate; 114 115 // 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. 116 // If true, it will wrap around to the start of the buffer each time it reaches the end. 117 bool m_isLooping; 118 119 double m_loopStart; 120 double m_loopEnd; 121 122 // m_virtualReadIndex is a sample-frame index into our buffer representing the current playback position. 123 // Since it's floating-point, it has sub-sample accuracy. 124 double m_virtualReadIndex; 125 126 // Granular playback 127 bool m_isGrain; 128 double m_grainOffset; // in seconds 129 double m_grainDuration; // in seconds 130 131 // totalPitchRate() returns the instantaneous pitch rate (non-time preserving). 132 // It incorporates the base pitch rate, any sample-rate conversion factor from the buffer, and any doppler shift from an associated panner node. 133 double totalPitchRate(); 134 135 // We optionally keep track of a panner node which has a doppler shift that 136 // is incorporated into the pitch rate. 137 // This RefPtr is connection reference. We must call AudioNode:: 138 // makeConnection() after ref(), and call AudioNode::breakConnection() 139 // before deref(). 140 // Oilpan: This holds connection references. We must call 141 // AudioNode::makeConnection when we add an AudioNode to this, and must call 142 // AudioNode::breakConnection() when we remove an AudioNode from this. 143 Member<PannerNode> m_pannerNode; 144 145 // This synchronizes process() with setBuffer() which can cause dynamic channel count changes. 146 mutable Mutex m_processLock; 147 }; 148 149 } // namespace blink 150 151 #endif // AudioBufferSourceNode_h 152