map

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

commit 34e63cc1868e45e9e9b79895738ac7f6d853bd0a
parent b175835c93f64761b9a6b8867b57c195e827a56e
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Thu, 18 Jun 2020 04:46:05 +0200

add volume control

Diffstat:
MREADME | 3++-
Mmap.c | 262++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Msndio.c | 9++++++---
3 files changed, 153 insertions(+), 121 deletions(-)

diff --git a/README b/README @@ -10,7 +10,8 @@ controls j/k : cursor down/up f/b : page forward/backward -h/l : rewind/skip 30 seconds +h/l : rewind/skip 20 seconds +o/p : set the volume -/+5% c : pause/resume space: play audio file under cursor q : quit diff --git a/map.c b/map.c @@ -1,7 +1,6 @@ /* TODO - - volume change - - fix indent in play() -*/ + * - add cli args to set seek_sec and default_vol + */ #include <dirent.h> #include <err.h> @@ -11,12 +10,14 @@ #include <time.h> #include <unistd.h> -#define SEEK_SEC 30 +#define SEEK_SEC 20 +#define DEFAULT_VOL 90 struct player { char *curdir; struct track **tracks; + int volume; }; struct track { @@ -41,7 +42,8 @@ void set_cursor(int, int); char poll_key_event(void); /* sndio.c */ -int sndio_init(void); +int sndio_init(int); +int sndio_vol(int); int sndio_open(int, int, int); int sndio_play(void *, int); void sndio_close(void); @@ -118,6 +120,13 @@ print_pause(void) write_term("||"); } +static void +print_volume(int volume) +{ + set_cursor(rows - 1, 48); + write_term("Volume: %-3d", volume); +} + static int strend(const char *s, const char *t) { @@ -195,16 +204,16 @@ static long opentrack(struct track *track) { int bits, channels, rate; - long buf_size; + long bufsize; track->length = track->open(track->filename, &bits, &rate, &channels); if (track->length < 0) errx(1, "failed opening: %s", track->filename); - if ((buf_size = sndio_open(bits, rate, channels)) < 0) + if ((bufsize = sndio_open(bits, rate, channels)) < 0) errx(1, "sndio_open error"); - return buf_size; + return bufsize; } static void @@ -212,126 +221,144 @@ play(struct player player) { int trackid = 0, cursorid = 1, toptrack, timersec; int gapless = 0; - long done, buf_size, tmpbuf_len; - long *buffer, *newbuf; - time_t start, pausetime; + long done, bufsize, tmpbuf_len; + long *buf, *newbuf; + time_t start, pause_t; struct track curtrack; while (trackid < ntracks) { next: - pausetime = toptrack = timersec = 0; - start = time(NULL); - - curtrack = *player.tracks[trackid]; - buf_size = opentrack(&curtrack); - if (!gapless) - buffer = malloc(buf_size * sizeof(long)); - print_info(player.curdir, curtrack); - - do { - switch (poll_key_event()) { - /* cursor move */ - case 'j': - if (cursorid >= rows - 5 || - toptrack + cursorid + 1 > ntracks) + pause_t = toptrack = timersec = 0; + start = time(NULL); + + curtrack = *player.tracks[trackid]; + bufsize = opentrack(&curtrack); + if (!gapless) + buf = malloc(bufsize * sizeof(long)); + print_info(player.curdir, curtrack); + print_volume(player.volume); + + do { + switch (poll_key_event()) { + /* cursor move */ + case 'j': + if (cursorid >= rows - 5 || + toptrack + cursorid + 1 > ntracks) + break; + cursorid++; + print_cursor(cursorid - 1, cursorid); break; - cursorid++; - print_cursor(cursorid - 1, cursorid); - break; - case 'k': - if (cursorid <= 1) + case 'k': + if (cursorid <= 1) + break; + cursorid--; + print_cursor(cursorid + 1, cursorid); break; - cursorid--; - print_cursor(cursorid + 1, cursorid); - break; - /* page scrolling */ - case 'f': - if (toptrack + rows - 5 >= ntracks) + /* page scrolling */ + case 'f': + if (toptrack + rows - 5 >= ntracks) + break; + toptrack += rows - 5; + cursorid = 1; + print_playlist(player.tracks, toptrack); break; - toptrack += rows - 5; - cursorid = 1; - print_playlist(player.tracks, toptrack); - break; - case 'b': - if (toptrack < rows - 5) + case 'b': + if (toptrack < rows - 5) + break; + toptrack -= rows - 5; + cursorid = 1; + print_playlist(player.tracks, toptrack); break; - toptrack -= rows - 5; - cursorid = 1; - print_playlist(player.tracks, toptrack); - break; - /* select track under cursor*/ - case ' ': - trackid = toptrack + cursorid - 1; - goto endtrack; - /* resume/pause */ - case 'c': - if (pausetime) - start += difftime(time(NULL), pausetime); - pausetime = pausetime ? 0 : time(NULL); - break; - /* rewind */ - case 'h': - curtrack.seek(SEEK_SEC * -1); - if (timersec >= SEEK_SEC) - start += SEEK_SEC; - else - start += timersec; - break; - /* skip */ - case 'l': - curtrack.seek(SEEK_SEC); - start -= SEEK_SEC; - break; - /* quit */ - case 'q': - trackid = ntracks; - goto endtrack; - } - - if (pausetime) { - print_pause(); - usleep(10 * 1000); - continue; - } + /* select track under cursor*/ + case ' ': + trackid = toptrack + cursorid - 1; + goto endtrack; + /* resume/pause */ + case 'c': + if (pause_t) + start += difftime(time(NULL), pause_t); + pause_t = pause_t ? 0 : time(NULL); + break; + /* rewind */ + case 'h': + curtrack.seek(SEEK_SEC * -1); + if (timersec >= SEEK_SEC) + start += SEEK_SEC; + else + start += timersec; + break; + /* volume */ + case 'o': + player.volume += -5; + if (player.volume < 0) + player.volume = 0; + sndio_vol(player.volume); + print_volume(player.volume); + break; + case 'p': + player.volume += 5; + if (player.volume > 100) + player.volume = 100; + sndio_vol(player.volume); + print_volume(player.volume); + break; + /* skip */ + case 'l': + curtrack.seek(SEEK_SEC); + start -= SEEK_SEC; + break; + /* quit */ + case 'q': + trackid = ntracks; + goto endtrack; + } + + if (pause_t) { + print_pause(); + usleep(10 * 1000); + continue; + } - if (!gapless) { - done = curtrack.decode(buffer, buf_size); - if (trackid+1 < ntracks && done < buf_size / 2) { - tmpbuf_len = done * 2; - gapless = 1; + if (!gapless) { + done = curtrack.decode(buf, bufsize); + if (trackid+1 < ntracks && done < bufsize / 2) { + tmpbuf_len = done * 2; + gapless = 1; + curtrack.close(); + trackid++; + goto next; + } + } else { + newbuf = malloc((bufsize - tmpbuf_len) * sizeof(long)); + done = curtrack.decode(newbuf, bufsize - tmpbuf_len); + memcpy(buf + tmpbuf_len, newbuf, done * 2 * sizeof(long)); + free(newbuf); + gapless = 0; + } + + if (done < 0 || sndio_play(buf, done) < 0) { + sndio_close(); curtrack.close(); - trackid++; - goto next; + errx(1, "Error during decoding."); + } + + if(get_term_size(&rows, &cols)) { + clear_term(); + print_playlist(player.tracks, 0); + cursorid = 1; + toptrack = 0; + print_info(player.curdir, curtrack); + print_volume(player.volume); } - } else { - newbuf = malloc((buf_size - tmpbuf_len) * sizeof(long)); - done = curtrack.decode(newbuf, buf_size - tmpbuf_len); - memcpy(buffer + tmpbuf_len, newbuf, done * 2 * sizeof(long)); - free(newbuf); - gapless = 0; - } - if (done < 0 || sndio_play(buffer, done) < 0) { - sndio_close(); - curtrack.close(); - errx(1, "Error during decoding."); - } - - if(get_term_size(&rows, &cols)) { - clear_term(); - print_playlist(player.tracks, 0); - cursorid = 1; - toptrack = 0; - print_info(player.curdir, curtrack); - } - - timersec = difftime(time(NULL), start); - print_timer(timersec, curtrack.length); - } while (done); - - trackid++; + + timersec = difftime(time(NULL), start); + print_timer(timersec, curtrack.length); + } while (done); + + trackid++; endtrack: - curtrack.close(); - free(buffer); + curtrack.close(); + free(buf); } } @@ -340,6 +367,7 @@ main(int argc, char **argv) { int i; struct player player; + player.volume = DEFAULT_VOL; if (argc > 1) player.curdir = argv[1]; @@ -352,7 +380,7 @@ main(int argc, char **argv) qsort(player.tracks, ntracks, sizeof(*player.tracks), trackcmp); init_term(); - if (sndio_init() < 0) + if (sndio_init(player.volume) < 0) errx(1, "Could not initialize sndio.\n"); play(player); diff --git a/sndio.c b/sndio.c @@ -1,25 +1,28 @@ #include <stdio.h> #include <sndio.h> +#define PCT_TO_SIO(pct) ((127 * (pct) + 50) / 100) + static struct sio_hdl *hdl; static int framesize; static int sndio_started; int -sndio_vol(long vol) +sndio_vol(int vol) { - if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) + if (!sio_setvol(hdl, PCT_TO_SIO(vol))) return -1; return 0; } int -sndio_init(void) +sndio_init(int vol) { hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); if (!hdl) return -1; + sndio_vol(vol); sndio_started = 0; return 0; }