Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement aufile_set_position #943

Merged
merged 10 commits into from
Sep 17, 2023
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);
size_t aufile_set_position(struct aufile *af, struct aufile_prm *prm,
off_t pos_ms);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is probably better to use size_t instead of off_t here ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that first, but it's fseek(FILE *, long, int); and using size_t breaks the Windows build.

40 changes: 40 additions & 0 deletions rem/aufile/aufile.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,43 @@ 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
*/
size_t aufile_set_position(struct aufile *af, struct aufile_prm *prm,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • return int as error code
  • correct @return doxygen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int instead of size_t

off_t pos_ms)
{
struct wav_fmt fmt;
int err;
off_t pos;
size_t datasize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use C99 style declare+assign if possible ...


if (!af || !prm) {
return EINVAL;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove curly braces that is not needed here ... same for the rest of the function


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, &datasize, af->f);
if (err) {
return err;
}

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;
}