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 "AudioBasicProcessorNode.h"
30
31#include "AudioBus.h"
32#include "AudioContext.h"
33#include "AudioNodeInput.h"
34#include "AudioNodeOutput.h"
35#include "AudioProcessor.h"
36#include <wtf/IsoMallocInlines.h>
37
38namespace WebCore {
39
40WTF_MAKE_ISO_ALLOCATED_IMPL(AudioBasicProcessorNode);
41
42AudioBasicProcessorNode::AudioBasicProcessorNode(AudioContext& context, float sampleRate)
43 : AudioNode(context, sampleRate)
44{
45 addInput(std::make_unique<AudioNodeInput>(this));
46 addOutput(std::make_unique<AudioNodeOutput>(this, 1));
47
48 // The subclass must create m_processor.
49}
50
51void AudioBasicProcessorNode::initialize()
52{
53 if (isInitialized())
54 return;
55
56 ASSERT(processor());
57 processor()->initialize();
58
59 AudioNode::initialize();
60}
61
62void AudioBasicProcessorNode::uninitialize()
63{
64 if (!isInitialized())
65 return;
66
67 ASSERT(processor());
68 processor()->uninitialize();
69
70 AudioNode::uninitialize();
71}
72
73void AudioBasicProcessorNode::process(size_t framesToProcess)
74{
75 AudioBus* destinationBus = output(0)->bus();
76
77 if (!isInitialized() || !processor() || processor()->numberOfChannels() != numberOfChannels())
78 destinationBus->zero();
79 else {
80 AudioBus* sourceBus = input(0)->bus();
81
82 // FIXME: if we take "tail time" into account, then we can avoid calling processor()->process() once the tail dies down.
83 if (!input(0)->isConnected())
84 sourceBus->zero();
85
86 processor()->process(sourceBus, destinationBus, framesToProcess);
87 }
88}
89
90// Nice optimization in the very common case allowing for "in-place" processing
91void AudioBasicProcessorNode::pullInputs(size_t framesToProcess)
92{
93 // Render input stream - suggest to the input to render directly into output bus for in-place processing in process() if possible.
94 input(0)->pull(output(0)->bus(), framesToProcess);
95}
96
97void AudioBasicProcessorNode::reset()
98{
99 if (processor())
100 processor()->reset();
101}
102
103// As soon as we know the channel count of our input, we can lazily initialize.
104// Sometimes this may be called more than once with different channel counts, in which case we must safely
105// uninitialize and then re-initialize with the new channel count.
106void AudioBasicProcessorNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
107{
108 ASSERT(context().isAudioThread() && context().isGraphOwner());
109
110 ASSERT(input == this->input(0));
111 if (input != this->input(0))
112 return;
113
114 ASSERT(processor());
115 if (!processor())
116 return;
117
118 unsigned numberOfChannels = input->numberOfChannels();
119
120 if (isInitialized() && numberOfChannels != output(0)->numberOfChannels()) {
121 // We're already initialized but the channel count has changed.
122 uninitialize();
123 }
124
125 if (!isInitialized()) {
126 // This will propagate the channel count to any nodes connected further down the chain...
127 output(0)->setNumberOfChannels(numberOfChannels);
128
129 // Re-initialize the processor with the new channel count.
130 processor()->setNumberOfChannels(numberOfChannels);
131 initialize();
132 }
133
134 AudioNode::checkNumberOfChannelsForInput(input);
135}
136
137unsigned AudioBasicProcessorNode::numberOfChannels()
138{
139 return output(0)->numberOfChannels();
140}
141
142double AudioBasicProcessorNode::tailTime() const
143{
144 return m_processor->tailTime();
145}
146
147double AudioBasicProcessorNode::latencyTime() const
148{
149 return m_processor->latencyTime();
150}
151
152} // namespace WebCore
153
154#endif // ENABLE(WEB_AUDIO)
155