diff --git a/include/rem_aufile.h b/include/rem_aufile.h index 98a3628fd..a9a84253f 100644 --- a/include/rem_aufile.h +++ b/include/rem_aufile.h @@ -26,3 +26,5 @@ 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); +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 8c9587b44..dfa20f796 100644 --- a/rem/aufile/aufile.c +++ b/rem/aufile/aufile.c @@ -278,3 +278,37 @@ size_t aufile_get_length(struct aufile *af, struct aufile_prm *prm) return 0; } + +/** + * Set initial playing position of a WAV file in ms + * + * @param af Audio-file + * @param prm Audio file parameters from aufile_open + * + * @return 0 if success, otherwise errorcode + */ +int aufile_set_position(struct aufile *af, const struct aufile_prm *prm, + size_t pos_ms) +{ + if (!af || !prm) + return EINVAL; + + 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. */ + struct wav_fmt fmt; + size_t datasize; + int err = wav_header_decode(&fmt, &datasize, af->f); + if (err) + return err; + + off_t pos = (off_t)(prm->srate * aufmt_sample_size(prm->fmt) + * prm->channels * pos_ms / 1000); + + if (fseek(af->f, pos, SEEK_CUR) < 0) + return errno; + + return 0; +}