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.
29 #include "modules/webaudio/AudioNodeInput.h"
31 #include "modules/webaudio/AudioContext.h"
32 #include "modules/webaudio/AudioNodeOutput.h"
37 inline AudioNodeInput::AudioNodeInput(AudioNode& node)
38 : AudioSummingJunction(node.context())
41 // Set to mono by default.
42 m_internalSummingBus = AudioBus::create(1, AudioNode::ProcessingSizeInFrames);
45 AudioNodeInput* AudioNodeInput::create(AudioNode& node)
47 return new AudioNodeInput(node);
50 void AudioNodeInput::trace(Visitor* visitor)
52 visitor->trace(m_node);
53 AudioSummingJunction::trace(visitor);
56 void AudioNodeInput::connect(AudioNodeOutput& output)
58 ASSERT(context()->isGraphOwner());
60 // Check if we're already connected to this output.
61 if (m_outputs.contains(&output))
64 output.addInput(*this);
65 m_outputs.add(&output);
69 void AudioNodeInput::disconnect(AudioNodeOutput& output)
71 ASSERT(context()->isGraphOwner());
73 // First try to disconnect from "active" connections.
74 if (m_outputs.contains(&output)) {
75 m_outputs.remove(&output);
77 output.removeInput(*this);
78 // Note: it's important to return immediately after removeInput() calls
79 // since the node may be deleted.
83 // Otherwise, try to disconnect from disabled connections.
84 if (m_disabledOutputs.contains(&output)) {
85 m_disabledOutputs.remove(&output);
86 output.removeInput(*this);
87 // Note: it's important to return immediately after all removeInput() calls
88 // since the node may be deleted.
95 void AudioNodeInput::disable(AudioNodeOutput& output)
97 ASSERT(context()->isGraphOwner());
98 ASSERT(m_outputs.contains(&output));
100 m_disabledOutputs.add(&output);
101 m_outputs.remove(&output);
104 // Propagate disabled state to outputs.
105 node().disableOutputsIfNecessary();
108 void AudioNodeInput::enable(AudioNodeOutput& output)
110 ASSERT(context()->isGraphOwner());
111 ASSERT(m_disabledOutputs.contains(&output));
113 // Move output from disabled list to active list.
114 m_outputs.add(&output);
115 m_disabledOutputs.remove(&output);
118 // Propagate enabled state to outputs.
119 node().enableOutputsIfNecessary();
122 void AudioNodeInput::didUpdate()
124 node().checkNumberOfChannelsForInput(this);
127 void AudioNodeInput::updateInternalBus()
129 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
131 unsigned numberOfInputChannels = numberOfChannels();
133 if (numberOfInputChannels == m_internalSummingBus->numberOfChannels())
136 m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioNode::ProcessingSizeInFrames);
139 unsigned AudioNodeInput::numberOfChannels() const
141 AudioNode::ChannelCountMode mode = node().internalChannelCountMode();
142 if (mode == AudioNode::Explicit)
143 return node().channelCount();
145 // Find the number of channels of the connection with the largest number of channels.
146 unsigned maxChannels = 1; // one channel is the minimum allowed
148 for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outputs.end(); ++i) {
149 AudioNodeOutput* output = *i;
150 // Use output()->numberOfChannels() instead of output->bus()->numberOfChannels(),
151 // because the calling of AudioNodeOutput::bus() is not safe here.
152 maxChannels = std::max(maxChannels, output->numberOfChannels());
155 if (mode == AudioNode::ClampedMax)
156 maxChannels = std::min(maxChannels, static_cast<unsigned>(node().channelCount()));
161 AudioBus* AudioNodeInput::bus()
163 ASSERT(context()->isAudioThread());
165 // Handle single connection specially to allow for in-place processing.
166 if (numberOfRenderingConnections() == 1 && node().internalChannelCountMode() == AudioNode::Max)
167 return renderingOutput(0)->bus();
169 // Multiple connections case or complex ChannelCountMode (or no connections).
170 return internalSummingBus();
173 AudioBus* AudioNodeInput::internalSummingBus()
175 ASSERT(context()->isAudioThread());
177 return m_internalSummingBus.get();
180 void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProcess)
182 ASSERT(context()->isAudioThread());
184 // We shouldn't be calling this method if there's only one connection, since it's less efficient.
185 ASSERT(numberOfRenderingConnections() > 1 || node().internalChannelCountMode() != AudioNode::Max);
193 AudioBus::ChannelInterpretation interpretation = node().internalChannelInterpretation();
195 for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
196 AudioNodeOutput* output = renderingOutput(i);
199 // Render audio from this output.
200 AudioBus* connectionBus = output->pull(0, framesToProcess);
202 // Sum, with unity-gain.
203 summingBus->sumFrom(*connectionBus, interpretation);
207 AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
209 ASSERT(context()->isAudioThread());
211 // Handle single connection case.
212 if (numberOfRenderingConnections() == 1 && node().internalChannelCountMode() == AudioNode::Max) {
213 // The output will optimize processing using inPlaceBus if it's able.
214 AudioNodeOutput* output = this->renderingOutput(0);
215 return output->pull(inPlaceBus, framesToProcess);
218 AudioBus* internalSummingBus = this->internalSummingBus();
220 if (!numberOfRenderingConnections()) {
221 // At least, generate silence if we're not connected to anything.
222 // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing.
223 internalSummingBus->zero();
224 return internalSummingBus;
227 // Handle multiple connections case.
228 sumAllConnections(internalSummingBus, framesToProcess);
230 return internalSummingBus;
235 #endif // ENABLE(WEB_AUDIO)