Skip to content

Commit

Permalink
Implement aufile_set_position (#943)
Browse files Browse the repository at this point in the history
* Implement aufile_set_position

* Take wav header into account, thanks @cspiel1

Also observe max column length of 80.

* Fix more linter warnings.

* Fix compilation errors on Windows (fingers crossed)

* One more Windows error fixed

* Fix doxygen comment

* Remove obvious comment

* int as return type for aufile_set_position (doh)

* Do review comments from @alfredh

* Use size_t in aufile_set_position
  • Loading branch information
larsimmisch authored Sep 17, 2023
1 parent 4c9d851 commit e60207a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/rem_aufile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
34 changes: 34 additions & 0 deletions rem/aufile/aufile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e60207a

Please sign in to comment.