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 "equaliser.h"
23 :
24 : #include <cmath>
25 :
26 : #include "log.h"
27 :
28 : using namespace muse::audio;
29 : using namespace muse::audio::fx;
30 :
31 0 : void Equaliser::setOutputSpec(const OutputSpec& spec)
32 : {
33 0 : m_outputSpec = spec;
34 0 : calculate();
35 0 : }
36 :
37 0 : bool Equaliser::active() const
38 : {
39 0 : return m_active;
40 : }
41 :
42 0 : void Equaliser::setActive(bool active)
43 : {
44 0 : m_active = active;
45 0 : }
46 :
47 0 : void Equaliser::setMode(const ProcessMode)
48 : {
49 0 : }
50 :
51 0 : bool Equaliser::shouldProcessDuringSilence() const
52 : {
53 0 : return false;
54 : }
55 :
56 0 : void Equaliser::process(float* buffer, samples_t sampleCount, samples_t)
57 : {
58 0 : for (samples_t i = 0; i < sampleCount; ++i) {
59 0 : m_x[2] = m_x[1];
60 0 : m_x[1] = m_x[0];
61 0 : m_x[0] = buffer[i];
62 :
63 0 : m_y[2] = m_y[1];
64 0 : m_y[1] = m_y[0];
65 0 : m_y[0] = (m_b[2] * m_x[2] + m_b[1] * m_x[1] + m_b[0] * m_x[0] - m_a[1] * m_y[1] - m_a[2] * m_y[2]) / m_a[0];
66 :
67 0 : buffer[i] = m_y[0];
68 : }
69 0 : }
70 :
71 0 : void Equaliser::calculate()
72 : {
73 0 : if (!m_outputSpec.sampleRate) {
74 0 : return;
75 : }
76 0 : float a = std::pow(10.f, m_gain / 40.f);
77 0 : float w0 = 2 * M_PI * m_frequency / m_outputSpec.sampleRate;
78 0 : float alpha = std::sin(w0) * a / (2 * m_q);
79 :
80 0 : m_b[0] = 1 + alpha * a;
81 0 : m_b[1] = -2 * std::cos(w0);
82 0 : m_b[2] = 1 - alpha * a;
83 :
84 0 : m_a[0] = 1 + alpha / a;
85 0 : m_a[1] = -2 * std::cos(w0);
86 0 : m_a[2] = 1 - alpha / a;
87 : }
88 :
89 0 : void Equaliser::setFrequency(float value)
90 : {
91 0 : m_frequency = value;
92 0 : calculate();
93 0 : }
94 :
95 0 : void Equaliser::setGain(float value)
96 : {
97 0 : m_gain = value;
98 0 : calculate();
99 0 : }
100 :
101 0 : void Equaliser::setQ(float value)
102 : {
103 0 : m_q = value;
104 0 : calculate();
105 0 : }
|