• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "config.h"
26 
27 #if ENABLE(WEB_AUDIO)
28 
29 #include "modules/webaudio/AudioBasicProcessorNode.h"
30 
31 #include "platform/audio/AudioBus.h"
32 #include "platform/audio/AudioProcessor.h"
33 #include "modules/webaudio/AudioContext.h"
34 #include "modules/webaudio/AudioNodeInput.h"
35 #include "modules/webaudio/AudioNodeOutput.h"
36 
37 namespace WebCore {
38 
AudioBasicProcessorNode(AudioContext * context,float sampleRate)39 AudioBasicProcessorNode::AudioBasicProcessorNode(AudioContext* context, float sampleRate)
40     : AudioNode(context, sampleRate)
41 {
42     addInput(adoptPtr(new AudioNodeInput(this)));
43     addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
44 
45     // The subclass must create m_processor.
46 }
47 
initialize()48 void AudioBasicProcessorNode::initialize()
49 {
50     if (isInitialized())
51         return;
52 
53     ASSERT(processor());
54     processor()->initialize();
55 
56     AudioNode::initialize();
57 }
58 
uninitialize()59 void AudioBasicProcessorNode::uninitialize()
60 {
61     if (!isInitialized())
62         return;
63 
64     ASSERT(processor());
65     processor()->uninitialize();
66 
67     AudioNode::uninitialize();
68 }
69 
process(size_t framesToProcess)70 void AudioBasicProcessorNode::process(size_t framesToProcess)
71 {
72     AudioBus* destinationBus = output(0)->bus();
73 
74     if (!isInitialized() || !processor() || processor()->numberOfChannels() != numberOfChannels())
75         destinationBus->zero();
76     else {
77         AudioBus* sourceBus = input(0)->bus();
78 
79         // FIXME: if we take "tail time" into account, then we can avoid calling processor()->process() once the tail dies down.
80         if (!input(0)->isConnected())
81             sourceBus->zero();
82 
83         processor()->process(sourceBus, destinationBus, framesToProcess);
84     }
85 }
86 
87 // Nice optimization in the very common case allowing for "in-place" processing
pullInputs(size_t framesToProcess)88 void AudioBasicProcessorNode::pullInputs(size_t framesToProcess)
89 {
90     // Render input stream - suggest to the input to render directly into output bus for in-place processing in process() if possible.
91     input(0)->pull(output(0)->bus(), framesToProcess);
92 }
93 
94 // As soon as we know the channel count of our input, we can lazily initialize.
95 // Sometimes this may be called more than once with different channel counts, in which case we must safely
96 // uninitialize and then re-initialize with the new channel count.
checkNumberOfChannelsForInput(AudioNodeInput * input)97 void AudioBasicProcessorNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
98 {
99     ASSERT(context()->isAudioThread() && context()->isGraphOwner());
100 
101     ASSERT(input == this->input(0));
102     if (input != this->input(0))
103         return;
104 
105     ASSERT(processor());
106     if (!processor())
107         return;
108 
109     unsigned numberOfChannels = input->numberOfChannels();
110 
111     if (isInitialized() && numberOfChannels != output(0)->numberOfChannels()) {
112         // We're already initialized but the channel count has changed.
113         uninitialize();
114     }
115 
116     if (!isInitialized()) {
117         // This will propagate the channel count to any nodes connected further down the chain...
118         output(0)->setNumberOfChannels(numberOfChannels);
119 
120         // Re-initialize the processor with the new channel count.
121         processor()->setNumberOfChannels(numberOfChannels);
122         initialize();
123     }
124 
125     AudioNode::checkNumberOfChannelsForInput(input);
126 }
127 
numberOfChannels()128 unsigned AudioBasicProcessorNode::numberOfChannels()
129 {
130     return output(0)->numberOfChannels();
131 }
132 
tailTime() const133 double AudioBasicProcessorNode::tailTime() const
134 {
135     return m_processor->tailTime();
136 }
137 
latencyTime() const138 double AudioBasicProcessorNode::latencyTime() const
139 {
140     return m_processor->latencyTime();
141 }
142 
143 } // namespace WebCore
144 
145 #endif // ENABLE(WEB_AUDIO)
146