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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | |
31 | #if ENABLE(WEB_AUDIO) |
32 | |
33 | #include "ChannelMergerNode.h" |
34 | |
35 | #include "AudioContext.h" |
36 | #include "AudioNodeInput.h" |
37 | #include "AudioNodeOutput.h" |
38 | #include <wtf/IsoMallocInlines.h> |
39 | |
40 | const unsigned DefaultNumberOfOutputChannels = 1; |
41 | |
42 | namespace WebCore { |
43 | |
44 | WTF_MAKE_ISO_ALLOCATED_IMPL(ChannelMergerNode); |
45 | |
46 | RefPtr<ChannelMergerNode> ChannelMergerNode::create(AudioContext& context, float sampleRate, unsigned numberOfInputs) |
47 | { |
48 | if (!numberOfInputs || numberOfInputs > AudioContext::maxNumberOfChannels()) |
49 | return nullptr; |
50 | |
51 | return adoptRef(*new ChannelMergerNode(context, sampleRate, numberOfInputs)); |
52 | } |
53 | |
54 | ChannelMergerNode::ChannelMergerNode(AudioContext& context, float sampleRate, unsigned numberOfInputs) |
55 | : AudioNode(context, sampleRate) |
56 | , m_desiredNumberOfOutputChannels(DefaultNumberOfOutputChannels) |
57 | { |
58 | setNodeType(NodeTypeChannelMerger); |
59 | |
60 | // Create the requested number of inputs. |
61 | for (unsigned i = 0; i < numberOfInputs; ++i) |
62 | addInput(std::make_unique<AudioNodeInput>(this)); |
63 | |
64 | addOutput(std::make_unique<AudioNodeOutput>(this, 1)); |
65 | |
66 | initialize(); |
67 | } |
68 | |
69 | void ChannelMergerNode::process(size_t framesToProcess) |
70 | { |
71 | AudioNodeOutput* output = this->output(0); |
72 | ASSERT(output); |
73 | ASSERT_UNUSED(framesToProcess, framesToProcess == output->bus()->length()); |
74 | |
75 | // Output bus not updated yet, so just output silence. |
76 | if (m_desiredNumberOfOutputChannels != output->numberOfChannels()) { |
77 | output->bus()->zero(); |
78 | return; |
79 | } |
80 | |
81 | // Merge all the channels from all the inputs into one output. |
82 | unsigned outputChannelIndex = 0; |
83 | for (unsigned i = 0; i < numberOfInputs(); ++i) { |
84 | AudioNodeInput* input = this->input(i); |
85 | if (input->isConnected()) { |
86 | unsigned numberOfInputChannels = input->bus()->numberOfChannels(); |
87 | |
88 | // Merge channels from this particular input. |
89 | for (unsigned j = 0; j < numberOfInputChannels; ++j) { |
90 | AudioChannel* inputChannel = input->bus()->channel(j); |
91 | AudioChannel* outputChannel = output->bus()->channel(outputChannelIndex); |
92 | outputChannel->copyFrom(inputChannel); |
93 | |
94 | ++outputChannelIndex; |
95 | } |
96 | } |
97 | } |
98 | |
99 | ASSERT(outputChannelIndex == output->numberOfChannels()); |
100 | } |
101 | |
102 | void ChannelMergerNode::reset() |
103 | { |
104 | } |
105 | |
106 | // Any time a connection or disconnection happens on any of our inputs, we potentially need to change the |
107 | // number of channels of our output. |
108 | void ChannelMergerNode::checkNumberOfChannelsForInput(AudioNodeInput* input) |
109 | { |
110 | ASSERT(context().isAudioThread() && context().isGraphOwner()); |
111 | |
112 | // Count how many channels we have all together from all of the inputs. |
113 | unsigned numberOfOutputChannels = 0; |
114 | for (unsigned i = 0; i < numberOfInputs(); ++i) { |
115 | AudioNodeInput* input = this->input(i); |
116 | if (input->isConnected()) |
117 | numberOfOutputChannels += input->numberOfChannels(); |
118 | } |
119 | |
120 | // Set the correct number of channels on the output |
121 | AudioNodeOutput* output = this->output(0); |
122 | ASSERT(output); |
123 | output->setNumberOfChannels(numberOfOutputChannels); |
124 | // There can in rare cases be a slight delay before the output bus is updated to the new number of |
125 | // channels because of tryLocks() in the context's updating system. So record the new number of |
126 | // output channels here. |
127 | m_desiredNumberOfOutputChannels = numberOfOutputChannels; |
128 | |
129 | AudioNode::checkNumberOfChannelsForInput(input); |
130 | } |
131 | |
132 | } // namespace WebCore |
133 | |
134 | #endif // ENABLE(WEB_AUDIO) |
135 | |