aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/filters/splitter.cpp
blob: 6aed749396d1c08281554106b1f9b225f0b98933 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#include "config.h"

#include "splitter.h"

#include "math_defs.h"


void bandsplit_init(BandSplitter *splitter, ALfloat f0norm)
{
    ALfloat w = f0norm * F_TAU;
    ALfloat cw = cosf(w);
    if(cw > FLT_EPSILON)
        splitter->coeff = (sinf(w) - 1.0f) / cw;
    else
        splitter->coeff = cw * -0.5f;

    splitter->lp_z1 = 0.0f;
    splitter->lp_z2 = 0.0f;
    splitter->hp_z1 = 0.0f;
}

void bandsplit_clear(BandSplitter *splitter)
{
    splitter->lp_z1 = 0.0f;
    splitter->lp_z2 = 0.0f;
    splitter->hp_z1 = 0.0f;
}

void bandsplit_process(BandSplitter *splitter, ALfloat *RESTRICT hpout, ALfloat *RESTRICT lpout,
                       const ALfloat *input, ALsizei count)
{
    ALfloat lp_coeff, hp_coeff, lp_y, hp_y, d;
    ALfloat lp_z1, lp_z2, hp_z1;
    ALsizei i;

    ASSUME(count > 0);

    hp_coeff = splitter->coeff;
    lp_coeff = splitter->coeff*0.5f + 0.5f;
    lp_z1 = splitter->lp_z1;
    lp_z2 = splitter->lp_z2;
    hp_z1 = splitter->hp_z1;
    for(i = 0;i < count;i++)
    {
        ALfloat in = input[i];

        /* Low-pass sample processing. */
        d = (in - lp_z1) * lp_coeff;
        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[i] = lp_y;

        /* All-pass sample processing. */
        hp_y = in*hp_coeff + hp_z1;
        hp_z1 = in - hp_y*hp_coeff;

        /* High-pass generated from removing low-passed output. */
        hpout[i] = hp_y - lp_y;
    }
    splitter->lp_z1 = lp_z1;
    splitter->lp_z2 = lp_z2;
    splitter->hp_z1 = hp_z1;
}


void splitterap_init(SplitterAllpass *splitter, ALfloat f0norm)
{
    ALfloat w = f0norm * F_TAU;
    ALfloat cw = cosf(w);
    if(cw > FLT_EPSILON)
        splitter->coeff = (sinf(w) - 1.0f) / cw;
    else
        splitter->coeff = cw * -0.5f;

    splitter->z1 = 0.0f;
}

void splitterap_clear(SplitterAllpass *splitter)
{
    splitter->z1 = 0.0f;
}

void splitterap_process(SplitterAllpass *splitter, ALfloat *RESTRICT samples, ALsizei count)
{
    ALfloat coeff, in, out;
    ALfloat z1;
    ALsizei i;

    ASSUME(count > 0);

    coeff = splitter->coeff;
    z1 = splitter->z1;
    for(i = 0;i < count;i++)
    {
        in = samples[i];

        out = in*coeff + z1;
        z1 = in - out*coeff;

        samples[i] = out;
    }
    splitter->z1 = z1;
}