diff --git a/include/rem_aufile.h b/include/rem_aufile.h index a9a84253f..b76bb0f86 100644 --- a/include/rem_aufile.h +++ b/include/rem_aufile.h @@ -25,6 +25,6 @@ int aufile_open(struct aufile **afp, struct aufile_prm *prm, int aufile_read(struct aufile *af, uint8_t *p, size_t *sz); int aufile_write(struct aufile *af, const uint8_t *p, size_t sz); size_t aufile_get_size(struct aufile *af); -size_t aufile_get_length(struct aufile *af, struct aufile_prm *prm); +size_t aufile_get_length(struct aufile *af, const struct aufile_prm *prm); int aufile_set_position(struct aufile *af, const struct aufile_prm *prm, size_t pos_ms); diff --git a/rem/aufile/aufile.c b/rem/aufile/aufile.c index dfa20f796..13f2cd26b 100644 --- a/rem/aufile/aufile.c +++ b/rem/aufile/aufile.c @@ -252,26 +252,26 @@ size_t aufile_get_size(struct aufile *af) * * @return length in ms if success, otherwise 0. */ -size_t aufile_get_length(struct aufile *af, struct aufile_prm *prm) +size_t aufile_get_length(struct aufile *af, const struct aufile_prm *prm) { - if (!af) + if (!af || !prm) return 0; switch (prm->fmt) { case AUFMT_PCMA: case AUFMT_PCMU: - return af->datasize * prm->channels * prm->srate - / 1000; + return af->datasize * 1000 / + (prm->channels * prm->srate); case AUFMT_S16LE: - return af->datasize * 2 * prm->channels * prm->srate - / 1000; + return af->datasize * 1000 / + (prm->channels * prm->srate * 2); case AUFMT_S24_3LE: - return af->datasize * 3 * prm->channels * prm->srate - / 1000; + return af->datasize * 1000 / + (prm->channels * prm->srate * 3); case AUFMT_S32LE: case AUFMT_FLOAT: - return af->datasize * 4 * prm->channels * prm->srate - / 1000; + return af->datasize * 1000 / + (prm->channels * prm->srate * 4); default: return 0; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c61cfbb96..9412ac2f5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ set(SRCS aes.c async.c aubuf.c + aulength.c aulevel.c auresamp.c av1.c diff --git a/test/aulength.c b/test/aulength.c new file mode 100644 index 000000000..2b4f8f5b2 --- /dev/null +++ b/test/aulength.c @@ -0,0 +1,36 @@ +/** + * @file src/aulength.c audio file duration test + * + * Copyright (C) 2023 Lars Immisch + */ + +#include +#include +#include "test.h" + + +#define DEBUG_MODULE "aulength" +#define DEBUG_LEVEL 5 +#include + + +int test_aulength(void) +{ + struct aufile *af = NULL; + struct aufile_prm prm; + char path[256]; + + re_snprintf(path, sizeof(path), "%s/beep.wav", test_datapath()); + + int err = aufile_open(&af, &prm, path, AUFILE_READ); + if (err) + TEST_ERR(err); + + size_t length = aufile_get_length(af, &prm); + TEST_EQUALS(67, length); + +out: + mem_deref(af); + + return err; +} diff --git a/test/data/beep.wav b/test/data/beep.wav new file mode 100644 index 000000000..a148a48ae Binary files /dev/null and b/test/data/beep.wav differ diff --git a/test/test.c b/test/test.c index 039dd6894..ca71e99a7 100644 --- a/test/test.c +++ b/test/test.c @@ -53,6 +53,7 @@ static const struct test tests[] = { TEST(test_aes), TEST(test_aes_gcm), TEST(test_aubuf), + TEST(test_aulength), TEST(test_aulevel), TEST(test_auresamp), TEST(test_async), diff --git a/test/test.h b/test/test.h index ee386b5e9..306d9bf47 100644 --- a/test/test.h +++ b/test/test.h @@ -159,6 +159,7 @@ int test_aes(void); int test_aes_gcm(void); int test_aubuf(void); int test_aulevel(void); +int test_aulength(void); int test_auresamp(void); int test_async(void); int test_av1(void);