Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
aubuf: refactor aubuf_auframe test (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Sep 27, 2022
1 parent dbb6b81 commit ad8ea9b
Showing 1 changed file with 101 additions and 75 deletions.
176 changes: 101 additions & 75 deletions src/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,46 @@

#define AUDIO_TIMEBASE 1000000U

enum {
FRAMES = 80,
};


static int test_aubuf_raw(void)
{
struct aubuf *ab = NULL;
int16_t sampv_in[160];
int16_t sampv_out[160];
int16_t sampv_in[2 * FRAMES];
int16_t sampv_out[2 * FRAMES];
struct mbuf *mb;
unsigned i;
int err;

mb = mbuf_alloc(80 * sizeof(int16_t));
mb = mbuf_alloc(FRAMES * sizeof(int16_t));
if (!mb)
return ENOMEM;

for (i=0; i<ARRAY_SIZE(sampv_in); i++)
sampv_in[i] = i;
memset(sampv_out, 0, sizeof(sampv_out));

err = aubuf_alloc(&ab, 320, 0);
err = aubuf_alloc(&ab, 4 * FRAMES, 0);
TEST_ERR(err);
TEST_EQUALS(0, aubuf_cur_size(ab));

err = aubuf_write(ab, (uint8_t *)sampv_in, 80 * sizeof(int16_t));
err = aubuf_write(ab, (uint8_t *)sampv_in, FRAMES * sizeof(int16_t));
TEST_ERR(err);
TEST_EQUALS(160, aubuf_cur_size(ab));
TEST_EQUALS(2 * FRAMES, aubuf_cur_size(ab));

(void)mbuf_write_mem(mb, (uint8_t *)&sampv_in[80],
80 * sizeof(int16_t));
(void)mbuf_write_mem(mb, (uint8_t *)&sampv_in[FRAMES],
FRAMES * sizeof(int16_t));
mb->pos = 0;

err = aubuf_append(ab, mb);
TEST_ERR(err);
TEST_EQUALS(320, aubuf_cur_size(ab));
TEST_EQUALS(4 * FRAMES, aubuf_cur_size(ab));

memset(sampv_out, 0, sizeof(sampv_out));
aubuf_read(ab, (uint8_t *)sampv_out, 160 * sizeof(int16_t));
aubuf_read(ab, (uint8_t *)sampv_out, 2 * FRAMES * sizeof(int16_t));
TEST_MEMCMP(sampv_in, sizeof(sampv_in), sampv_out, sizeof(sampv_out));
TEST_EQUALS(0, aubuf_cur_size(ab));

Expand All @@ -64,25 +68,25 @@ static int test_aubuf_raw(void)
static int test_aubuf_samp(void)
{
struct aubuf *ab = NULL;
int16_t sampv_in[160];
int16_t sampv_out[160];
int16_t sampv_in[2 * FRAMES];
int16_t sampv_out[2 * FRAMES];
unsigned i;
int err;

for (i=0; i<ARRAY_SIZE(sampv_in); i++)
sampv_in[i] = i;
memset(sampv_out, 0, sizeof(sampv_out));

err = aubuf_alloc(&ab, 320, 0);
err = aubuf_alloc(&ab, 4 * FRAMES, 0);
TEST_ERR(err);

TEST_EQUALS(0, aubuf_cur_size(ab));

err |= aubuf_write_samp(ab, sampv_in, 80);
err |= aubuf_write_samp(ab, &sampv_in[80], 80);
err |= aubuf_write_samp(ab, sampv_in, FRAMES);
err |= aubuf_write_samp(ab, &sampv_in[FRAMES], FRAMES);
TEST_ERR(err);

TEST_EQUALS(320, aubuf_cur_size(ab));
TEST_EQUALS(4 * FRAMES, aubuf_cur_size(ab));

aubuf_read_samp(ab, sampv_out, ARRAY_SIZE(sampv_out));
TEST_MEMCMP(sampv_in, sizeof(sampv_in), sampv_out, sizeof(sampv_out));
Expand All @@ -97,74 +101,96 @@ static int test_aubuf_samp(void)
static int test_aubuf_auframe(void)
{
struct aubuf *ab = NULL;
float sampv_in[160];
float sampv_out[160];
float sampv_in[3 * FRAMES + (FRAMES / 2)];
float sampv_out[3 * FRAMES + (FRAMES / 2)];
uint64_t dt;

struct auframe af_in;
struct auframe af_out;
unsigned i;
int err;

for (i=0; i<ARRAY_SIZE(sampv_in); i++)
for (unsigned i = 0; i < ARRAY_SIZE(sampv_in); i++)
sampv_in[i] = (float)i;
memset(sampv_out, 0, sizeof(sampv_out));

err = aubuf_alloc(&ab, 80 * sizeof(float), 4 * 80 * sizeof(float));
err = aubuf_alloc(&ab, FRAMES * sizeof(float),
4 * FRAMES * sizeof(float));
TEST_ERR(err);

TEST_EQUALS(0, aubuf_cur_size(ab));

/* write one frame */
auframe_init(&af_in, AUFMT_FLOAT, sampv_in, 80, 48000, 2);
/* write first frame (filling with wish_sz) */
auframe_init(&af_in, AUFMT_FLOAT, sampv_in, FRAMES, 48000, 2);
af_in.timestamp = 0;

err |= aubuf_write_auframe(ab, &af_in);
dt = FRAMES * AUDIO_TIMEBASE / (af_in.srate * af_in.ch);

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(FRAMES * sizeof(float), aubuf_cur_size(ab));

dt = 80 * AUDIO_TIMEBASE / (af_in.srate * af_in.ch);
af_in.sampv = &sampv_in[80];
af_in.sampc = 80;
/* first read after filling should start aubuf */
af_out.fmt = AUFMT_FLOAT;
af_out.sampv = sampv_out;
af_out.sampc = FRAMES;
aubuf_read_auframe(ab, &af_out);
TEST_EQUALS(0, aubuf_cur_size(ab));
TEST_EQUALS(0, af_out.timestamp);

/* write one frame */
af_in.sampv = &sampv_in[FRAMES];
af_in.sampc = FRAMES;
af_in.timestamp = dt;

/* write second frame */
err |= aubuf_write_auframe(ab, &af_in);
err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(160 * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(FRAMES * sizeof(float), aubuf_cur_size(ab));

/* read half frame */
af_out.fmt = AUFMT_FLOAT;
af_out.sampv = sampv_out;
af_out.sampc = 40;

af_out.sampc = FRAMES / 2;
af_out.sampv = &sampv_out[FRAMES];
aubuf_read_auframe(ab, &af_out);

/* the first read drops old data: 80 - 40 = 40 */
TEST_EQUALS(40 * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS((FRAMES / 2) * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(dt, af_out.timestamp);

/* write another frame (which is appended now) */
af_in.timestamp += dt;
err |= aubuf_write_auframe(ab, &af_in);
TEST_EQUALS(120 * sizeof(float), aubuf_cur_size(ab));
/* write one frame */
af_in.sampv = &sampv_in[2 * FRAMES];
af_in.sampc = FRAMES;
af_in.timestamp = 2 * dt;

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS((FRAMES + (FRAMES / 2)) * sizeof(float),
aubuf_cur_size(ab));

/* write half frame */
af_in.sampv = &sampv_in[3 * FRAMES];
af_in.sampc = FRAMES / 2;
af_in.timestamp = 3 * dt;

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(2 * FRAMES * sizeof(float), aubuf_cur_size(ab));

/* read half frame */
af_out.sampv = &sampv_out[40];
af_out.sampc = 40;
af_out.sampv = &sampv_out[(FRAMES + (FRAMES / 2))];
af_out.sampc = FRAMES / 2;
aubuf_read_auframe(ab, &af_out);
TEST_EQUALS(80 * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(dt + dt / 2, af_out.timestamp);
TEST_EQUALS((FRAMES + FRAMES / 2) * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(3 * (dt / 2) + 1, af_out.timestamp);

/* read whole frame */
af_out.sampv = &sampv_out[80];
af_out.sampc = 80;
/* read one and a half frame */
af_out.sampv = &sampv_out[2 * FRAMES];
af_out.sampc = FRAMES + (FRAMES / 2);
aubuf_read_auframe(ab, &af_out);

TEST_EQUALS(2, af_out.ch);
TEST_EQUALS(48000, af_out.srate);
TEST_EQUALS(2*dt, af_out.timestamp);
TEST_EQUALS(3 * dt, af_out.timestamp);

TEST_MEMCMP(sampv_in + 80,
sizeof(sampv_in) - 80 * sizeof(float),
sampv_out, sizeof(sampv_out) - 80 * sizeof(float));
TEST_MEMCMP(sampv_in, sizeof(sampv_in), sampv_out, sizeof(sampv_out));
TEST_EQUALS(0, aubuf_cur_size(ab));

/* test automatic timestamps */
Expand Down Expand Up @@ -210,39 +236,39 @@ static int test_aubuf_sort_auframe(void)
{
int err;
struct aubuf *ab = NULL;
int16_t sampv_in[160];
int16_t sampv_out[160];
int16_t sampv_in[2 * FRAMES];
int16_t sampv_out[2 * FRAMES];
struct auframe af[3] = {
{
.fmt = AUFMT_S16LE,
.sampv = sampv_in,
.sampc = 160,
.sampc = 2 * FRAMES,
.timestamp = 1
},
{
.fmt = AUFMT_S16LE,
.sampv = sampv_in,
.sampc = 160,
.sampc = 2 * FRAMES,
.timestamp = 2
},
{
.fmt = AUFMT_S16LE,
.sampv = sampv_in,
.sampc = 160,
.sampc = 2 * FRAMES,
.timestamp = 3
},
};
struct auframe af_out = {
.fmt = AUFMT_S16LE,
.sampv = sampv_out,
.sampc = 160,
.sampc = 2 * FRAMES,
.timestamp = 0
};

err = aubuf_alloc(&ab, 3*sizeof(sampv_in), 0);
err = aubuf_alloc(&ab, 3 * sizeof(sampv_in), 0);
TEST_ERR(err);

/* Write auframes sorted */
/* Write auframes disordered */
err = aubuf_write_auframe(ab, &af[0]);
TEST_ERR(err);

Expand All @@ -252,7 +278,7 @@ static int test_aubuf_sort_auframe(void)
err = aubuf_write_auframe(ab, &af[1]);
TEST_ERR(err);

/* Check */
/* Check sorted */
aubuf_read_auframe(ab, &af_out);
TEST_EQUALS(1, af_out.timestamp);

Expand All @@ -263,14 +289,14 @@ static int test_aubuf_sort_auframe(void)
TEST_EQUALS(3, af_out.timestamp);

/* Test zero af.timestamp */
err = aubuf_write_samp(ab, sampv_in, 80);
err |= aubuf_write_samp(ab, sampv_in, 80);
err |= aubuf_write_samp(ab, sampv_in, 160);
err = aubuf_write_samp(ab, sampv_in, FRAMES);
err |= aubuf_write_samp(ab, sampv_in, FRAMES);
err |= aubuf_write_samp(ab, sampv_in, 2 * FRAMES);
TEST_ERR(err);

/* Sort - test not stuck */
aubuf_sort_auframe(ab);
TEST_EQUALS(640, aubuf_cur_size(ab));
TEST_EQUALS(8 * FRAMES, aubuf_cur_size(ab));

out:
mem_deref(ab);
Expand All @@ -281,43 +307,43 @@ static int test_aubuf_sort_auframe(void)
static int test_aubuf_resize(void)
{
struct aubuf *ab = NULL;
int16_t sampv_in[160] = {1};
int16_t sampv_out[160];
int16_t sampv_in[2 * FRAMES] = {1};
int16_t sampv_out[2 * FRAMES];
struct auframe af_out = {
.fmt = AUFMT_S16LE,
.sampv = sampv_out,
.sampc = 80,
.sampc = FRAMES,
.timestamp = 0
};
int err;

err = aubuf_alloc(&ab, 160, 160);
err = aubuf_alloc(&ab, 2 * FRAMES, 2 * FRAMES);
TEST_ERR(err);

TEST_EQUALS(0, aubuf_cur_size(ab));

err = aubuf_write_samp(ab, sampv_in, 80);
err = aubuf_write_samp(ab, sampv_in, FRAMES);
TEST_ERR(err);

err = aubuf_write_samp(ab, sampv_in, 80);
err = aubuf_write_samp(ab, sampv_in, FRAMES);
TEST_ERR(err);

TEST_EQUALS(160, aubuf_cur_size(ab));
TEST_EQUALS(2 * FRAMES, aubuf_cur_size(ab));

err = aubuf_resize(ab, 160, 320);
err = aubuf_resize(ab, 2 * FRAMES, 4 * FRAMES);
TEST_ERR(err);

TEST_EQUALS(0, aubuf_cur_size(ab));

err = aubuf_write_samp(ab, sampv_in, 80);
err = aubuf_write_samp(ab, sampv_in, FRAMES);
aubuf_read_auframe(ab, &af_out);
err |= aubuf_write_samp(ab, sampv_in, 80);
err |= aubuf_write_samp(ab, sampv_in, FRAMES);
TEST_ERR(err);

err = aubuf_write_samp(ab, sampv_in, 80);
err = aubuf_write_samp(ab, sampv_in, FRAMES);
TEST_ERR(err);

TEST_EQUALS(320, aubuf_cur_size(ab));
TEST_EQUALS(4 * FRAMES, aubuf_cur_size(ab));

out:
mem_deref(ab);
Expand Down

0 comments on commit ad8ea9b

Please sign in to comment.