Home | History | Annotate | Download | only in webaudio
      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 WebCore {
     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 public:
     47     static PassRefPtrWillBeRawPtr<AudioBufferSourceNode> create(AudioContext*, float sampleRate);
     48 
     49     virtual ~AudioBufferSourceNode();
     50 
     51     // AudioNode
     52     virtual void process(size_t framesToProcess) OVERRIDE;
     53 
     54     // setBuffer() is called on the main thread. This is the buffer we use for playback.
     55     void setBuffer(AudioBuffer*, ExceptionState&);
     56     AudioBuffer* buffer() { return m_buffer.get(); }
     57 
     58     // numberOfChannels() returns the number of output channels.  This value equals the number of channels from the buffer.
     59     // If a new buffer is set with a different number of channels, then this value will dynamically change.
     60     unsigned numberOfChannels();
     61 
     62     // Play-state
     63     void start(ExceptionState& exceptionState) { start(0, exceptionState); }
     64     void start(double when, ExceptionState&);
     65     void start(double when, double grainOffset, ExceptionState&);
     66     void start(double when, double grainOffset, double grainDuration, ExceptionState&);
     67 
     68     // Note: the attribute was originally exposed as .looping, but to be more consistent in naming with <audio>
     69     // and with how it's described in the specification, the proper attribute name is .loop
     70     // The old attribute is kept for backwards compatibility.
     71     bool loop() const { return m_isLooping; }
     72     void setLoop(bool looping) { m_isLooping = looping; }
     73 
     74     // Loop times in seconds.
     75     double loopStart() const { return m_loopStart; }
     76     double loopEnd() const { return m_loopEnd; }
     77     void setLoopStart(double loopStart) { m_loopStart = loopStart; }
     78     void setLoopEnd(double loopEnd) { m_loopEnd = loopEnd; }
     79 
     80     AudioParam* playbackRate() { return m_playbackRate.get(); }
     81 
     82     // If a panner node is set, then we can incorporate doppler shift into the playback pitch rate.
     83     void setPannerNode(PannerNode*);
     84     void clearPannerNode();
     85 
     86     // If we are no longer playing, propogate silence ahead to downstream nodes.
     87     virtual bool propagatesSilence() const OVERRIDE;
     88 
     89     // AudioScheduledSourceNode
     90     virtual void finish() OVERRIDE;
     91 
     92     virtual void trace(Visitor*) OVERRIDE;
     93 
     94 private:
     95     AudioBufferSourceNode(AudioContext*, float sampleRate);
     96 
     97     // Returns true on success.
     98     bool renderFromBuffer(AudioBus*, unsigned destinationFrameOffset, size_t numberOfFrames);
     99 
    100     // Render silence starting from "index" frame in AudioBus.
    101     inline bool renderSilenceAndFinishIfNotLooping(AudioBus*, unsigned index, size_t framesToProcess);
    102 
    103     // m_buffer holds the sample data which this node outputs.
    104     RefPtrWillBeMember<AudioBuffer> m_buffer;
    105 
    106     // Pointers for the buffer and destination.
    107     OwnPtr<const float*[]> m_sourceChannels;
    108     OwnPtr<float*[]> m_destinationChannels;
    109 
    110     // Used for the "playbackRate" attributes.
    111     RefPtrWillBeMember<AudioParam> m_playbackRate;
    112 
    113     // 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.
    114     // If true, it will wrap around to the start of the buffer each time it reaches the end.
    115     bool m_isLooping;
    116 
    117     double m_loopStart;
    118     double m_loopEnd;
    119 
    120     // m_virtualReadIndex is a sample-frame index into our buffer representing the current playback position.
    121     // Since it's floating-point, it has sub-sample accuracy.
    122     double m_virtualReadIndex;
    123 
    124     // Granular playback
    125     bool m_isGrain;
    126     double m_grainOffset; // in seconds
    127     double m_grainDuration; // in seconds
    128 
    129     // totalPitchRate() returns the instantaneous pitch rate (non-time preserving).
    130     // It incorporates the base pitch rate, any sample-rate conversion factor from the buffer, and any doppler shift from an associated panner node.
    131     double totalPitchRate();
    132 
    133     // We optionally keep track of a panner node which has a doppler shift that is incorporated into
    134     // the pitch rate. We manually manage ref-counting because we want to use RefTypeConnection.
    135     PannerNode* m_pannerNode;
    136 
    137     // This synchronizes process() with setBuffer() which can cause dynamic channel count changes.
    138     mutable Mutex m_processLock;
    139 };
    140 
    141 } // namespace WebCore
    142 
    143 #endif // AudioBufferSourceNode_h
    144