Home | History | Annotate | Download | only in webaudio
      1 /*
      2  * Copyright (C) 2011, 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/DynamicsCompressorNode.h"
     30 
     31 #include "platform/audio/DynamicsCompressor.h"
     32 #include "modules/webaudio/AudioContext.h"
     33 #include "modules/webaudio/AudioNodeInput.h"
     34 #include "modules/webaudio/AudioNodeOutput.h"
     35 
     36 // Set output to stereo by default.
     37 static const unsigned defaultNumberOfOutputChannels = 2;
     38 
     39 namespace WebCore {
     40 
     41 DynamicsCompressorNode::DynamicsCompressorNode(AudioContext* context, float sampleRate)
     42     : AudioNode(context, sampleRate)
     43 {
     44     ScriptWrappable::init(this);
     45     addInput(adoptPtr(new AudioNodeInput(this)));
     46     addOutput(adoptPtr(new AudioNodeOutput(this, defaultNumberOfOutputChannels)));
     47 
     48     setNodeType(NodeTypeDynamicsCompressor);
     49 
     50     m_threshold = AudioParam::create(context, "threshold", -24, -100, 0);
     51     m_knee = AudioParam::create(context, "knee", 30, 0, 40);
     52     m_ratio = AudioParam::create(context, "ratio", 12, 1, 20);
     53     m_reduction = AudioParam::create(context, "reduction", 0, -20, 0);
     54     m_attack = AudioParam::create(context, "attack", 0.003, 0, 1);
     55     m_release = AudioParam::create(context, "release", 0.250, 0, 1);
     56 
     57     initialize();
     58 }
     59 
     60 DynamicsCompressorNode::~DynamicsCompressorNode()
     61 {
     62     uninitialize();
     63 }
     64 
     65 void DynamicsCompressorNode::process(size_t framesToProcess)
     66 {
     67     AudioBus* outputBus = output(0)->bus();
     68     ASSERT(outputBus);
     69 
     70     float threshold = m_threshold->value();
     71     float knee = m_knee->value();
     72     float ratio = m_ratio->value();
     73     float attack = m_attack->value();
     74     float release = m_release->value();
     75 
     76     m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamThreshold, threshold);
     77     m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamKnee, knee);
     78     m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamRatio, ratio);
     79     m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamAttack, attack);
     80     m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamRelease, release);
     81 
     82     m_dynamicsCompressor->process(input(0)->bus(), outputBus, framesToProcess);
     83 
     84     float reduction = m_dynamicsCompressor->parameterValue(DynamicsCompressor::ParamReduction);
     85     m_reduction->setValue(reduction);
     86 }
     87 
     88 void DynamicsCompressorNode::initialize()
     89 {
     90     if (isInitialized())
     91         return;
     92 
     93     AudioNode::initialize();
     94     m_dynamicsCompressor = adoptPtr(new DynamicsCompressor(sampleRate(), defaultNumberOfOutputChannels));
     95 }
     96 
     97 void DynamicsCompressorNode::uninitialize()
     98 {
     99     if (!isInitialized())
    100         return;
    101 
    102     m_dynamicsCompressor.clear();
    103     AudioNode::uninitialize();
    104 }
    105 
    106 double DynamicsCompressorNode::tailTime() const
    107 {
    108     return m_dynamicsCompressor->tailTime();
    109 }
    110 
    111 double DynamicsCompressorNode::latencyTime() const
    112 {
    113     return m_dynamicsCompressor->latencyTime();
    114 }
    115 
    116 void DynamicsCompressorNode::trace(Visitor* visitor)
    117 {
    118     visitor->trace(m_threshold);
    119     visitor->trace(m_knee);
    120     visitor->trace(m_ratio);
    121     visitor->trace(m_reduction);
    122     visitor->trace(m_attack);
    123     visitor->trace(m_release);
    124     AudioNode::trace(visitor);
    125 }
    126 
    127 } // namespace WebCore
    128 
    129 #endif // ENABLE(WEB_AUDIO)
    130