OpenShot Audio Library | OpenShotAudio: juce_BallisticsFilter.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_BallisticsFilter.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::dsp
27{
28
29//==============================================================================
30template <typename SampleType>
31BallisticsFilter<SampleType>::BallisticsFilter()
32{
33 setAttackTime (attackTime);
34 setReleaseTime (releaseTime);
35}
36
37template <typename SampleType>
38 void BallisticsFilter<SampleType>::setAttackTime (SampleType attackTimeMs)
39{
40 attackTime = attackTimeMs;
41 cteAT = calculateLimitedCte (static_cast<SampleType> (attackTime));
42}
43
44template <typename SampleType>
45 void BallisticsFilter<SampleType>::setReleaseTime (SampleType releaseTimeMs)
46{
47 releaseTime = releaseTimeMs;
48 cteRL = calculateLimitedCte (static_cast<SampleType> (releaseTime));
49}
50
51template <typename SampleType>
52 void BallisticsFilter<SampleType>::setLevelCalculationType (LevelCalculationType newLevelType)
53{
54 levelType = newLevelType;
55 reset();
56}
57
58template <typename SampleType>
59 void BallisticsFilter<SampleType>::prepare (const ProcessSpec& spec)
60{
61 jassert (spec.sampleRate > 0);
62 jassert (spec.numChannels > 0);
63
64 sampleRate = spec.sampleRate;
65 expFactor = -2.0 * MathConstants<double>::pi * 1000.0 / sampleRate;
66
67 setAttackTime (attackTime);
68 setReleaseTime (releaseTime);
69
70 yold.resize (spec.numChannels);
71
72 reset();
73}
74
75template <typename SampleType>
76 void BallisticsFilter<SampleType>::reset()
77{
78 reset (0);
79}
80
81template <typename SampleType>
82 void BallisticsFilter<SampleType>::reset (SampleType initialValue)
83{
84 for (auto& old : yold)
85 old = initialValue;
86}
87
88template <typename SampleType>
89SampleType BallisticsFilter<SampleType>::processSample (int channel, SampleType inputValue)
90{
91 jassert (isPositiveAndBelow (channel, yold.size()));
92
93 if (levelType == LevelCalculationType::RMS)
94 inputValue *= inputValue;
95 else
96 inputValue = std::abs (inputValue);
97
98 SampleType cte = (inputValue > yold[(size_t) channel] ? cteAT : cteRL);
99
100 SampleType result = inputValue + cte * (yold[(size_t) channel] - inputValue);
101 yold[(size_t) channel] = result;
102
103 if (levelType == LevelCalculationType::RMS)
104 return std::sqrt (result);
105
106 return result;
107}
108
109template <typename SampleType>
110 void BallisticsFilter<SampleType>::snapToZero() noexcept
111{
112 for (auto& old : yold)
113 util::snapToZero (old);
114}
115
116template <typename SampleType>
117SampleType BallisticsFilter<SampleType>::calculateLimitedCte (SampleType timeMs) const noexcept
118{
119 return timeMs < static_cast<SampleType> (1.0e-3) ? 0
120 : static_cast<SampleType> (std::exp (expFactor / timeMs));
121}
122
123//==============================================================================
124template class BallisticsFilter<float>;
125template class BallisticsFilter<double>;
126
127} // namespace juce::dsp
juce::dsp::BallisticsFilter
Definition juce_BallisticsFilter.h:44
juce::dsp::BallisticsFilter::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_BallisticsFilter.cpp:89
juce::dsp::BallisticsFilter::reset
void reset()
Definition juce_BallisticsFilter.cpp:76
juce::dsp::BallisticsFilter::BallisticsFilter
BallisticsFilter()
Definition juce_BallisticsFilter.cpp:31
juce::dsp::BallisticsFilter::setAttackTime
void setAttackTime(SampleType attackTimeMs)
Definition juce_BallisticsFilter.cpp:38
juce::dsp::BallisticsFilter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_BallisticsFilter.cpp:59
juce::dsp::BallisticsFilter::setReleaseTime
void setReleaseTime(SampleType releaseTimeMs)
Definition juce_BallisticsFilter.cpp:45
juce::dsp::BallisticsFilter::snapToZero
void snapToZero() noexcept
Definition juce_BallisticsFilter.cpp:110
juce::dsp::BallisticsFilter::setLevelCalculationType
void setLevelCalculationType(LevelCalculationType newCalculationType)
Definition juce_BallisticsFilter.cpp:52
juce::MathConstants
Definition juce_MathsFunctions.h:139
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessSpec::numChannels
uint32 numChannels
Definition juce_ProcessContext.h:44
juce::dsp::ProcessSpec::sampleRate
double sampleRate
Definition juce_ProcessContext.h:38