OpenShot Audio Library | OpenShotAudio  0.6.0
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 
26 namespace juce::dsp
27 {
28 
29 //==============================================================================
30 template <typename SampleType>
32 {
33  setAttackTime (attackTime);
34  setReleaseTime (releaseTime);
35 }
36 
37 template <typename SampleType>
38 void BallisticsFilter<SampleType>::setAttackTime (SampleType attackTimeMs)
39 {
40  attackTime = attackTimeMs;
41  cteAT = calculateLimitedCte (static_cast<SampleType> (attackTime));
42 }
43 
44 template <typename SampleType>
45 void BallisticsFilter<SampleType>::setReleaseTime (SampleType releaseTimeMs)
46 {
47  releaseTime = releaseTimeMs;
48  cteRL = calculateLimitedCte (static_cast<SampleType> (releaseTime));
49 }
50 
51 template <typename SampleType>
52 void BallisticsFilter<SampleType>::setLevelCalculationType (LevelCalculationType newLevelType)
53 {
54  levelType = newLevelType;
55  reset();
56 }
57 
58 template <typename SampleType>
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 
75 template <typename SampleType>
77 {
78  reset (0);
79 }
80 
81 template <typename SampleType>
82 void BallisticsFilter<SampleType>::reset (SampleType initialValue)
83 {
84  for (auto& old : yold)
85  old = initialValue;
86 }
87 
88 template <typename SampleType>
89 SampleType 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 
109 template <typename SampleType>
111 {
112  for (auto& old : yold)
113  util::snapToZero (old);
114 }
115 
116 template <typename SampleType>
117 SampleType 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 //==============================================================================
124 template class BallisticsFilter<float>;
125 template class BallisticsFilter<double>;
126 
127 } // namespace juce::dsp
SampleType processSample(int channel, SampleType inputValue)
void setAttackTime(SampleType attackTimeMs)
void prepare(const ProcessSpec &spec)
void setReleaseTime(SampleType releaseTimeMs)
void setLevelCalculationType(LevelCalculationType newCalculationType)