Skip to content

Commit bab7157

Browse files
test(cmocka, dcblock): adopt test to use SOF API
Signed-off-by: Piotr Hoppe <piotr.hoppe@intel.com>
1 parent fb53107 commit bab7157

2 files changed

Lines changed: 65 additions & 36 deletions

File tree

test/cmocka/src/audio/dcblock/dcblock_math_test.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,34 @@
1111
#include <string.h>
1212
#include <cmocka.h>
1313

14-
#include <sof/audio/audio_stream.h>
14+
#include <sof/common.h>
1515
#include <sof/audio/format.h>
16+
#include <sof/audio/audio_stream.h>
1617

1718
#include "dcblock.h"
1819

1920
#define TEST_CHANNELS 2
2021
#define TEST_FRAMES 256
2122

23+
/*
24+
* The dcblock processing functions operate on struct cir_buf_source /
25+
* struct cir_buf_sink circular buffer views. The tests use static, non
26+
* wrapping sample buffers, so the views simply span the whole array.
27+
*/
28+
static void cir_buf_src_setup(struct cir_buf_source *s, const void *data, size_t bytes)
29+
{
30+
s->buf_start = data;
31+
s->buf_end = (const char *)data + bytes;
32+
s->ptr = data;
33+
}
34+
35+
static void cir_buf_snk_setup(struct cir_buf_sink *s, void *data, size_t bytes)
36+
{
37+
s->buf_start = data;
38+
s->buf_end = (char *)data + bytes;
39+
s->ptr = data;
40+
}
41+
2242
/* Q2.30 coefficient close to 1.0 used for the DC removal test cases. */
2343
#define R_COEF_NEAR_ONE 1063004406 /* ~0.99 in Q2.30 */
2444

@@ -41,20 +61,6 @@ static double dcblock_ref(struct ref_state *s, double r, double x)
4161
return y;
4262
}
4363

44-
/* Build an audio_stream over a linear (non wrapping) sample buffer. */
45-
static void setup_stream(struct audio_stream *stream, void *data,
46-
size_t bytes, enum sof_ipc_frame fmt, int channels)
47-
{
48-
memset(stream, 0, sizeof(*stream));
49-
stream->addr = data;
50-
stream->end_addr = (char *)data + bytes;
51-
stream->r_ptr = data;
52-
stream->w_ptr = data;
53-
stream->size = bytes;
54-
stream->runtime_stream_params.frame_fmt = fmt;
55-
stream->runtime_stream_params.channels = channels;
56-
}
57-
5864
/* Fill the source buffer with a per-channel sinusoid plus a DC offset. */
5965
static void gen_input(double *ref_in, int channels, int frames, double dc)
6066
{
@@ -77,7 +83,8 @@ static void gen_input(double *ref_in, int channels, int frames, double dc)
7783
static double run_s32_case(int32_t r_coeff, double dc, double tol_rel)
7884
{
7985
struct comp_data cd;
80-
struct audio_stream source, sink;
86+
struct cir_buf_source csrc;
87+
struct cir_buf_sink csnk;
8188
int32_t src[TEST_FRAMES * TEST_CHANNELS];
8289
int32_t dst[TEST_FRAMES * TEST_CHANNELS];
8390
double ref_in[TEST_FRAMES * TEST_CHANNELS];
@@ -89,6 +96,7 @@ static double run_s32_case(int32_t r_coeff, double dc, double tol_rel)
8996
int ch, i;
9097

9198
memset(&cd, 0, sizeof(cd));
99+
cd.channels = TEST_CHANNELS;
92100
for (ch = 0; ch < TEST_CHANNELS; ch++)
93101
cd.R_coeffs[ch] = r_coeff;
94102

@@ -98,8 +106,8 @@ static double run_s32_case(int32_t r_coeff, double dc, double tol_rel)
98106
for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++)
99107
src[i] = (int32_t)round(ref_in[i] * 2147483647.0);
100108

101-
setup_stream(&source, src, sizeof(src), SOF_IPC_FRAME_S32_LE, TEST_CHANNELS);
102-
setup_stream(&sink, dst, sizeof(dst), SOF_IPC_FRAME_S32_LE, TEST_CHANNELS);
109+
cir_buf_src_setup(&csrc, src, sizeof(src));
110+
cir_buf_snk_setup(&csnk, dst, sizeof(dst));
103111

104112
func = dcblock_find_func(SOF_IPC_FRAME_S32_LE);
105113
assert_non_null(func);
@@ -156,22 +164,24 @@ static void test_dcblock_saturation(void **state)
156164
(void)state;
157165

158166
struct comp_data cd;
159-
struct audio_stream source, sink;
167+
struct cir_buf_source csrc;
168+
struct cir_buf_sink csnk;
160169
int32_t src[TEST_FRAMES * TEST_CHANNELS];
161170
int32_t dst[TEST_FRAMES * TEST_CHANNELS];
162171
dcblock_func func;
163172
int i;
164173

165174
memset(&cd, 0, sizeof(cd));
175+
cd.channels = TEST_CHANNELS;
166176
for (i = 0; i < TEST_CHANNELS; i++)
167177
cd.R_coeffs[i] = ONE_Q2_30;
168178

169179
/* Alternating +full/-full scale is the worst case for the difference. */
170180
for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++)
171181
src[i] = (i & 1) ? INT32_MAX : INT32_MIN;
172182

