Home | History | Annotate | Download | only in webaudio
      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 "modules/webaudio/ChannelMergerNode.h"
     34 
     35 #include "modules/webaudio/AudioContext.h"
     36 #include "modules/webaudio/AudioNodeInput.h"
     37 #include "modules/webaudio/AudioNodeOutput.h"
     38 
     39 const unsigned DefaultNumberOfOutputChannels = 1;
     40 
     41 namespace WebCore {
     42 
     43 PassRefPtrWillBeRawPtr<ChannelMergerNode> ChannelMergerNode::create(AudioContext* context, float sampleRate, unsigned numberOfInputs)
     44 {
     45     if (!numberOfInputs || numberOfInputs > AudioContext::maxNumberOfChannels())
     46         return nullptr;
     47 
     48     return adoptRefWillBeNoop(new ChannelMergerNode(context, sampleRate, numberOfInputs));
     49 }
     50 
     51 ChannelMergerNode::ChannelMergerNode(AudioContext* context, float sampleRate, unsigned numberOfInputs)
     52     : AudioNode(context, sampleRate)
     53     , m_desiredNumberOfOutputChannels(DefaultNumberOfOutputChannels)
     54 {
     55     ScriptWrappable::init(this);
     56     // Create the requested number of inputs.
     57     for (unsigned i = 0; i < numberOfInputs; ++i)
     58         addInput(adoptPtr(new AudioNodeInput(this)));
     59 
     60     addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
     61     setNodeType(NodeTypeChannelMerger);
     62     initialize();
     63 }
     64 
     65 void ChannelMergerNode::process(size_t framesToProcess)
     66 {
     67     AudioNodeOutput* output = this->output(0);
     68     ASSERT(output);
     69     ASSERT_UNUSED(framesToProcess, framesToProcess == output->bus()->length());
     70 
     71     // Output bus not updated yet, so just output silence.
     72     if (m_desiredNumberOfOutputChannels != output->numberOfChannels()) {
     73         output->bus()->zero();
     74         return;
     75     }
     76 
     77     // Merge all the channels from all the inputs into one output.
     78     unsigned outputChannelIndex = 0;
     79     unsigned maxAllowedOutputChannels = output->numberOfChannels();
     80 
     81     for (unsigned i = 0; i < numberOfInputs(); ++i) {
     82         AudioNodeInput* input = this->input(i);
     83         if (input->isConnected()) {
     84             unsigned numberOfInputChannels = input->bus()->numberOfChannels();
     85 
     86             // Merge channels from this particular input, but be careful not to exceed the number of
     87             // output channels.  (This can happen if there are many inputs with each input
     88             // containing many channels.)
     89             for (unsigned j = 0; j < numberOfInputChannels; ++j) {
     90                 if (outputChannelIndex < maxAllowedOutputChannels) {
     91                     AudioChannel* inputChannel = input->bus()->channel(j);
     92                     AudioChannel* outputChannel = output->bus()->channel(outputChannelIndex);
     93                     outputChannel->copyFrom(inputChannel);
     94 
     95                     ++outputChannelIndex;
     96                 }
     97             }
     98         }
     99         if (outputChannelIndex >= maxAllowedOutputChannels)
    100             break;
    101     }
    102 
    103     ASSERT(outputChannelIndex == output->numberOfChannels());
    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     // If the actual number of channels exceeds the max allowed, just drop the excess.
    121     numberOfOutputChannels = std::min(numberOfOutputChannels, AudioContext::maxNumberOfChannels());
    122 
    123     // Set the correct number of channels on the output
    124     AudioNodeOutput* output = this->output(0);
    125     ASSERT(output);
    126     output->setNumberOfChannels(numberOfOutputChannels);
    127     // There can in rare cases be a slight delay before the output bus is updated to the new number of
    128     // channels because of tryLocks() in the context's updating system. So record the new number of
    129     // output channels here.
    130     m_desiredNumberOfOutputChannels = numberOfOutputChannels;
    131 
    132     AudioNode::checkNumberOfChannelsForInput(input);
    133 }
    134 
    135 } // namespace WebCore
    136 
    137 #endif // ENABLE(WEB_AUDIO)
    138