map

minimalist audio player
git clone https://git.porkepik.fr/map
Log | Files | Refs | README | LICENSE

commit 5366291d5ef79b8797175100ec1483f2670fc87a
parent 36ce73d3c47edd8684bcd9d078fec4481a8523a1
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Mon, 18 May 2020 11:56:16 +0200

fix buffer size

Diffstat:
Mmap.c | 19+++++++++++--------
Msndio.c | 11++++++++---
2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/map.c b/map.c @@ -10,8 +10,6 @@ #include <time.h> #include <unistd.h> - -#define BUFF_SIZE 2048 #define SEEK_SEC 30 @@ -189,17 +187,20 @@ trackcmp(const void *a, const void *b) return strcmp(arg1->filename, arg2->filename); } -static void +static long opentrack(struct track *track) { int bits, channels, rate; + long buf_size; track->length = track->open(track->filename, &bits, &rate, &channels); if (track->length < 0) errx(1, "failed opening: %s", track->filename); - if (sndio_open(bits, rate, channels) < 0) + if ((buf_size = sndio_open(bits, rate, channels)) < 0) errx(1, "sndio_open error"); + + return buf_size; } static int @@ -207,13 +208,14 @@ playtrack(struct track curtrack, struct player player, int trackid) { static int cursorid = 1, toptrack = 0; int timersec = 0; - long done; + long done, buf_size; + long *buffer; time_t start, pausetime = 0; - long buffer[BUFF_SIZE]; start = time(NULL); - opentrack(&curtrack); + buf_size = opentrack(&curtrack); print_info(player.curdir, curtrack); + buffer = malloc(buf_size * sizeof(long)); do { if(get_term_size(&rows, &cols)) { @@ -291,7 +293,7 @@ playtrack(struct track curtrack, struct player player, int trackid) continue; } - done = curtrack.decode(buffer, BUFF_SIZE); + done = curtrack.decode(buffer, buf_size); if (done < 0 || sndio_play(buffer, done) < 0) { sndio_close(); curtrack.close(); @@ -306,6 +308,7 @@ playtrack(struct track curtrack, struct player player, int trackid) trackid++; endtrack: sndio_stop(); + free(buffer); return trackid; } diff --git a/sndio.c b/sndio.c @@ -25,20 +25,25 @@ int sndio_open(int bits, int rate, int channels) { struct sio_par par; + int buf_size; sio_initpar(&par); par.bits = bits; par.rate = rate; par.pchan = channels; + par.le = SIO_LE_NATIVE; framesize = (bits + 7) / 8 * channels; if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) goto err0; + buf_size = (0.1 * par.rate) + par.round - 1; + buf_size -= buf_size % par.round; + if (!sio_start(hdl)) goto err0; - return 0; + return buf_size; err0: sio_close(hdl); hdl = NULL; @@ -46,9 +51,9 @@ err0: } int -sndio_play(void *buf, size_t nbytes) +sndio_play(void *buf, size_t nframes) { - return sio_write(hdl, buf, nbytes * framesize); + return sio_write(hdl, buf, nframes * framesize); } void