map

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

commit da5c8dd896263b42f68f5a62bc21a2b91794bb78
parent 156bc4fec48798520ca132471ff1a9a4537f1853
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Tue,  4 Aug 2020 13:08:03 +0200

can now open multiple folders and/or files

Diffstat:
MREADME | 1-
Mmap.c | 147++++++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 78 insertions(+), 70 deletions(-)

diff --git a/README b/README @@ -3,7 +3,6 @@ map - minimalist audio player Requires sndio backend. Supports FLAC and MP3. -Play audio files in a directory. controls -------- diff --git a/map.c b/map.c @@ -1,5 +1,7 @@ /* TODO * - add cli args to set seek_sec and default_vol + * - fix samples playing when quitting while paused + * - display parent folder name */ #include <dirent.h> @@ -9,17 +11,12 @@ #include <string.h> #include <time.h> #include <unistd.h> +#include <sys/stat.h> #define SEEK_SEC 20 -#define DEFAULT_VOL 80 +#define DEFAULT_VOL 75 -struct player { - char *curdir; - struct track **tracks; - int volume; -}; - struct track { char *filename; char *name; @@ -64,6 +61,8 @@ void mp3_close(void); static int rows; static int cols; static int ntracks; +struct track **tracks; +static int volume; static void @@ -93,11 +92,8 @@ print_playlist(struct track **tracks, int start_index) } static void -print_info(const char *curdir, struct track track) +print_info(struct track track) { - set_cursor(rows - 3, 1); - write_term(" Directory: %s\x1b[K", curdir); - set_cursor(rows - 2, 1); write_term(" Playing: %s\x1b[K", track.name); @@ -134,27 +130,26 @@ strend(const char *s, const char *t) return nt <= ns && strcmp(s + ns - nt, t) == 0; } + static struct track * -init_track(const char *curdir, const char *d_name) +init_track(const char *path, const char *filename) { size_t extension; struct track *tmp = malloc(sizeof(struct track)); - tmp->filename = malloc(strlen(curdir) + strlen(d_name) + 2); + tmp->filename = malloc(strlen(path) + strlen(filename) + 2); if (!tmp->filename) errx(1, "malloc failure"); - strcpy(tmp->filename, curdir); - if (curdir[strlen(curdir) - 1] != '/') - strcat(tmp->filename, "/"); - strcat(tmp->filename, d_name); + strcpy(tmp->filename, path); + strcat(tmp->filename, filename); - if (strend(d_name, ".mp3")) { + if (strend(tmp->filename, ".mp3")) { tmp->open = mp3_open; tmp->decode = mp3_decode; tmp->seek = mp3_seek; tmp->close = mp3_close; tmp->format = "MP3"; - } else if (strend(d_name, ".flac")) { + } else if (strend(tmp->filename, ".flac")) { tmp->open = flac_open; tmp->decode = flac_decode; tmp->seek = flac_seek; @@ -172,26 +167,45 @@ init_track(const char *curdir, const char *d_name) return tmp; } -static struct track ** -get_tracks(const char *curdir) +static void +get_tracks(const char *path) { - struct track **tracks = NULL; - struct dirent *dir; - DIR *d; - - if (!(d = opendir(curdir))) - return NULL; - while ((dir = readdir(d))) { - if (!strend(dir->d_name, ".flac") && - !strend(dir->d_name, ".mp3")) - continue; + struct stat path_stat; + stat(path, &path_stat); + + if (S_ISREG(path_stat.st_mode)) { tracks = realloc(tracks, sizeof(struct track *) * (ntracks+1)); - tracks[ntracks++] = init_track(curdir, dir->d_name); + tracks[ntracks++] = init_track(path, ""); + } else if (S_ISDIR(path_stat.st_mode)) { + struct dirent *dir; + DIR *d; + if (!(d = opendir(path))) { + printf("Could not open directory: %s\n", path); + return; + } + while ((dir = readdir(d))) { + if (!strend(dir->d_name, ".flac") && + !strend(dir->d_name, ".mp3")) + continue; + tracks = realloc(tracks, sizeof(struct track *) * (ntracks+1)); + tracks[ntracks++] = init_track(path, dir->d_name); + } + closedir(d); } - closedir(d); - return tracks; } + +static void +check_args(int argc, char **argv) +{ + int i; + + for (i = 1; i <= argc - 1; i++) { + get_tracks(argv[i]); + } +} + + static int trackcmp(const void *a, const void *b) { @@ -217,7 +231,7 @@ opentrack(struct track *track) } static void -play(struct player player) +play(void) { int trackid = 0, cursorid = 1, toptrack, timersec; int gapless = 0; @@ -231,12 +245,12 @@ next: pause_t = toptrack = timersec = 0; start = time(NULL); - curtrack = *player.tracks[trackid]; + curtrack = *tracks[trackid]; bufsize = opentrack(&curtrack); if (!gapless) buf = malloc(bufsize * sizeof(long)); - print_info(player.curdir, curtrack); - print_volume(player.volume); + print_info(curtrack); + print_volume(volume); do { switch (poll_key_event()) { @@ -260,14 +274,14 @@ next: break; toptrack += rows - 5; cursorid = 1; - print_playlist(player.tracks, toptrack); + print_playlist(tracks, toptrack); break; case 'b': if (toptrack < rows - 5) break; toptrack -= rows - 5; cursorid = 1; - print_playlist(player.tracks, toptrack); + print_playlist(tracks, toptrack); break; /* select track under cursor*/ case ' ': @@ -289,18 +303,18 @@ next: break; /* volume */ case 'o': - player.volume += -5; - if (player.volume < 0) - player.volume = 0; - sndio_vol(player.volume); - print_volume(player.volume); + volume += -5; + if (volume < 0) + volume = 0; + sndio_vol(volume); + print_volume(volume); break; case 'p': - player.volume += 5; - if (player.volume > 100) - player.volume = 100; - sndio_vol(player.volume); - print_volume(player.volume); + volume += 5; + if (volume > 100) + volume = 100; + sndio_vol(volume); + print_volume(volume); break; /* skip */ case 'l': @@ -315,11 +329,11 @@ next: if(get_term_size(&rows, &cols)) { clear_term(); - print_playlist(player.tracks, 0); + print_playlist(tracks, 0); cursorid = 1; toptrack = 0; - print_info(player.curdir, curtrack); - print_volume(player.volume); + print_info(curtrack); + print_volume(volume); } if (pause_t) { @@ -366,33 +380,28 @@ int main(int argc, char **argv) { int i; - struct player player; - player.volume = DEFAULT_VOL; + volume = DEFAULT_VOL; - if (argc > 1) - player.curdir = argv[1]; - else - player.curdir = "./"; + check_args(argc, argv); - player.tracks = get_tracks(player.curdir); - if (ntracks == 0 || player.tracks == NULL) - errx(1, "No audio files found in %s", player.curdir); + if (ntracks == 0 || tracks == NULL) + errx(1, "No audio files found.\n"); - qsort(player.tracks, ntracks, sizeof(*player.tracks), trackcmp); + qsort(tracks, ntracks, sizeof(*tracks), trackcmp); init_term(); - if (sndio_init(player.volume) < 0) + if (sndio_init(volume) < 0) errx(1, "Could not initialize sndio.\n"); - play(player); + play(); sndio_close(); end_term(); for (i = 0; i < ntracks; i++) { - free(player.tracks[i]->filename); - free(player.tracks[i]->name); - free(player.tracks[i]); + free(tracks[i]->filename); + free(tracks[i]->name); + free(tracks[i]); } - free(player.tracks); + free(tracks); return 0; }