2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "bindings/v8/ScriptWrappable.h"
29 #include "core/events/EventTarget.h"
30 #include "platform/audio/AudioBus.h"
31 #include "wtf/Forward.h"
32 #include "wtf/OwnPtr.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/RefPtr.h"
35 #include "wtf/Vector.h"
37 #define DEBUG_AUDIONODE_REFERENCES 0
43 class AudioNodeOutput;
47 // An AudioNode is the basic building block for handling audio within an AudioContext.
48 // It may be an audio source, an intermediate processing module, or an audio destination.
49 // Each AudioNode can have inputs and/or outputs. An AudioSourceNode has no inputs and a single output.
50 // An AudioDestinationNode has one input and no outputs and represents the final destination to the audio hardware.
51 // Most processing nodes such as filters will have one input and one output, although multiple inputs and outputs are possible.
53 class AudioNode : public ScriptWrappable, public EventTargetWithInlineData {
55 enum { ProcessingSizeInFrames = 128 };
57 AudioNode(AudioContext*, float sampleRate);
60 AudioContext* context() { return m_context.get(); }
61 const AudioContext* context() const { return m_context.get(); }
67 NodeTypeAudioBufferSource,
68 NodeTypeMediaElementAudioSource,
69 NodeTypeMediaStreamAudioDestination,
70 NodeTypeMediaStreamAudioSource,
77 NodeTypeChannelSplitter,
78 NodeTypeChannelMerger,
80 NodeTypeDynamicsCompressor,
85 enum ChannelCountMode {
91 NodeType nodeType() const { return m_nodeType; }
92 String nodeTypeName() const;
93 void setNodeType(NodeType);
95 // We handle our own ref-counting because of the threading issues and subtle nature of
96 // how AudioNodes can continue processing (playing one-shot sound) after there are no more
97 // JavaScript references to the object.
98 enum RefType { RefTypeNormal, RefTypeConnection };
100 // Can be called from main thread or context's audio thread.
101 void ref(RefType refType = RefTypeNormal);
102 void deref(RefType refType = RefTypeNormal);
104 // Can be called from main thread or context's audio thread. It must be called while the context's graph lock is held.
105 void finishDeref(RefType refType);
107 // The AudioNodeInput(s) (if any) will already have their input data available when process() is called.
108 // Subclasses will take this input data and put the results in the AudioBus(s) of its AudioNodeOutput(s) (if any).
109 // Called from context's audio thread.
110 virtual void process(size_t framesToProcess) = 0;
112 // No significant resources should be allocated until initialize() is called.
113 // Processing may not occur until a node is initialized.
114 virtual void initialize();
115 virtual void uninitialize();
117 bool isInitialized() const { return m_isInitialized; }
119 unsigned numberOfInputs() const { return m_inputs.size(); }
120 unsigned numberOfOutputs() const { return m_outputs.size(); }
122 AudioNodeInput* input(unsigned);
123 AudioNodeOutput* output(unsigned);
125 // Called from main thread by corresponding JavaScript methods.
126 virtual void connect(AudioNode*, unsigned outputIndex, unsigned inputIndex, ExceptionState&);
127 void connect(AudioParam*, unsigned outputIndex, ExceptionState&);
128 virtual void disconnect(unsigned outputIndex, ExceptionState&);
130 virtual float sampleRate() const { return m_sampleRate; }
132 // processIfNecessary() is called by our output(s) when the rendering graph needs this AudioNode to process.
133 // This method ensures that the AudioNode will only process once per rendering time quantum even if it's called repeatedly.
134 // This handles the case of "fanout" where an output is connected to multiple AudioNode inputs.
135 // Called from context's audio thread.
136 void processIfNecessary(size_t framesToProcess);
138 // Called when a new connection has been made to one of our inputs or the connection number of channels has changed.
139 // This potentially gives us enough information to perform a lazy initialization or, if necessary, a re-initialization.
140 // Called from main thread.
141 virtual void checkNumberOfChannelsForInput(AudioNodeInput*);
143 #if DEBUG_AUDIONODE_REFERENCES
144 static void printNodeCounts();
147 bool isMarkedForDeletion() const { return m_isMarkedForDeletion; }
149 // tailTime() is the length of time (not counting latency time) where non-zero output may occur after continuous silent input.
150 virtual double tailTime() const = 0;
151 // latencyTime() is the length of time it takes for non-zero output to appear after non-zero input is provided. This only applies to
152 // processing delay which is an artifact of the processing algorithm chosen and is *not* part of the intrinsic desired effect. For
153 // example, a "delay" effect is expected to delay the signal, and thus would not be considered latency.
154 virtual double latencyTime() const = 0;
156 // propagatesSilence() should return true if the node will generate silent output when given silent input. By default, AudioNode
157 // will take tailTime() and latencyTime() into account when determining whether the node will propagate silence.
158 virtual bool propagatesSilence() const;
159 bool inputsAreSilent();
160 void silenceOutputs();
161 void unsilenceOutputs();
163 void enableOutputsIfNecessary();
164 void disableOutputsIfNecessary();
166 unsigned long channelCount();
167 virtual void setChannelCount(unsigned long, ExceptionState&);
169 String channelCountMode();
170 void setChannelCountMode(const String&, ExceptionState&);
172 String channelInterpretation();
173 void setChannelInterpretation(const String&, ExceptionState&);
175 ChannelCountMode internalChannelCountMode() const { return m_channelCountMode; }
176 AudioBus::ChannelInterpretation internalChannelInterpretation() const { return m_channelInterpretation; }
179 virtual const AtomicString& interfaceName() const OVERRIDE FINAL;
180 virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
183 // Inputs and outputs must be created before the AudioNode is initialized.
184 void addInput(PassOwnPtr<AudioNodeInput>);
185 void addOutput(PassOwnPtr<AudioNodeOutput>);
187 // Called by processIfNecessary() to cause all parts of the rendering graph connected to us to process.
188 // Each rendering quantum, the audio data for each of the AudioNode's inputs will be available after this method is called.
189 // Called from context's audio thread.
190 virtual void pullInputs(size_t framesToProcess);
192 // Force all inputs to take any channel interpretation changes into account.
193 void updateChannelsForInputs();
196 volatile bool m_isInitialized;
198 RefPtr<AudioContext> m_context;
200 Vector<OwnPtr<AudioNodeInput> > m_inputs;
201 Vector<OwnPtr<AudioNodeOutput> > m_outputs;
203 double m_lastProcessingTime;
204 double m_lastNonSilentTime;
207 volatile int m_normalRefCount;
208 volatile int m_connectionRefCount;
210 bool m_isMarkedForDeletion;
213 #if DEBUG_AUDIONODE_REFERENCES
214 static bool s_isNodeCountInitialized;
215 static int s_nodeCount[NodeTypeEnd];
218 virtual void refEventTarget() OVERRIDE FINAL { ref(); }
219 virtual void derefEventTarget() OVERRIDE FINAL { deref(); }
222 unsigned m_channelCount;
223 ChannelCountMode m_channelCountMode;
224 AudioBus::ChannelInterpretation m_channelInterpretation;
227 } // namespace WebCore
229 #endif // AudioNode_h