Skip to content

Commit

Permalink
rem/aufile: fix aufile_get_length calculations (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsimmisch authored Nov 20, 2023
1 parent 80ea918 commit 8c626be
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/rem_aufile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
20 changes: 10 additions & 10 deletions rem/aufile/aufile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ set(SRCS
aes.c
async.c
aubuf.c
aulength.c
aulevel.c
auresamp.c
av1.c
Expand Down
36 changes: 36 additions & 0 deletions test/aulength.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file src/aulength.c audio file duration test
*
* Copyright (C) 2023 Lars Immisch
*/

#include <re.h>
#include <rem.h>
#include "test.h"


#define DEBUG_MODULE "aulength"
#define DEBUG_LEVEL 5
#include <re_dbg.h>


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;
}
Binary file added test/data/beep.wav
Binary file not shown.
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8c626be

Please sign in to comment.