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 Computer, 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
39const unsigned DefaultNumberOfOutputChannels = 1;
40
41namespace WebCore {
42
43PassRefPtr<ChannelMergerNode> ChannelMergerNode::create(AudioContext* context, float sampleRate, unsigned numberOfInputs)
44{
45    if (!numberOfInputs || numberOfInputs > AudioContext::maxNumberOfChannels())
46        return 0;
47
48    return adoptRef(new ChannelMergerNode(context, sampleRate, numberOfInputs));
49}
50
51ChannelMergerNode::ChannelMergerNode(AudioContext* context, float sampleRate, unsigned numberOfInputs)
52    : AudioNode(context, sampleRate)
53    , m_desiredNumberOfOutputChannels(DefaultNumberOfOutputChannels)
54{
55    // Create the requested number of inputs.
56    for (unsigned i = 0; i < numberOfInputs; ++i)
57        addInput(adoptPtr(new AudioNodeInput(this)));
58
59    addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
60
61    setNodeType(NodeTypeChannelMerger);
62
63    initialize();
64}
65
66void ChannelMergerNode::process(size_t framesToProcess)
67{
68    AudioNodeOutput* output = this->output(0);
69    ASSERT(output);
70    ASSERT_UNUSED(framesToProcess, framesToProcess == output->bus()->length());
71
72    // Output bus not updated yet, so just output silence.
73    if (m_desiredNumberOfOutputChannels != output->numberOfChannels()) {
74        output->bus()->zero();
75        return;
76    }
77
78    // Merge all the channels from all the inputs into one output.
79    unsigned outputChannelIndex = 0;
80    for (unsigned i = 0; i < numberOfInputs(); ++i) {
81        AudioNodeInput* input = this->input(i);
82        if (input->isConnected()) {
83            unsigned numberOfInputChannels = input->bus()->numberOfChannels();
84
85            // Merge channels from this particular input.
86            for (unsigned j = 0; j < numberOfInputChannels; ++j) {
87                AudioChannel* inputChannel = input->bus()->channel(j);
88                AudioChannel* outputChannel = output->bus()->channel(outputChannelIndex);
89                outputChannel->copyFrom(inputChannel);
90
91                ++outputChannelIndex;
92            }
93        }
94    }
95
96    ASSERT(outputChannelIndex == output->numberOfChannels());
97}
98
99void ChannelMergerNode::reset()
100{
101}
102
103// Any time a connection or disconnection happens on any of our inputs, we potentially need to change the
104// number of channels of our output.
105void ChannelMergerNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
106{
107    ASSERT(context()->isAudioThread() && context()->isGraphOwner());
108
109    // Count how many channels we have all together from all of the inputs.
110    unsigned numberOfOutputChannels = 0;
111    for (unsigned i = 0; i < numberOfInputs(); ++i) {
112        AudioNodeInput* input = this->input(i);
113        if (input->isConnected())
114            numberOfOutputChannels += input->numberOfChannels();
115    }
116
117    // Set the correct number of channels on the output
118    AudioNodeOutput* output = this->output(0);
119    ASSERT(output);
120    output->setNumberOfChannels(numberOfOutputChannels);
121    // There can in rare cases be a slight delay before the output bus is updated to the new number of
122    // channels because of tryLocks() in the context's updating system. So record the new number of
123    // output channels here.
124    m_desiredNumberOfOutputChannels = numberOfOutputChannels;
125
126    AudioNode::checkNumberOfChannelsForInput(input);
127}
128
129} // namespace WebCore
130
131#endif // ENABLE(WEB_AUDIO)
132