aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/filters/splitter.cpp
blob: 27ea697aa05f0ff54adcad5b9d236f70777d4ad8 (plain)
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
87
88
89

#include "config.h"

#include "splitter.h"

#include <cmath>
#include <limits>
#include <algorithm>

#include "math_defs.h"


void BandSplitter::init(float f0norm)
{
    float w = f0norm * F_TAU;
    float cw = std::cos(w);
    if(cw > std::numeric_limits<float>::epsilon())
        coeff = (std::sin(w) - 1.0f) / cw;
    else
        coeff = cw * -0.5f;

    lp_z1 = 0.0f;
    lp_z2 = 0.0f;
    ap_z1 = 0.0f;
}

void BandSplitter::process(float *RESTRICT hpout, float *RESTRICT lpout, const float *input, int count)
{
    ASSUME(count > 0);

    const float ap_coeff{this->coeff};
    const float lp_coeff{this->coeff*0.5f + 0.5f};
    float lp_z1{this->lp_z1};
    float lp_z2{this->lp_z2};
    float ap_z1{this->ap_z1};
    auto proc_sample = [ap_coeff,lp_coeff,&lp_z1,&lp_z2,&ap_z1,&lpout](const float in) noexcept -> float
    {
        /* Low-pass sample processing. */
        float d{(in - lp_z1) * lp_coeff};
        float lp_y{lp_z1 + d};
        lp_z1 = lp_y + d;

        d = (lp_y - lp_z2) * lp_coeff;
        lp_y = lp_z2 + d;
        lp_z2 = lp_y + d;

        *(lpout++) = lp_y;

        /* All-pass sample processing. */
        float ap_y{in*ap_coeff + ap_z1};
        ap_z1 = in - ap_y*ap_coeff;

        /* High-pass generated from removing low-passed output. */
        return ap_y - lp_y;
    };
    std::transform(input, input+count, hpout, proc_sample);
    this->lp_z1 = lp_z1;
    this->lp_z2 = lp_z2;
    this->ap_z1 = ap_z1;
}


void SplitterAllpass::init(float f0norm)
{
    float w = f0norm * F_TAU;
    float cw = std::cos(w);
    if(cw > std::numeric_limits<float>::epsilon())
        coeff = (std::sin(w) - 1.0f) / cw;
    else
        coeff = cw * -0.5f;

    z1 = 0.0f;
}

void SplitterAllpass::process(float *RESTRICT samples, int count)
{
    ASSUME(count > 0);

    const float coeff{this->coeff};
    float z1{this->z1};
    auto proc_sample = [coeff,&z1](const float in) noexcept -> float
    {
        float out{in*coeff + z1};
        z1 = in - out*coeff;
        return out;
    };
    std::transform(samples, samples+count, samples, proc_sample);
    this->z1 = z1;
}