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 "modules/webaudio/GainNode.h" 30 31#include "platform/audio/AudioBus.h" 32#include "modules/webaudio/AudioNodeInput.h" 33#include "modules/webaudio/AudioNodeOutput.h" 34 35namespace blink { 36 37GainNode::GainNode(AudioContext* context, float sampleRate) 38 : AudioNode(context, sampleRate) 39 , m_lastGain(1.0) 40 , m_sampleAccurateGainValues(AudioNode::ProcessingSizeInFrames) // FIXME: can probably share temp buffer in context 41{ 42 m_gain = AudioParam::create(context, 1.0); 43 44 addInput(); 45 addOutput(AudioNodeOutput::create(this, 1)); 46 47 setNodeType(NodeTypeGain); 48 49 initialize(); 50} 51 52void GainNode::process(size_t framesToProcess) 53{ 54 // FIXME: for some cases there is a nice optimization to avoid processing here, and let the gain change 55 // happen in the summing junction input of the AudioNode we're connected to. 56 // Then we can avoid all of the following: 57 58 AudioBus* outputBus = output(0)->bus(); 59 ASSERT(outputBus); 60 61 if (!isInitialized() || !input(0)->isConnected()) 62 outputBus->zero(); 63 else { 64 AudioBus* inputBus = input(0)->bus(); 65 66 if (gain()->hasSampleAccurateValues()) { 67 // Apply sample-accurate gain scaling for precise envelopes, grain windows, etc. 68 ASSERT(framesToProcess <= m_sampleAccurateGainValues.size()); 69 if (framesToProcess <= m_sampleAccurateGainValues.size()) { 70 float* gainValues = m_sampleAccurateGainValues.data(); 71 gain()->calculateSampleAccurateValues(gainValues, framesToProcess); 72 outputBus->copyWithSampleAccurateGainValuesFrom(*inputBus, gainValues, framesToProcess); 73 } 74 } else { 75 // Apply the gain with de-zippering into the output bus. 76 outputBus->copyWithGainFrom(*inputBus, &m_lastGain, gain()->value()); 77 } 78 } 79} 80 81// FIXME: this can go away when we do mixing with gain directly in summing junction of AudioNodeInput 82// 83// As soon as we know the channel count of our input, we can lazily initialize. 84// Sometimes this may be called more than once with different channel counts, in which case we must safely 85// uninitialize and then re-initialize with the new channel count. 86void GainNode::checkNumberOfChannelsForInput(AudioNodeInput* input) 87{ 88 ASSERT(context()->isAudioThread() && context()->isGraphOwner()); 89 90 ASSERT(input && input == this->input(0)); 91 if (input != this->input(0)) 92 return; 93 94 unsigned numberOfChannels = input->numberOfChannels(); 95 96 if (isInitialized() && numberOfChannels != output(0)->numberOfChannels()) { 97 // We're already initialized but the channel count has changed. 98 uninitialize(); 99 } 100 101 if (!isInitialized()) { 102 // This will propagate the channel count to any nodes connected further downstream in the graph. 103 output(0)->setNumberOfChannels(numberOfChannels); 104 initialize(); 105 } 106 107 AudioNode::checkNumberOfChannelsForInput(input); 108} 109 110void GainNode::trace(Visitor* visitor) 111{ 112 visitor->trace(m_gain); 113 AudioNode::trace(visitor); 114} 115 116} // namespace blink 117 118#endif // ENABLE(WEB_AUDIO) 119