2 * Copyright (C) 2011, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "DynamicsCompressorNode.h"
31 #include "AudioContext.h"
32 #include "AudioNodeInput.h"
33 #include "AudioNodeOutput.h"
34 #include "DynamicsCompressor.h"
36 // Set output to stereo by default.
37 static const unsigned defaultNumberOfOutputChannels = 2;
41 DynamicsCompressorNode::DynamicsCompressorNode(AudioContext* context, float sampleRate)
42 : AudioNode(context, sampleRate)
44 addInput(adoptPtr(new AudioNodeInput(this)));
45 addOutput(adoptPtr(new AudioNodeOutput(this, defaultNumberOfOutputChannels)));
47 setNodeType(NodeTypeDynamicsCompressor);
49 m_threshold = AudioParam::create("threshold", -24, -100, 0);
50 m_knee = AudioParam::create("knee", 30, 0, 40);
51 m_ratio = AudioParam::create("ratio", 12, 1, 20);
52 m_reduction = AudioParam::create("reduction", 0, -20, 0);
54 m_threshold->setContext(context);
55 m_knee->setContext(context);
56 m_ratio->setContext(context);
57 m_reduction->setContext(context);
62 DynamicsCompressorNode::~DynamicsCompressorNode()
67 void DynamicsCompressorNode::process(size_t framesToProcess)
69 AudioBus* outputBus = output(0)->bus();
72 float threshold = m_threshold->value();
73 float knee = m_knee->value();
74 float ratio = m_ratio->value();
76 m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamThreshold, threshold);
77 m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamKnee, knee);
78 m_dynamicsCompressor->setParameterValue(DynamicsCompressor::ParamRatio, ratio);
80 m_dynamicsCompressor->process(input(0)->bus(), outputBus, framesToProcess);
82 float reduction = m_dynamicsCompressor->parameterValue(DynamicsCompressor::ParamReduction);
83 m_reduction->setValue(reduction);
86 void DynamicsCompressorNode::reset()
88 m_dynamicsCompressor->reset();
91 void DynamicsCompressorNode::initialize()
96 AudioNode::initialize();
97 m_dynamicsCompressor = adoptPtr(new DynamicsCompressor(sampleRate(), defaultNumberOfOutputChannels));
100 void DynamicsCompressorNode::uninitialize()
102 if (!isInitialized())
105 m_dynamicsCompressor.clear();
106 AudioNode::uninitialize();
109 double DynamicsCompressorNode::tailTime() const
111 return m_dynamicsCompressor->tailTime();
114 double DynamicsCompressorNode::latencyTime() const
116 return m_dynamicsCompressor->latencyTime();
119 } // namespace WebCore
121 #endif // ENABLE(WEB_AUDIO)