map

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

commit 84ee925d69736cf7bd3e97177b823b94c8a32331
parent 9ecaf8e2d2dee80bd93f45a935f97c33f4ad8906
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Mon, 18 May 2020 07:52:16 +0200

switch to sndio backend

Diffstat:
MMakefile | 4++--
MREADME | 3+--
Dalsa.c | 89-------------------------------------------------------------------------------
Mmap.c | 51+++++++++++----------------------------------------
Asndio.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 69 insertions(+), 133 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ .POSIX: PREFIX = /usr/local CFLAGS = -O2 -std=c99 -D_DEFAULT_SOURCE -Wall -LDLIBS = -lasound -OBJ = alsa.o flac.o map.o mp3.o term.o +LDLIBS = -lsndio +OBJ = flac.o map.o mp3.o sndio.o term.o BIN = map $(BIN): $(OBJ) diff --git a/README b/README @@ -1,7 +1,7 @@ map - minimalist audio player ============================= -Linux only, using the ALSA backend. +Requires sndio backend. Supports FLAC and MP3. Play audio files in a directory. @@ -11,7 +11,6 @@ controls j/k : cursor down/up f/b : page forward/backward h/l : rewind/skip 30 seconds -o/p : reduce/increase volume c : pause/resume space: play audio file under cursor q : quit diff --git a/alsa.c b/alsa.c @@ -1,89 +0,0 @@ -#include <alsa/asoundlib.h> - -static snd_pcm_t *hdl; - -int -alsa_vol(long volume, int volchange) -{ - long min, max; - snd_mixer_selem_id_t *sid; - snd_mixer_t *mixerhdl; - snd_mixer_elem_t *elem; - - if (snd_mixer_open(&mixerhdl, 0) < 0) - return -1; - - if (snd_mixer_attach(mixerhdl, "default") < 0) - goto err0; - - if (snd_mixer_selem_register(mixerhdl, NULL, NULL) < 0) - goto err0; - - if (snd_mixer_load(mixerhdl) < 0) - goto err0; - - snd_mixer_selem_id_alloca(&sid); - snd_mixer_selem_id_set_name(sid, "Master"); - if (!(elem = snd_mixer_find_selem(mixerhdl, sid))) - goto err0; - - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - snd_mixer_selem_get_playback_volume(elem, 0, &volume); - if (volume + volchange <= max && volume + volchange >= min) - volume += volchange; - snd_mixer_selem_set_playback_volume_all(elem, volume); - snd_mixer_close(mixerhdl); - - return volume * 100 / max; -err0: - snd_mixer_close(mixerhdl); - return -1; -} - -int -alsa_open(int bits, int rate, int channels) -{ - snd_pcm_format_t format; - - switch (bits) { - case 16: - format = SND_PCM_FORMAT_S16; - break; - case 32: - format = SND_PCM_FORMAT_S32; - break; - default: - return -1; - } - - if (snd_pcm_open(&hdl, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) - return -1; - - if (snd_pcm_set_params(hdl, format, SND_PCM_ACCESS_RW_INTERLEAVED, - channels, rate, 1, 500000) < 0) { - snd_pcm_close(hdl); - hdl = NULL; - return -1; - } - - return 0; -} - -int -alsa_play(void *buf, size_t nbytes) -{ - snd_pcm_sframes_t frames; - - frames = snd_pcm_writei(hdl, buf, nbytes); - if (frames < 0) - frames = snd_pcm_recover(hdl, frames, 1); - return frames; -} - -void -alsa_close(void) -{ - if (hdl) - snd_pcm_close(hdl); - hdl = NULL; -} diff --git a/map.c b/map.c @@ -1,10 +1,3 @@ -/* - * This is a project I use to learn C, don't expect good programming practices - * - * TODO - * - add sndio backend - */ - #include <dirent.h> #include <err.h> #include <stdio.h> @@ -20,7 +13,6 @@ struct player { char *curdir; - int volume; struct track **tracks; }; @@ -45,11 +37,10 @@ void clear_term(void); void set_cursor(int, int); char poll_key_event(void); -/* alsa.c */ -int alsa_vol(long, int); -int alsa_open(int, int, int); -int alsa_play(void *, int); -void alsa_close(void); +/* sndio.c */ +int sndio_open(int, int, int); +int sndio_play(void *, int); +void sndio_close(void); /* flac.c */ int flac_open(const char *, int *, int *, int *); @@ -121,13 +112,6 @@ 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) { @@ -208,12 +192,12 @@ opentrack(struct track *track) if (track->length < 0) errx(1, "failed opening: %s", track->filename); - if (alsa_open(bits, rate, channels) < 0) - errx(1, "alsa_open error"); + if (sndio_open(bits, rate, channels) < 0) + errx(1, "sndio_open error"); } static int -playtrack(struct track curtrack, struct player player, int *volume, int trackid) +playtrack(struct track curtrack, struct player player, int trackid) { static int cursorid = 1, toptrack = 0; int timersec = 0; @@ -224,7 +208,6 @@ playtrack(struct track curtrack, struct player player, int *volume, int trackid) start = time(NULL); opentrack(&curtrack); print_info(player.curdir, curtrack); - print_volume(*volume); do { if(get_term_size(&rows, &cols)) { @@ -233,7 +216,6 @@ playtrack(struct track curtrack, struct player player, int *volume, int trackid) cursorid = 1; toptrack = 0; print_info(player.curdir, curtrack); - print_volume(*volume); } switch (poll_key_event()) { @@ -266,15 +248,6 @@ playtrack(struct track curtrack, struct player player, int *volume, int trackid) cursorid = 1; print_playlist(player.tracks, toptrack); break; - /* volume */ - case 'o': - *volume = alsa_vol(*volume, -2); - print_volume(*volume); - break; - case 'p': - *volume = alsa_vol(*volume, 2); - print_volume(*volume); - break; /* select track under cursor*/ case ' ': curtrack.close(); @@ -313,8 +286,8 @@ playtrack(struct track curtrack, struct player player, int *volume, int trackid) } done = curtrack.decode(buffer, BUFF_SIZE); - if (done < 0 || alsa_play(buffer, done) < 0) { - alsa_close(); + if (done < 0 || sndio_play(buffer, done) < 0) { + sndio_close(); curtrack.close(); errx(1, "Error during decoding."); } @@ -326,7 +299,7 @@ playtrack(struct track curtrack, struct player player, int *volume, int trackid) curtrack.close(); trackid++; endtrack: - alsa_close(); + sndio_close(); return trackid; } @@ -346,12 +319,10 @@ main(int argc, char **argv) errx(1, "No audio files found in %s", player.curdir); qsort(player.tracks, ntracks, sizeof(*player.tracks), trackcmp); - player.volume = alsa_vol(player.volume, 0); init_term(); while (trackid < ntracks) - trackid = playtrack(*player.tracks[trackid], player, - &player.volume, trackid); + trackid = playtrack(*player.tracks[trackid], player, trackid); end_term(); diff --git a/sndio.c b/sndio.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <sndio.h> + +static struct sio_hdl *hdl; +static int framesize; + +int +sndio_vol(long vol) +{ + if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) + return -1; + return 0; +} + +int +sndio_open(int bits, int rate, int channels) +{ + struct sio_par par; + + hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); + if (!hdl) + return -1; + + sio_initpar(&par); + par.bits = bits; + par.rate = rate; + par.pchan = channels; + framesize = (bits + 7) / 8 * channels; + + if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) + goto err0; + + if (!sio_start(hdl)) + goto err0; + + return 0; +err0: + sio_close(hdl); + hdl = NULL; + return -1; +} + +int +sndio_play(void *buf, size_t nbytes) +{ + return sio_write(hdl, buf, nbytes * framesize); +} + +void +sndio_close(void) +{ + if (hdl) + sio_close(hdl); + hdl = NULL; +}