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 "AudioNodeOutput.h"
32 #include "AudioContext.h"
33 #include "AudioNodeInput.h"
34 #include "AudioParam.h"
35 #include <wtf/Threading.h>
39 AudioNodeOutput::AudioNodeOutput(AudioNode* node, unsigned numberOfChannels)
41 , m_numberOfChannels(numberOfChannels)
42 , m_desiredNumberOfChannels(numberOfChannels)
43 , m_actualDestinationBus(0)
45 , m_renderingFanOutCount(0)
46 , m_renderingParamFanOutCount(0)
48 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels());
50 m_internalBus = adoptPtr(new AudioBus(numberOfChannels, AudioNode::ProcessingSizeInFrames));
51 m_actualDestinationBus = m_internalBus.get();
54 void AudioNodeOutput::setNumberOfChannels(unsigned numberOfChannels)
56 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels());
57 ASSERT(context()->isGraphOwner());
59 m_desiredNumberOfChannels = numberOfChannels;
61 if (context()->isAudioThread()) {
62 // 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).
63 updateNumberOfChannels();
65 // Let the context take care of it in the audio thread in the pre and post render tasks.
66 context()->markAudioNodeOutputDirty(this);
70 void AudioNodeOutput::updateInternalBus()
72 if (numberOfChannels() == m_internalBus->numberOfChannels())
75 m_internalBus = adoptPtr(new AudioBus(numberOfChannels(), AudioNode::ProcessingSizeInFrames));
77 // This may later be changed in pull() to point to an in-place bus with the same number of channels.
78 m_actualDestinationBus = m_internalBus.get();
81 void AudioNodeOutput::updateRenderingState()
83 updateNumberOfChannels();
84 m_renderingFanOutCount = fanOutCount();
85 m_renderingParamFanOutCount = paramFanOutCount();
88 void AudioNodeOutput::updateNumberOfChannels()
90 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
92 if (m_numberOfChannels != m_desiredNumberOfChannels) {
93 m_numberOfChannels = m_desiredNumberOfChannels;
95 propagateChannelCount();
99 void AudioNodeOutput::propagateChannelCount()
101 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
103 if (isChannelCountKnown()) {
104 // Announce to any nodes we're connected to that we changed our channel count for its input.
105 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
106 AudioNodeInput* input = *i;
107 AudioNode* connectionNode = input->node();
108 connectionNode->checkNumberOfChannelsForInput(input);
113 AudioBus* AudioNodeOutput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
115 ASSERT(context()->isAudioThread());
116 ASSERT(m_renderingFanOutCount > 0 || m_renderingParamFanOutCount > 0);
118 // Causes our AudioNode to process if it hasn't already for this render quantum.
119 // We try to do in-place processing (using inPlaceBus) if at all possible,
120 // but we can't process in-place if we're connected to more than one input (fan-out > 1).
121 // In this case pull() is called multiple times per rendering quantum, and the processIfNecessary() call below will
122 // cause our node to process() only the first time, caching the output in m_internalOutputBus for subsequent calls.
124 bool isInPlace = inPlaceBus && inPlaceBus->numberOfChannels() == numberOfChannels() && (m_renderingFanOutCount + m_renderingParamFanOutCount) == 1;
126 // Setup the actual destination bus for processing when our node's process() method gets called in processIfNecessary() below.
127 m_actualDestinationBus = isInPlace ? inPlaceBus : m_internalBus.get();
129 node()->processIfNecessary(framesToProcess);
130 return m_actualDestinationBus;
133 AudioBus* AudioNodeOutput::bus() const
135 ASSERT(const_cast<AudioNodeOutput*>(this)->context()->isAudioThread());
136 ASSERT(m_actualDestinationBus);
137 return m_actualDestinationBus;
140 unsigned AudioNodeOutput::fanOutCount()
142 ASSERT(context()->isGraphOwner());
143 return m_inputs.size();
146 unsigned AudioNodeOutput::paramFanOutCount()
148 ASSERT(context()->isGraphOwner());
149 return m_params.size();
152 unsigned AudioNodeOutput::renderingFanOutCount() const
154 return m_renderingFanOutCount;
157 unsigned AudioNodeOutput::renderingParamFanOutCount() const
159 return m_renderingParamFanOutCount;
162 void AudioNodeOutput::addInput(AudioNodeInput* input)
164 ASSERT(context()->isGraphOwner());
173 void AudioNodeOutput::removeInput(AudioNodeInput* input)
175 ASSERT(context()->isGraphOwner());
181 m_inputs.remove(input);
184 void AudioNodeOutput::disconnectAllInputs()
186 ASSERT(context()->isGraphOwner());
188 // AudioNodeInput::disconnect() changes m_inputs by calling removeInput().
189 while (!m_inputs.isEmpty()) {
190 AudioNodeInput* input = *m_inputs.begin();
191 input->disconnect(this);
195 void AudioNodeOutput::addParam(AudioParam* param)
197 ASSERT(context()->isGraphOwner());
206 void AudioNodeOutput::removeParam(AudioParam* param)
208 ASSERT(context()->isGraphOwner());
214 m_params.remove(param);
217 void AudioNodeOutput::disconnectAllParams()
219 ASSERT(context()->isGraphOwner());
221 for (ParamsIterator it = m_params.begin(); it != m_params.end(); ++it)
222 (*it)->disconnect(this);
227 void AudioNodeOutput::disconnectAll()
229 disconnectAllInputs();
230 disconnectAllParams();
233 void AudioNodeOutput::disable()
235 ASSERT(context()->isGraphOwner());
238 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
239 AudioNodeInput* input = *i;
240 input->disable(this);
246 void AudioNodeOutput::enable()
248 ASSERT(context()->isGraphOwner());
251 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
252 AudioNodeInput* input = *i;
259 } // namespace WebCore
261 #endif // ENABLE(WEB_AUDIO)