173-
setup_stream(&source, src, sizeof(src), SOF_IPC_FRAME_S32_LE, TEST_CHANNELS);
174-
setup_stream(&sink, dst, sizeof(dst), SOF_IPC_FRAME_S32_LE, TEST_CHANNELS);
183+
cir_buf_src_setup(&csrc, src, sizeof(src));
184+
cir_buf_snk_setup(&csnk, dst, sizeof(dst));
175185

176186
func = dcblock_find_func(SOF_IPC_FRAME_S32_LE);
177187
assert_non_null(func);
@@ -199,7 +209,8 @@ static void test_dcblock_bitexact_s32(void **state)
199209
static void run_s16_case(int32_t r_coeff, double dc)
200210
{
201211
struct comp_data cd;
202-
struct audio_stream source, sink;
212+
struct cir_buf_source csrc;
213+
struct cir_buf_sink csnk;
203214
int16_t src[TEST_FRAMES * TEST_CHANNELS];
204215
int16_t dst[TEST_FRAMES * TEST_CHANNELS];
205216
double ref_in[TEST_FRAMES * TEST_CHANNELS];
@@ -210,6 +221,7 @@ static void run_s16_case(int32_t r_coeff, double dc)
210221
int ch, i;
211222

212223
memset(&cd, 0, sizeof(cd));
224+
cd.channels = TEST_CHANNELS;
213225
for (ch = 0; ch < TEST_CHANNELS; ch++)
214226
cd.R_coeffs[ch] = r_coeff;
215227

@@ -219,8 +231,8 @@ static void run_s16_case(int32_t r_coeff, double dc)
219231
for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++)
220232
src[i] = (int16_t)round(ref_in[i] * 32767.0);
221233

222-
setup_stream(&source, src, sizeof(src), SOF_IPC_FRAME_S16_LE, TEST_CHANNELS);
223-
setup_stream(&sink, dst, sizeof(dst), SOF_IPC_FRAME_S16_LE, TEST_CHANNELS);
234+
cir_buf_src_setup(&csrc, src, sizeof(src));
235+
cir_buf_snk_setup(&csnk, dst, sizeof(dst));
224236

225237
func = dcblock_find_func(SOF_IPC_FRAME_S16_LE);
226238
assert_non_null(func);
@@ -258,7 +270,8 @@ static void test_dcblock_bitexact_s16(void **state)
258270
static void run_s24_case(int32_t r_coeff, double dc)
259271
{
260272
struct comp_data cd;
261-
struct audio_stream source, sink;
273+
struct cir_buf_source csrc;
274+
struct cir_buf_sink csnk;
262275
int32_t src[TEST_FRAMES * TEST_CHANNELS];
263276
int32_t dst[TEST_FRAMES * TEST_CHANNELS];
264277
double ref_in[TEST_FRAMES * TEST_CHANNELS];
@@ -269,6 +282,7 @@ static void run_s24_case(int32_t r_coeff, double dc)
269282
int ch, i;
270283

271284
memset(&cd, 0, sizeof(cd));
285+
cd.channels = TEST_CHANNELS;
272286
for (ch = 0; ch < TEST_CHANNELS; ch++)
273287
cd.R_coeffs[ch] = r_coeff;
274288

@@ -278,12 +292,12 @@ static void run_s24_case(int32_t r_coeff, double dc)
278292
for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++)
279293
src[i] = (int32_t)round(ref_in[i] * 8388607.0);
280294

