chenlh
2026-03-26 36e42207da4c088b5bfd96f2cfc8944f890440d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
#pragma once
 
#define _USE_MATH_DEFINES
#include <cmath>
 
namespace ReverbHallRoom
{
    class Lp1
    {
    private:
        float fs;
        float b0, a1;
        float cutoffHz;
 
    public:
        float Output;
 
        Lp1()
        {
            fs = 48000;
            b0 = 1;
            a1 = 0;
            cutoffHz = 1000;
        }
 
        float GetSamplerate()
        {
            return fs;
        }
 
        void SetSamplerate(float samplerate)
        {
            fs = samplerate;
        }
 
        float GetCutoffHz()
        {
            return cutoffHz;
        }
 
        void SetCutoffHz(float hz)
        {
            cutoffHz = hz;
            Update();
        }
 
        void ClearBuffers()
        {
            Output = 0;
        }
 
        void Update()
        {
            // Prevent going over the Nyquist frequency
            if (cutoffHz >= fs * 0.5f)
                cutoffHz = fs * 0.499f;
 
            auto x = 2.0f * M_PI * cutoffHz / fs;
            auto nn = (2.0f - cosf(x));
            auto alpha = nn - sqrtf(nn * nn - 1);
 
            a1 = alpha;
            b0 = 1 - alpha;
        }
 
        float Process(float input)
        {
            if (input == 0 && Output < 0.0000001f)
            {
                Output = 0;
            }
            else
            {
                Output = b0 * input + a1 * Output;
            }
            return Output;
        }
 
        void Process(float* input, float* output, int len)
        {
            for (int i = 0; i < len; i++)
                output[i] = Process(input[i]);
        }
    };
}