Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "compressor.h"
23 :
24 : #include "audiomathutils.h"
25 :
26 : #include "log.h"
27 :
28 : using namespace muse::audio;
29 : using namespace muse::audio::dsp;
30 :
31 : static constexpr float RATIO = 4.f;
32 :
33 0 : Compressor::Compressor(const unsigned int sampleRate)
34 0 : : m_filterConfig(sampleRate, RATIO),
35 0 : m_feedbackFactor(1.f) // 0 = feed forward, 1 = feed back
36 : {
37 0 : }
38 :
39 0 : bool Compressor::isActive() const
40 : {
41 0 : return m_isActive;
42 : }
43 :
44 0 : void Compressor::setIsActive(const bool active)
45 : {
46 0 : m_isActive = active;
47 0 : }
48 :
49 0 : volume_db_t Compressor::gainSmoothing(const float& newGainReduction) const
50 : {
51 0 : float coefficient = 0.f;
52 :
53 0 : if (newGainReduction <= m_previousGainReduction) {
54 0 : coefficient = m_filterConfig.attackTimeCoefficient();
55 : } else {
56 0 : coefficient = m_filterConfig.releaseTimeCoefficient();
57 : }
58 :
59 0 : return (coefficient * m_previousGainReduction) + ((1 - coefficient) * newGainReduction);
60 : }
61 :
62 0 : volume_db_t Compressor::computeGain(const volume_db_t& logarithmSample) const
63 : {
64 0 : if (logarithmSample < m_softThresholdLower) {
65 0 : return logarithmSample;
66 : }
67 :
68 0 : if (logarithmSample >= m_softThresholdLower && logarithmSample <= m_softThresholdUpper) {
69 : return logarithmSample
70 0 : + ((((1 / m_filterConfig.ratio()) - 1) * std::pow(logarithmSample - m_softThresholdUpper, 2))
71 0 : / (2 * m_filterConfig.kneeWidth()));
72 : }
73 :
74 0 : return m_filterConfig.logarithmicThreshold()
75 0 : + ((logarithmSample - m_filterConfig.logarithmicThreshold()) / m_filterConfig.ratio());
76 : }
77 :
78 0 : void Compressor::process(const float linearRms, float* buffer, const audioch_t& audioChannelsCount, const samples_t samplesPerChannel)
79 : {
80 0 : float dbGain = muse::linear_to_db(linearRms);
81 :
82 0 : if (dbGain <= m_filterConfig.minimumOperableLevel()) {
83 0 : return;
84 : }
85 :
86 : // trying to predict the next sample by the previous gain reduction
87 0 : dbGain += m_feedbackFactor * m_feedbackGain;
88 :
89 0 : float dbDiff = computeGain(dbGain) - dbGain;
90 :
91 0 : m_feedbackGain = dbDiff;
92 0 : float gainFact = muse::db_to_linear(dbDiff * (1.f + m_feedbackFactor));
93 :
94 0 : float currentGainReduction = std::min(gainFact, m_previousGainReduction);
95 :
96 : // apply gain
97 0 : for (audioch_t audioChNum = 0; audioChNum < audioChannelsCount; ++audioChNum) {
98 0 : multiplySamples(buffer, audioChannelsCount, audioChNum, samplesPerChannel, currentGainReduction);
99 : }
100 :
101 0 : m_previousGainReduction = currentGainReduction;
102 : }
|