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/AudioNodeOutput.h"
31 #include "modules/webaudio/AudioContext.h"
32 #include "modules/webaudio/AudioNodeInput.h"
33 #include "wtf/Threading.h"
37 AudioNodeOutput::AudioNodeOutput(AudioNode* node, unsigned numberOfChannels)
39 , m_numberOfChannels(numberOfChannels)
40 , m_desiredNumberOfChannels(numberOfChannels)
43 , m_renderingFanOutCount(0)
44 , m_renderingParamFanOutCount(0)
46 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels());
48 m_internalBus = AudioBus::create(numberOfChannels, AudioNode::ProcessingSizeInFrames);
51 void AudioNodeOutput::setNumberOfChannels(unsigned numberOfChannels)
53 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels());
54 ASSERT(context()->isGraphOwner());
56 m_desiredNumberOfChannels = numberOfChannels;
58 if (context()->isAudioThread()) {
59 // If we're in the audio thread then we can take care of it right away (we should be at the very start or end of a rendering quantum).
60 updateNumberOfChannels();
62 // Let the context take care of it in the audio thread in the pre and post render tasks.
63 context()->markAudioNodeOutputDirty(this);
67 void AudioNodeOutput::updateInternalBus()
69 if (numberOfChannels() == m_internalBus->numberOfChannels())
72 m_internalBus = AudioBus::create(numberOfChannels(), AudioNode::ProcessingSizeInFrames);
75 void AudioNodeOutput::updateRenderingState()
77 updateNumberOfChannels();
78 m_renderingFanOutCount = fanOutCount();
79 m_renderingParamFanOutCount = paramFanOutCount();
82 void AudioNodeOutput::updateNumberOfChannels()
84 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
86 if (m_numberOfChannels != m_desiredNumberOfChannels) {
87 m_numberOfChannels = m_desiredNumberOfChannels;
89 propagateChannelCount();
93 void AudioNodeOutput::propagateChannelCount()
95 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
97 if (isChannelCountKnown()) {
98 // Announce to any nodes we're connected to that we changed our channel count for its input.
99 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
100 AudioNodeInput* input = *i;
101 AudioNode* connectionNode = input->node();
102 connectionNode->checkNumberOfChannelsForInput(input);
107 AudioBus* AudioNodeOutput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
109 ASSERT(context()->isAudioThread());
110 ASSERT(m_renderingFanOutCount > 0 || m_renderingParamFanOutCount > 0);
112 // Causes our AudioNode to process if it hasn't already for this render quantum.
113 // We try to do in-place processing (using inPlaceBus) if at all possible,
114 // but we can't process in-place if we're connected to more than one input (fan-out > 1).
115 // In this case pull() is called multiple times per rendering quantum, and the processIfNecessary() call below will
116 // cause our node to process() only the first time, caching the output in m_internalOutputBus for subsequent calls.
118 m_isInPlace = inPlaceBus && inPlaceBus->numberOfChannels() == numberOfChannels() && (m_renderingFanOutCount + m_renderingParamFanOutCount) == 1;
120 m_inPlaceBus = m_isInPlace ? inPlaceBus : 0;
122 node()->processIfNecessary(framesToProcess);
126 AudioBus* AudioNodeOutput::bus() const
128 ASSERT(const_cast<AudioNodeOutput*>(this)->context()->isAudioThread());
129 return m_isInPlace ? m_inPlaceBus.get() : m_internalBus.get();
132 unsigned AudioNodeOutput::fanOutCount()
134 ASSERT(context()->isGraphOwner());
135 return m_inputs.size();
138 unsigned AudioNodeOutput::paramFanOutCount()
140 ASSERT(context()->isGraphOwner());
141 return m_params.size();
144 unsigned AudioNodeOutput::renderingFanOutCount() const
146 return m_renderingFanOutCount;
149 void AudioNodeOutput::addInput(AudioNodeInput* input)
151 ASSERT(context()->isGraphOwner());
160 void AudioNodeOutput::removeInput(AudioNodeInput* input)
162 ASSERT(context()->isGraphOwner());
168 m_inputs.remove(input);
171 void AudioNodeOutput::disconnectAllInputs()
173 ASSERT(context()->isGraphOwner());
175 // AudioNodeInput::disconnect() changes m_inputs by calling removeInput().
176 while (!m_inputs.isEmpty()) {
177 AudioNodeInput* input = *m_inputs.begin();
178 input->disconnect(this);
182 void AudioNodeOutput::addParam(AudioParam* param)
184 ASSERT(context()->isGraphOwner());
193 void AudioNodeOutput::removeParam(AudioParam* param)
195 ASSERT(context()->isGraphOwner());
201 m_params.remove(param);
204 void AudioNodeOutput::disconnectAllParams()
206 ASSERT(context()->isGraphOwner());
208 // AudioParam::disconnect() changes m_params by calling removeParam().
209 while (!m_params.isEmpty()) {
210 AudioParam* param = m_params.begin()->get();
211 param->disconnect(this);
215 void AudioNodeOutput::disconnectAll()
217 disconnectAllInputs();
218 disconnectAllParams();
221 void AudioNodeOutput::disable()
223 ASSERT(context()->isGraphOwner());
226 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
227 AudioNodeInput* input = *i;
228 input->disable(this);
234 void AudioNodeOutput::enable()
236 ASSERT(context()->isGraphOwner());
239 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
240 AudioNodeInput* input = *i;
247 } // namespace WebCore
249 #endif // ENABLE(WEB_AUDIO)