281-
setup_stream(&source, src, sizeof(src), SOF_IPC_FRAME_S24_4LE, TEST_CHANNELS);
282-
setup_stream(&sink, dst, sizeof(dst), SOF_IPC_FRAME_S24_4LE, TEST_CHANNELS);
295+
cir_buf_src_setup(&csrc, src, sizeof(src));
296+
cir_buf_snk_setup(&csnk, dst, sizeof(dst));
283297

284298
func = dcblock_find_func(SOF_IPC_FRAME_S24_4LE);
285299
assert_non_null(func);
286-
func(&cd, &source, &sink, TEST_FRAMES);
300+
func(&cd, &csrc, &csnk, TEST_FRAMES);
287301

288302
for (i = 0; i < TEST_FRAMES; i++) {
289303
for (ch = 0; ch < TEST_CHANNELS; ch++) {

test/cmocka/src/audio/dcblock/dcblock_process.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <kernel/header.h>
1313
#include <sof/audio/component_ext.h>
1414
#include <sof/audio/module_adapter/module/generic.h>
15+
#include <sof/audio/audio_buffer.h>
16+
#include <sof/audio/source_api.h>
17+
#include <sof/audio/sink_api.h>
1518
#include <sof/audio/format.h>
1619
#include <ipc/control.h>
1720

@@ -188,6 +191,8 @@ static int setup(void **state)
188191
struct test_data *td;
189192
struct sof_ipc_comp_process *ipc;
190193
struct comp_dev *dev;
194+
struct sof_source *sources[1];
195+
struct sof_sink *sinks[1];
191196
int ret;
192197
int i;
193198

@@ -234,7 +239,9 @@ static int setup(void **state)
234239
mod->stream_params->channels = params->channels;
235240
mod->period_bytes = get_frame_bytes(params->source_format, params->channels) * 48000 / 1000;
236241

237-
ret = module_prepare(mod, NULL, 0, NULL, 0);
242+
sources[0] = audio_buffer_get_source(&td->source->audio_buffer);
243+
sinks[0] = audio_buffer_get_sink(&td->sink->audio_buffer);
244+
ret = module_prepare(mod, sources, 1, sinks, 1);
238245
if (ret)
239246
return ret;
240247

@@ -485,9 +492,15 @@ static void test_audio_dcblock(void **state)
485492
struct processing_module *mod = comp_mod(td->dev);
486493
struct comp_buffer *source = td->source;
487494
struct comp_buffer *sink = td->sink;
495+
struct sof_source *sources[1];
496+
struct sof_sink *sinks[1];
497+
uint32_t avail_before;
488498
int ret;
489499
int frames;
490500

501+
sources[0] = audio_buffer_get_source(&source->audio_buffer);
502+
sinks[0] = audio_buffer_get_sink(&sink->audio_buffer);
503+
491504
while (td->continue_loop) {
492505
frames = frames_jitter(td->params->frames);
493506
switch (audio_stream_get_frm_fmt(&source->stream)) {
@@ -505,14 +518,16 @@ static void test_audio_dcblock(void **state)
505518
break;
506519
}
507520

508-
mod->input_buffers[0].consumed = 0;
509-
mod->output_buffers[0].size = 0;
510-
ret = module_process_legacy(mod, mod->input_buffers, 1,
511-
mod->output_buffers, 1);
521+
/* Process exactly the frames just filled. The source/sink API
522+
* updates the buffer read/write positions internally, so record
523+
* the sink fill level to know how many bytes were produced.
524+
*/
525+
td->dev->frames = mod->input_buffers[0].size;
526+
avail_before = audio_stream_get_avail_bytes(&sink->stream);
527+
ret = module_process_sink_src(mod, sources, 1, sinks, 1);
512528
assert_int_equal(ret, 0);
513-
514-
comp_update_buffer_consume(source, mod->input_buffers[0].consumed);
515-
comp_update_buffer_produce(sink, mod->output_buffers[0].size);
529+
mod->output_buffers[0].size =
530+
audio_stream_get_avail_bytes(&sink->stream) - avail_before;
516531

517532
switch (audio_stream_get_frm_fmt(&sink->stream)) {
518533
case SOF_IPC_FRAME_S16_LE:

0 commit comments

Comments
 (0)