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 "AudioNodeInput.h"
31 #include "AudioContext.h"
32 #include "AudioNode.h"
33 #include "AudioNodeOutput.h"
34 #include "ChannelCountMode.h"
39 AudioNodeInput::AudioNodeInput(AudioNode* node)
40 : AudioSummingJunction(node->context())
43 // Set to mono by default.
44 m_internalSummingBus = AudioBus::create(1, AudioUtilities::renderQuantumSize);
47 void AudioNodeInput::connect(AudioNodeOutput* output)
49 ASSERT(context().isGraphOwner());
51 ASSERT(output && node());
52 if (!output || !node())
55 auto& outputsMap = output->isEnabled() ? m_outputs : m_disabledOutputs;
56 // Check if we're already connected to this output.
57 if (!outputsMap.add(output).isNewEntry)
60 output->addInput(this);
63 // Sombody has just connected to us, so count it as a reference.
64 node()->ref(AudioNode::RefTypeConnection);
67 void AudioNodeInput::disconnect(AudioNodeOutput* output)
69 ASSERT(context().isGraphOwner());
71 ASSERT(output && node());
72 if (!output || !node())
75 // First try to disconnect from "active" connections.
76 if (m_outputs.remove(output)) {
78 output->removeInput(this);
79 node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
83 // Otherwise, try to disconnect from disabled connections.
84 if (m_disabledOutputs.remove(output)) {
85 output->removeInput(this);
86 node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
93 void AudioNodeInput::disable(AudioNodeOutput* output)
95 ASSERT(context().isGraphOwner());
97 ASSERT(output && node());
98 if (!output || !node())
101 ASSERT(m_outputs.contains(output));
103 m_disabledOutputs.add(output);
104 m_outputs.remove(output);
107 // Propagate disabled state to outputs.
108 node()->disableOutputsIfNecessary();
111 void AudioNodeInput::enable(AudioNodeOutput* output)
113 ASSERT(context().isGraphOwner());
115 ASSERT(output && node());
116 if (!output || !node())
119 ASSERT(m_disabledOutputs.contains(output));
121 // Move output from disabled list to active list.
122 m_outputs.add(output);
123 m_disabledOutputs.remove(output);
126 // Propagate enabled state to outputs.
127 node()->enableOutputsIfNecessary();
130 void AudioNodeInput::didUpdate()
132 node()->checkNumberOfChannelsForInput(this);
135 void AudioNodeInput::updateInternalBus()
137 ASSERT(context().isAudioThread() && context().isGraphOwner());
139 unsigned numberOfInputChannels = numberOfChannels();
141 if (numberOfInputChannels == m_internalSummingBus->numberOfChannels())
144 m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioUtilities::renderQuantumSize);
147 unsigned AudioNodeInput::numberOfChannels() const
149 auto mode = node()->channelCountMode();
150 if (mode == ChannelCountMode::Explicit)
151 return node()->channelCount();
153 // Find the number of channels of the connection with the largest number of channels.
154 unsigned maxChannels = 1; // one channel is the minimum allowed
156 for (auto& output : m_outputs) {
157 // Use output()->numberOfChannels() instead of output->bus()->numberOfChannels(),
158 // because the calling of AudioNodeOutput::bus() is not safe here.
159 maxChannels = std::max(maxChannels, output->numberOfChannels());
162 if (mode == ChannelCountMode::ClampedMax)
163 maxChannels = std::min(maxChannels, static_cast<unsigned>(node()->channelCount()));
168 AudioBus* AudioNodeInput::bus()
170 ASSERT(context().isAudioThread());
172 // Handle single connection specially to allow for in-place processing.
173 if (numberOfRenderingConnections() == 1 && node()->channelCountMode() == ChannelCountMode::Max)
174 return renderingOutput(0)->bus();
176 // Multiple connections case or complex ChannelCountMode (or no connections).
177 return internalSummingBus();
180 AudioBus* AudioNodeInput::internalSummingBus()
182 ASSERT(context().isAudioThread());
184 return m_internalSummingBus.get();
187 void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProcess)
189 ASSERT(context().isAudioThread());
191 // We shouldn't be calling this method if there's only one connection, since it's less efficient.
192 ASSERT(numberOfRenderingConnections() > 1 || node()->channelCountMode() != ChannelCountMode::Max);
200 auto interpretation = node()->channelInterpretation();
202 for (auto& output : m_renderingOutputs) {
205 // Render audio from this output.
206 AudioBus* connectionBus = output->pull(0, framesToProcess);
208 // Sum, with unity-gain.
209 summingBus->sumFrom(*connectionBus, interpretation);
213 AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
215 ASSERT(context().isAudioThread());
217 // Handle single connection case.
218 if (numberOfRenderingConnections() == 1 && node()->channelCountMode() == ChannelCountMode::Max) {
219 // The output will optimize processing using inPlaceBus if it's able.
220 AudioNodeOutput* output = this->renderingOutput(0);
221 return output->pull(inPlaceBus, framesToProcess);
224 AudioBus* internalSummingBus = this->internalSummingBus();
226 if (!numberOfRenderingConnections()) {
227 // At least, generate silence if we're not connected to anything.
228 // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing.
229 internalSummingBus->zero();
230 return internalSummingBus;
233 // Handle multiple connections case.
234 sumAllConnections(internalSummingBus, framesToProcess);
236 return internalSummingBus;
239 } // namespace WebCore
241 #endif // ENABLE(WEB_AUDIO)