From f33ca0614159b628a5c1d886d5bd16d4b9b2c562 Mon Sep 17 00:00:00 2001 From: Lars Immisch Date: Wed, 6 Sep 2023 16:04:42 +0200 Subject: [PATCH] Take wav header into account, thanks @cspiel1 Also observe max column length of 80. --- rem/aufile/aufile.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/rem/aufile/aufile.c b/rem/aufile/aufile.c index 42bc7f304..6e1178fc5 100644 --- a/rem/aufile/aufile.c +++ b/rem/aufile/aufile.c @@ -287,9 +287,34 @@ size_t aufile_get_length(struct aufile *af, struct aufile_prm *prm) * * @return position in bytes or (size_t)-1 in case of an error. */ -size_t aufile_set_position(struct aufile *af, struct aufile_prm *prm, size_t pos_ms) +size_t aufile_set_position(struct aufile *af, struct aufile_prm *prm, + size_t pos_ms) { - size_t pos = prm->srate * aufmt_sample_size(prm->fmt) * prm->channels * pos_ms / 1000; + struct wav_fmt fmt; + int err; + size_t pos; + + if (!af || !prm) { + return EINVAL; + } + + // Seek to the beginning of the file + if (fseek(af->f, 0, SEEK_SET) < 0) { + return errno; + } + // this is only used for the side effect of moving the file ptr to the + // first data block. + err = wav_header_decode(&fmt, &pos, af->f); + if (err) { + return err; + } - return fseek(af->f, pos, SEEK_SET); + pos = prm->srate * aufmt_sample_size(prm->fmt) + * prm->channels * pos_ms / 1000; + + if (fseek(af->f, pos, SEEK_CUR) < 0) { + return errno; + } + + return 0; }