8bf98958866bcfb454808e36db0afaa9a729f0a6
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / AudioNode.h
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 AudioNode_h
26 #define AudioNode_h
27
28 #include "modules/EventTargetModules.h"
29 #include "platform/audio/AudioBus.h"
30 #include "wtf/Forward.h"
31 #include "wtf/OwnPtr.h"
32 #include "wtf/PassOwnPtr.h"
33 #include "wtf/RefPtr.h"
34 #include "wtf/Vector.h"
35
36 #define DEBUG_AUDIONODE_REFERENCES 0
37
38 namespace blink {
39
40 class AudioContext;
41 class AudioNodeInput;
42 class AudioNodeOutput;
43 class AudioParam;
44 class ExceptionState;
45
46 // An AudioNode is the basic building block for handling audio within an AudioContext.
47 // It may be an audio source, an intermediate processing module, or an audio destination.
48 // Each AudioNode can have inputs and/or outputs. An AudioSourceNode has no inputs and a single output.
49 // An AudioDestinationNode has one input and no outputs and represents the final destination to the audio hardware.
50 // Most processing nodes such as filters will have one input and one output, although multiple inputs and outputs are possible.
51
52 // AudioNode has its own ref-counting mechanism that use RefTypes so we cannot use RefCountedGarbageCollected.
53 class AudioNode : public NoBaseWillBeGarbageCollectedFinalized<AudioNode>, public EventTargetWithInlineData {
54     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AudioNode);
55 public:
56     enum { ProcessingSizeInFrames = 128 };
57
58     AudioNode(AudioContext*, float sampleRate);
59     virtual ~AudioNode();
60     // dispose() is called just before the destructor. This must be called in
61     // the main thread, and while the graph lock is held.
62     virtual void dispose();
63     static unsigned instanceCount() { return s_instanceCount; }
64
65     AudioContext* context() { return m_context.get(); }
66     const AudioContext* context() const { return m_context.get(); }
67
68     enum NodeType {
69         NodeTypeUnknown,
70         NodeTypeDestination,
71         NodeTypeOscillator,
72         NodeTypeAudioBufferSource,
73         NodeTypeMediaElementAudioSource,
74         NodeTypeMediaStreamAudioDestination,
75         NodeTypeMediaStreamAudioSource,
76         NodeTypeJavaScript,
77         NodeTypeBiquadFilter,
78         NodeTypePanner,
79         NodeTypeConvolver,
80         NodeTypeDelay,
81         NodeTypeGain,
82         NodeTypeChannelSplitter,
83         NodeTypeChannelMerger,
84         NodeTypeAnalyser,
85         NodeTypeDynamicsCompressor,
86         NodeTypeWaveShaper,
87         NodeTypeEnd
88     };
89
90     enum ChannelCountMode {
91         Max,
92         ClampedMax,
93         Explicit
94     };
95
96     NodeType nodeType() const { return m_nodeType; }
97     String nodeTypeName() const;
98     void setNodeType(NodeType);
99
100 #if !ENABLE(OILPAN)
101     // Can be called from main thread or context's audio thread.
102     void ref();
103     void deref();
104 #endif
105
106     // This object has been connected to another object. This might have
107     // existing connections from others.
108     // This function must be called after acquiring a connection reference.
109     void makeConnection();
110     // This object will be disconnected from another object. This might have
111     // remaining connections from others.
112     // This function must be called before releasing a connection reference.
113     void breakConnection();
114
115     // Can be called from main thread or context's audio thread.  It must be called while the context's graph lock is held.
116 #if !ENABLE(OILPAN)
117     void finishDeref();
118 #endif
119     void breakConnectionWithLock();
120
121     // The AudioNodeInput(s) (if any) will already have their input data available when process() is called.
122     // Subclasses will take this input data and put the results in the AudioBus(s) of its AudioNodeOutput(s) (if any).
123     // Called from context's audio thread.
124     virtual void process(size_t framesToProcess) = 0;
125
126     // No significant resources should be allocated until initialize() is called.
127     // Processing may not occur until a node is initialized.
128     virtual void initialize();
129     virtual void uninitialize();
130
131     bool isInitialized() const { return m_isInitialized; }
132
133     unsigned numberOfInputs() const { return m_inputs.size(); }
134     unsigned numberOfOutputs() const { return m_outputs.size(); }
135
136     AudioNodeInput* input(unsigned);
137     AudioNodeOutput* output(unsigned);
138
139     // Called from main thread by corresponding JavaScript methods.
140     virtual void connect(AudioNode*, unsigned outputIndex, unsigned inputIndex, ExceptionState&);
141     void connect(AudioParam*, unsigned outputIndex, ExceptionState&);
142     virtual void disconnect(unsigned outputIndex, ExceptionState&);
143
144     virtual float sampleRate() const { return m_sampleRate; }
145
146     // processIfNecessary() is called by our output(s) when the rendering graph needs this AudioNode to process.
147     // This method ensures that the AudioNode will only process once per rendering time quantum even if it's called repeatedly.
148     // This handles the case of "fanout" where an output is connected to multiple AudioNode inputs.
149     // Called from context's audio thread.
150     void processIfNecessary(size_t framesToProcess);
151
152     // Called when a new connection has been made to one of our inputs or the connection number of channels has changed.
153     // This potentially gives us enough information to perform a lazy initialization or, if necessary, a re-initialization.
154     // Called from main thread.
155     virtual void checkNumberOfChannelsForInput(AudioNodeInput*);
156
157 #if DEBUG_AUDIONODE_REFERENCES
158     static void printNodeCounts();
159 #endif
160
161     bool isDisposeCalled() const { return m_isDisposeCalled; }
162
163     // tailTime() is the length of time (not counting latency time) where non-zero output may occur after continuous silent input.
164     virtual double tailTime() const = 0;
165     // latencyTime() is the length of time it takes for non-zero output to appear after non-zero input is provided. This only applies to
166     // processing delay which is an artifact of the processing algorithm chosen and is *not* part of the intrinsic desired effect. For
167     // example, a "delay" effect is expected to delay the signal, and thus would not be considered latency.
168     virtual double latencyTime() const = 0;
169
170     // propagatesSilence() should return true if the node will generate silent output when given silent input. By default, AudioNode
171     // will take tailTime() and latencyTime() into account when determining whether the node will propagate silence.
172     virtual bool propagatesSilence() const;
173     bool inputsAreSilent();
174     void silenceOutputs();
175     void unsilenceOutputs();
176
177     void enableOutputsIfNecessary();
178     void disableOutputsIfNecessary();
179
180     unsigned long channelCount();
181     virtual void setChannelCount(unsigned long, ExceptionState&);
182
183     String channelCountMode();
184     void setChannelCountMode(const String&, ExceptionState&);
185
186     String channelInterpretation();
187     void setChannelInterpretation(const String&, ExceptionState&);
188
189     ChannelCountMode internalChannelCountMode() const { return m_channelCountMode; }
190     AudioBus::ChannelInterpretation internalChannelInterpretation() const { return m_channelInterpretation; }
191
192     // EventTarget
193     virtual const AtomicString& interfaceName() const OVERRIDE FINAL;
194     virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
195
196     virtual void trace(Visitor*) OVERRIDE;
197
198 protected:
199     // Inputs and outputs must be created before the AudioNode is initialized.
200     void addInput();
201     void addOutput(PassOwnPtrWillBeRawPtr<AudioNodeOutput>);
202
203     // Called by processIfNecessary() to cause all parts of the rendering graph connected to us to process.
204     // Each rendering quantum, the audio data for each of the AudioNode's inputs will be available after this method is called.
205     // Called from context's audio thread.
206     virtual void pullInputs(size_t framesToProcess);
207
208     // Force all inputs to take any channel interpretation changes into account.
209     void updateChannelsForInputs();
210
211 private:
212     volatile bool m_isInitialized;
213     NodeType m_nodeType;
214     RefPtrWillBeMember<AudioContext> m_context;
215     float m_sampleRate;
216     WillBeHeapVector<OwnPtrWillBeMember<AudioNodeInput> > m_inputs;
217     WillBeHeapVector<OwnPtrWillBeMember<AudioNodeOutput> > m_outputs;
218
219     double m_lastProcessingTime;
220     double m_lastNonSilentTime;
221
222 #if !ENABLE(OILPAN)
223     // Ref-counting
224     volatile int m_normalRefCount;
225 #endif
226     volatile int m_connectionRefCount;
227
228     bool m_isDisabled;
229     bool m_isDisposeCalled;
230
231 #if DEBUG_AUDIONODE_REFERENCES
232     static bool s_isNodeCountInitialized;
233     static int s_nodeCount[NodeTypeEnd];
234 #endif
235     static unsigned s_instanceCount;
236
237 #if !ENABLE(OILPAN)
238     virtual void refEventTarget() OVERRIDE FINAL { ref(); }
239     virtual void derefEventTarget() OVERRIDE FINAL { deref(); }
240 #endif
241
242 protected:
243     unsigned m_channelCount;
244     ChannelCountMode m_channelCountMode;
245     AudioBus::ChannelInterpretation m_channelInterpretation;
246 };
247
248 } // namespace blink
249
250 #endif // AudioNode_h