summaryrefslogtreecommitdiff
path: root/src/tests/lfe-filter-test.c
blob: e64288d68f57a63592b880e537f23642303a47df (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***
  This file is part of PulseAudio.

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <check.h>

#include <pulse/pulseaudio.h>
#include <pulse/sample.h>
#include <pulsecore/memblock.h>

#include <pulsecore/filter/lfe-filter.h>

struct lfe_filter_test {
    pa_lfe_filter_t *lf;
    pa_mempool *pool;
    pa_sample_spec *ss;
};

static uint8_t *ori_sample_ptr;

#define ONE_BLOCK_SAMPLES 4096
#define TOTAL_SAMPLES 8192
#define TOLERANT_VARIATION 1

static void save_data_block(struct lfe_filter_test *lft, void *d, pa_memblock *blk) {
    uint8_t *dst = d, *src;
    size_t blk_size = pa_frame_size(lft->ss) * ONE_BLOCK_SAMPLES;

    src = pa_memblock_acquire(blk);
    memcpy(dst, src, blk_size);
    pa_memblock_release(blk);
}

static pa_memblock* generate_data_block(struct lfe_filter_test *lft, int start) {
    pa_memblock *r;
    uint8_t *d, *s = ori_sample_ptr;
    size_t blk_size = pa_frame_size(lft->ss) * ONE_BLOCK_SAMPLES;

    pa_assert_se(r = pa_memblock_new(lft->pool, blk_size));
    d = pa_memblock_acquire(r);
    memcpy(d, s + start,  blk_size);
    pa_memblock_release(r);

    return r;
}

static int compare_data_block(struct lfe_filter_test *lft, void *a, void *b) {
    int ret = 0;
    uint32_t i;
    uint16_t *r = a, *u = b;

    pa_assert(lft->ss->format == PA_SAMPLE_S16NE);

    for (i = 0; i < ONE_BLOCK_SAMPLES; i++) {
        if (abs(*r++ - *u++) > TOLERANT_VARIATION) {
            pa_log_error("lfe-filter-test: test failed, the output data in the position 0x%x of a block does not equal!\n", i);
            ret = -1;
            break;
        }
    }
    return ret;
}

/* in this test case, we pass two blocks of sample data to lfe-filter, each
   block contains 4096 samples, and don't let rewind_samples exceed TOTAL_SAMPLES */
static int lfe_filter_rewind_test(struct lfe_filter_test *lft, int rewind_samples)
{
    int ret = -1, pos, i;
    pa_memchunk mc;
    uint8_t *outptr;
    uint32_t fz = pa_frame_size(lft->ss);

    if (rewind_samples > TOTAL_SAMPLES || rewind_samples < TOTAL_SAMPLES - ONE_BLOCK_SAMPLES) {
        pa_log_error("lfe-filter-test: Please keep %d samples < rewind_samples < %d samples\n", TOTAL_SAMPLES - ONE_BLOCK_SAMPLES, TOTAL_SAMPLES);
        return ret;
    }

    outptr = pa_xmalloc(fz * TOTAL_SAMPLES);

    /* let lfe-filter process all samples first, and save the processed data to the temp buffer,
       then rewind back to some position, reprocess some samples and compare the output data with
       the processed data saved before. */
    for (i = 0; i < TOTAL_SAMPLES / ONE_BLOCK_SAMPLES; i++) {
        mc.memblock = generate_data_block(lft, i * ONE_BLOCK_SAMPLES * fz);
        mc.length = pa_memblock_get_length(mc.memblock);
        mc.index = 0;
        pa_lfe_filter_process(lft->lf, &mc);
        save_data_block(lft, outptr + i * ONE_BLOCK_SAMPLES * fz, mc.memblock);
        pa_memblock_unref(mc.memblock);
    }

    pa_lfe_filter_rewind(lft->lf, rewind_samples * fz);
    pos = (TOTAL_SAMPLES - rewind_samples) * fz;
    mc.memblock = generate_data_block(lft, pos);
    mc.length = pa_memblock_get_length(mc.memblock);
    mc.index = 0;
    pa_lfe_filter_process(lft->lf, &mc);
    ret = compare_data_block(lft, outptr + pos, pa_memblock_acquire(mc.memblock));
    pa_memblock_release(mc.memblock);
    pa_memblock_unref(mc.memblock);

    pa_xfree(outptr);

    return ret;
}

START_TEST (lfe_filter_test) {
    pa_sample_spec a;
    int ret = -1;
    unsigned i, crossover_freq = 120;
    pa_channel_map chmapmono = {1, {PA_CHANNEL_POSITION_LFE}};
    struct lfe_filter_test lft;
    short *tmp_ptr;

    pa_log_set_level(PA_LOG_DEBUG);

    a.channels = 1;
    a.rate = 44100;
    a.format = PA_SAMPLE_S16NE;

    lft.ss = &a;
    pa_assert_se(lft.pool = pa_mempool_new(false, 0));

    /* We prepare pseudo-random input audio samples for lfe-filter rewind testing*/
    ori_sample_ptr = pa_xmalloc(pa_frame_size(lft.ss) * TOTAL_SAMPLES);
    tmp_ptr = (short *) ori_sample_ptr;
    for (i = 0; i < pa_frame_size(lft.ss) * TOTAL_SAMPLES / sizeof(short); i++)
        *tmp_ptr++ = random();

    /* we create a lfe-filter with cutoff frequency 120Hz and max rewind time 10 seconds */
    pa_assert_se(lft.lf = pa_lfe_filter_new(&a, &chmapmono, crossover_freq, a.rate * 10));
    /* rewind to a block boundary */
    ret = lfe_filter_rewind_test(&lft, ONE_BLOCK_SAMPLES);
    if (ret)
        pa_log_error("lfe-filer-test: rewind to block boundary test failed!!!");
    pa_lfe_filter_free(lft.lf);

    /* we create a lfe-filter with cutoff frequency 120Hz and max rewind time 10 seconds */
    pa_assert_se(lft.lf = pa_lfe_filter_new(&a, &chmapmono, crossover_freq, a.rate * 10));
    /* rewind to the middle position of a block */
    ret = lfe_filter_rewind_test(&lft, ONE_BLOCK_SAMPLES + ONE_BLOCK_SAMPLES / 2);
    if (ret)
        pa_log_error("lfe-filer-test: rewind to middle of block test failed!!!");

    pa_xfree(ori_sample_ptr);

    pa_lfe_filter_free(lft.lf);

    pa_mempool_unref(lft.pool);

    if (!ret)
        pa_log_debug("lfe-filter-test: tests for both rewind to block boundary and rewind to middle position of a block passed!");

    fail_unless(ret == 0);
}
END_TEST

int main(int argc, char *argv[]) {
    int failed = 0;
    Suite *s;
    TCase *tc;
    SRunner *sr;

    if (!getenv("MAKE_CHECK"))
        pa_log_set_level(PA_LOG_DEBUG);

    s = suite_create("lfe-filter");
    tc = tcase_create("lfe-filter");
    tcase_add_test(tc, lfe_filter_test);
    tcase_set_timeout(tc, 10);
    suite_add_tcase(s, tc);

    sr = srunner_create(s);
    srunner_run_all(sr, CK_NORMAL);
    failed = srunner_ntests_failed(sr);
    srunner_free(sr);

    return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}