From 8c626be5a4513611109c5c772cb9b5ec175c40e8 Mon Sep 17 00:00:00 2001 From: Lars Immisch Date: Mon, 20 Nov 2023 17:04:12 +0100 Subject: [PATCH] rem/aufile: fix aufile_get_length calculations (#1008) --- include/rem_aufile.h | 2 +- rem/aufile/aufile.c | 20 ++++++++++---------- test/CMakeLists.txt | 1 + test/aulength.c | 36 ++++++++++++++++++++++++++++++++++++ test/data/beep.wav | Bin 0 -> 6156 bytes test/test.c | 1 + test/test.h | 1 + 7 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 test/aulength.c create mode 100644 test/data/beep.wav 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 0000000000000000000000000000000000000000..a148a48aef8380792d9e36af36f0e50bace99212 GIT binary patch literal 6156 zcmeHL4Nw&K6<_d_4~ZZmU=VLP6gXf%?mRedfkWg&sUA8?#kQtqZ*LDzxDW3Z4lqe= z>PM)p9n+3Xrj4nYq{$>{F||pIrq;%Y&7i;@A0nSY6p^5aLI_CN?%t;-($Z+BnGAcw z?4P&q{oZ@O_x^AHvzv0Vvw5Kake|LKORc5A0000y*xxEX0PK+ifF}?DsBj9$cmQyX z;R78^6wt&Vz%PNScwyhX38{bM%Uu<# z1z+c!!O+~OH$FJP+n;lwHky{@MtU6ilMno=w5e*($ek_oTbj-!DLx6x`9neYQ1;-d z{x8b%+H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