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