~aleteoryx/muditaos

muditaos/module-audio/Audio/test/unittest_equalizer.cpp -rw-r--r-- 5.8 KiB
a405cad6Aleteoryx trim readme 8 days ago
                                                                                
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <catch2/catch.hpp>
#include <module-audio/Audio/equalizer/Equalizer.hpp>
#include <Utils.hpp>
#include <stdexcept>

SCENARIO("Calculate filter coeff")
{
    using namespace audio::equalizer;

    GIVEN("High pass filter")
    {
        const auto filterHighPass = qfilter_CalculateCoeffs(FilterType::HighPass, 300.9f, 44100, 0.701f, 0);
        THEN("Registers 1,2,3 should match b0 setup")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterHighPass.b0);
            REQUIRE(byte1 == 0x0F);
            REQUIRE(byte2 == 0x84);
            REQUIRE(byte3 == 0xAD);
        }
        THEN("Registers 4,5,6 should match b1 setup")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterHighPass.b1);
            REQUIRE(byte1 == 0xE0);
            REQUIRE(byte2 == 0xF6);
            REQUIRE(byte3 == 0xA6);
        }
        THEN("Registers 7,8,9 should match b2 setup")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterHighPass.b2);
            REQUIRE(byte1 == 0x0F);
            REQUIRE(byte2 == 0x84);
            REQUIRE(byte3 == 0xAD);
        }
        THEN("Registers 10,11,12 should match a1 setup")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterHighPass.a1);
            REQUIRE(byte1 == 0xE0);
            REQUIRE(byte2 == 0xFA);
            REQUIRE(byte3 == 0x4D);
        }
        THEN("Registers 13,14,15 should match a2 setup")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterHighPass.a2);
            REQUIRE(byte1 == 0x0F);
            REQUIRE(byte2 == 0x0D);
            REQUIRE(byte3 == 0x01);
        }
    }
    GIVEN("High shelf filter")
    {
        const auto filterHighShelf = qfilter_CalculateCoeffs(FilterType::LowShelf, 401.f, 44100, 0.701f, -10);
        THEN("Registers 1,2 should match b0 setup")
        {
            const auto [byte1, byte2, _] = utils::floatingPointConverter(filterHighShelf.b0);
            REQUIRE(byte1 == 0x0F);
            REQUIRE(byte2 == 0x9F);
        }
        THEN("Registers 4,5 should match b1 setup")
        {
            const auto [byte1, byte2, _] = utils::floatingPointConverter(filterHighShelf.b1);
            REQUIRE(byte1 == 0xE1);
            REQUIRE(byte2 == 0xB4);
        }
        THEN("Registers 7,8 should match b2 setup")
        {
            const auto [byte1, byte2, _] = utils::floatingPointConverter(filterHighShelf.b2);
            REQUIRE(byte1 == 0x0E);
            REQUIRE(byte2 == 0xB2);
        }
        THEN("Registers 10,11 should match a1 setup")
        {
            const auto [byte1, byte2, _] = utils::floatingPointConverter(filterHighShelf.a1);
            REQUIRE(byte1 == 0xE1);
            REQUIRE(byte2 == 0xBC);
        }
        THEN("Registers 13,14 should match a2 setup")
        {
            const auto [byte1, byte2, _] = utils::floatingPointConverter(filterHighShelf.a2);
            REQUIRE(byte1 == 0x0E);
            REQUIRE(byte2 == 0x5A);
        }
    }

    GIVEN("Filter none")
    {
        const auto filterNone = qfilter_CalculateCoeffs(FilterType::None, 0, 0, 0, 0);
        THEN("Register 1 should be equal to 16. Registers 2,3 should be equal to 0")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterNone.b0);
            REQUIRE(byte1 == 0x10);
            REQUIRE(byte2 == 0x00);
            REQUIRE(byte3 == 0x00);
        }
        THEN("Registers 4,5,6 should be equal to 0")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterNone.b1);
            REQUIRE(byte1 == 0x00);
            REQUIRE(byte2 == 0x00);
            REQUIRE(byte3 == 0x00);
        }
        THEN("Registers 7,8,9 should be equal to 0")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterNone.b2);
            REQUIRE(byte1 == 0x00);
            REQUIRE(byte2 == 0x00);
            REQUIRE(byte3 == 0x00);
        }
        THEN("Registers 10,11,12 should be equal to 0")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterNone.a1);
            REQUIRE(byte1 == 0x00);
            REQUIRE(byte2 == 0x00);
            REQUIRE(byte3 == 0x00);
        }
        THEN("Registers 13,14,15 should be equal to 0")
        {
            const auto [byte1, byte2, byte3] = utils::floatingPointConverter(filterNone.a2);
            REQUIRE(byte1 == 0x00);
            REQUIRE(byte2 == 0x00);
            REQUIRE(byte3 == 0x00);
        }
    }

    GIVEN("Filter with Q out of range")
    {
        WHEN("Q is too low")
        {
            THEN("Calculation of coefficients should throw")
            {
                REQUIRE_THROWS_AS(qfilter_CalculateCoeffs(FilterType::HighPass, 300.9f, 44100, -1.f, 0),
                                  std::invalid_argument);
            }
        }
        WHEN("Q is too high")
        {
            THEN("Calculation of coefficients should throw")
            {
                REQUIRE_THROWS_AS(qfilter_CalculateCoeffs(FilterType::HighPass, 300.9f, 44100, 100.f, 0),
                                  std::invalid_argument);
            }
        }
    }

    GIVEN("Filter with negative frequency")
    {
        THEN("Calculation of coefficients should throw")
        {
            REQUIRE_THROWS_AS(qfilter_CalculateCoeffs(FilterType::HighPass, -300.9f, 44100, 0.2f, 0),
                              std::invalid_argument);
        }
    }

    GIVEN("Filter with sample rate equal to 0")
    {
        THEN("Calculation of coefficients should throw")
        {
            REQUIRE_THROWS_AS(qfilter_CalculateCoeffs(FilterType::HighPass, 300.9f, 0, 0.2f, 0), std::invalid_argument);
        }
    }
}