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 "AudioNodeInput.h" |
30 | |
31 | #include "AudioContext.h" |
32 | #include "AudioNode.h" |
33 | #include "AudioNodeOutput.h" |
34 | #include <algorithm> |
35 | |
36 | namespace WebCore { |
37 | |
38 | AudioNodeInput::AudioNodeInput(AudioNode* node) |
39 | : AudioSummingJunction(node->context()) |
40 | , m_node(node) |
41 | { |
42 | // Set to mono by default. |
43 | m_internalSummingBus = AudioBus::create(1, AudioNode::ProcessingSizeInFrames); |
44 | } |
45 | |
46 | void AudioNodeInput::connect(AudioNodeOutput* output) |
47 | { |
48 | ASSERT(context().isGraphOwner()); |
49 | |
50 | ASSERT(output && node()); |
51 | if (!output || !node()) |
52 | return; |
53 | |
54 | // Check if we're already connected to this output. |
55 | if (!m_outputs.add(output).isNewEntry) |
56 | return; |
57 | |
58 | output->addInput(this); |
59 | changedOutputs(); |
60 | |
61 | // Sombody has just connected to us, so count it as a reference. |
62 | node()->ref(AudioNode::RefTypeConnection); |
63 | } |
64 | |
65 | void AudioNodeInput::disconnect(AudioNodeOutput* output) |
66 | { |
67 | ASSERT(context().isGraphOwner()); |
68 | |
69 | ASSERT(output && node()); |
70 | if (!output || !node()) |
71 | return; |
72 | |
73 | // First try to disconnect from "active" connections. |
74 | if (m_outputs.remove(output)) { |
75 | changedOutputs(); |
76 | output->removeInput(this); |
77 | node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted. |
78 | return; |
79 | } |
80 | |
81 | // Otherwise, try to disconnect from disabled connections. |
82 | if (m_disabledOutputs.remove(output)) { |
83 | output->removeInput(this); |
84 | node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted. |
85 | return; |
86 | } |
87 | |
88 | ASSERT_NOT_REACHED(); |
89 | } |
90 | |
91 | void AudioNodeInput::disable(AudioNodeOutput* output) |
92 | { |
93 | ASSERT(context().isGraphOwner()); |
94 | |
95 | ASSERT(output && node()); |
96 | if (!output || !node()) |
97 | return; |
98 | |
99 | ASSERT(m_outputs.contains(output)); |
100 | |
101 | m_disabledOutputs.add(output); |
102 | m_outputs.remove(output); |
103 | changedOutputs(); |
104 | |
105 | // Propagate disabled state to outputs. |
106 | node()->disableOutputsIfNecessary(); |
107 | } |
108 | |
109 | void AudioNodeInput::enable(AudioNodeOutput* output) |
110 | { |
111 | ASSERT(context().isGraphOwner()); |
112 | |
113 | ASSERT(output && node()); |
114 | if (!output || !node()) |
115 | return; |
116 | |
117 | ASSERT(m_disabledOutputs.contains(output)); |
118 | |
119 | // Move output from disabled list to active list. |
120 | m_outputs.add(output); |
121 | m_disabledOutputs.remove(output); |
122 | changedOutputs(); |
123 | |
124 | // Propagate enabled state to outputs. |
125 | node()->enableOutputsIfNecessary(); |
126 | } |
127 | |
128 | void AudioNodeInput::didUpdate() |
129 | { |
130 | node()->checkNumberOfChannelsForInput(this); |
131 | } |
132 | |
133 | void AudioNodeInput::updateInternalBus() |
134 | { |
135 | ASSERT(context().isAudioThread() && context().isGraphOwner()); |
136 | |
137 | unsigned numberOfInputChannels = numberOfChannels(); |
138 | |
139 | if (numberOfInputChannels == m_internalSummingBus->numberOfChannels()) |
140 | return; |
141 | |
142 | m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioNode::ProcessingSizeInFrames); |
143 | } |
144 | |
145 | unsigned AudioNodeInput::numberOfChannels() const |
146 | { |
147 | AudioNode::ChannelCountMode mode = node()->internalChannelCountMode(); |
148 | if (mode == AudioNode::Explicit) |
149 | return node()->channelCount(); |
150 | |
151 | // Find the number of channels of the connection with the largest number of channels. |
152 | unsigned maxChannels = 1; // one channel is the minimum allowed |
153 | |
154 | for (auto& output : m_outputs) { |
155 | // Use output()->numberOfChannels() instead of output->bus()->numberOfChannels(), |
156 | // because the calling of AudioNodeOutput::bus() is not safe here. |
157 | maxChannels = std::max(maxChannels, output->numberOfChannels()); |
158 | } |
159 | |
160 | if (mode == AudioNode::ClampedMax) |
161 | maxChannels = std::min(maxChannels, static_cast<unsigned>(node()->channelCount())); |
162 | |
163 | return maxChannels; |
164 | } |
165 | |
166 | AudioBus* AudioNodeInput::bus() |
167 | { |
168 | ASSERT(context().isAudioThread()); |
169 | |
170 | // Handle single connection specially to allow for in-place processing. |
171 | if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode() == AudioNode::Max) |
172 | return renderingOutput(0)->bus(); |
173 | |
174 | // Multiple connections case or complex ChannelCountMode (or no connections). |
175 | return internalSummingBus(); |
176 | } |
177 | |
178 | AudioBus* AudioNodeInput::internalSummingBus() |
179 | { |
180 | ASSERT(context().isAudioThread()); |
181 | |
182 | return m_internalSummingBus.get(); |
183 | } |
184 | |
185 | void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProcess) |
186 | { |
187 | ASSERT(context().isAudioThread()); |
188 | |
189 | // We shouldn't be calling this method if there's only one connection, since it's less efficient. |
190 | ASSERT(numberOfRenderingConnections() > 1 || node()->internalChannelCountMode() != AudioNode::Max); |
191 | |
192 | ASSERT(summingBus); |
193 | if (!summingBus) |
194 | return; |
195 | |
196 | summingBus->zero(); |
197 | |
198 | AudioBus::ChannelInterpretation interpretation = node()->internalChannelInterpretation(); |
199 | |
200 | for (auto& output : m_renderingOutputs) { |
201 | ASSERT(output); |
202 | |
203 | // Render audio from this output. |
204 | AudioBus* connectionBus = output->pull(0, framesToProcess); |
205 | |
206 | // Sum, with unity-gain. |
207 | summingBus->sumFrom(*connectionBus, interpretation); |
208 | } |
209 | } |
210 | |
211 | AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess) |
212 | { |
213 | ASSERT(context().isAudioThread()); |
214 | |
215 | // Handle single connection case. |
216 | if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode() == AudioNode::Max) { |
217 | // The output will optimize processing using inPlaceBus if it's able. |
218 | AudioNodeOutput* output = this->renderingOutput(0); |
219 | return output->pull(inPlaceBus, framesToProcess); |
220 | } |
221 | |
222 | AudioBus* internalSummingBus = this->internalSummingBus(); |
223 | |
224 | if (!numberOfRenderingConnections()) { |
225 | // At least, generate silence if we're not connected to anything. |
226 | // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing. |
227 | internalSummingBus->zero(); |
228 | return internalSummingBus; |
229 | } |
230 | |
231 | // Handle multiple connections case. |
232 | sumAllConnections(internalSummingBus, framesToProcess); |
233 | |
234 | return internalSummingBus; |
235 | } |
236 | |
237 | } // namespace WebCore |
238 | |
239 | #endif // ENABLE(WEB_AUDIO) |
240 | |