map

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

commit 3b4f8aaa93d9683e24d4d8f93aad32a4058e3946
parent bf51284ca32f95c354e29c12330a1e76224d539d
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Wed, 28 Aug 2019 01:29:17 +0200

optimize and simplify app start

Diffstat:
Mflac.c | 15+--------------
Mmap.c | 59++++++++++++++++++-----------------------------------------
Mmp3.c | 15+--------------
Mvorbis.c | 15+--------------
Mwav.c | 15+--------------
5 files changed, 22 insertions(+), 97 deletions(-)

diff --git a/flac.c b/flac.c @@ -13,20 +13,7 @@ flac_open(const char *filename, int *bits, int *rate, int *channels) *rate = flac->sampleRate; *channels = flac->channels; - return 0; -} - -int -flac_length(const char *filename) -{ - int length; - - if (!(flac = drflac_open_file(filename))) - return -1; - length = flac->totalPCMFrameCount / flac->sampleRate; - drflac_close(flac); - - return length; + return flac->totalPCMFrameCount / flac->sampleRate; } long diff --git a/map.c b/map.c @@ -21,7 +21,7 @@ struct player { char *curdir; long volume; - char **tracklist; + struct track **tracks; }; struct track { @@ -53,28 +53,24 @@ void alsa_close(void); /* flac.c */ int flac_open(const char *, int *, int *, int *); -int flac_length(const char *); long flac_decode(void *, int); void flac_seek(int); void flac_close(void); /* mp3.c */ int mp3_open(const char *, int *, int *, int *); -int mp3_length(const char *); long mp3_decode(void *, int); void mp3_seek(int); void mp3_close(void); /* vorbis.c */ int vorbis_open(const char *, int *, int *, int *); -int vorbis_length(const char *); long vorbis_decode(void *, int); void vorbis_seek(int); void vorbis_close(void); /* wav.c */ int wav_open(const char *, int *, int *, int *); -int wav_length(const char *); long wav_decode(void *, int); void wav_seek(int); void wav_close(void); @@ -95,7 +91,7 @@ display_cursor(int oldx, int newx) } static void -display_tracks(char **lines, int nb_lines, int start_index) +print_playlist(struct track **tracks, int start_index) { size_t buf_len = 0, line_len; char *buf = NULL; @@ -104,14 +100,13 @@ display_tracks(char **lines, int nb_lines, int start_index) write_term("\x1b[1J\x1b[H"); for (int i = start_index; i < start_index + rows - 5; i++) { - if (i == nb_lines) + if (i == ntracks) break; - line_len = strlen(lines[i]); + line_len = strlen(tracks[i]->name) + 8; if (line_len > (unsigned long) cols) line_len = cols; - buf = realloc(buf, buf_len + line_len + 1); - strncpy(&buf[buf_len], lines[i], line_len); - buf[buf_len+line_len] = '\0'; + buf = realloc(buf, buf_len + line_len); + sprintf(&buf[buf_len], " %s\n", tracks[i]->name); buf_len += line_len; } @@ -183,28 +178,24 @@ init_track(const char *curdir, const char *d_name) temp->decode = wav_decode; temp->seek = wav_seek; temp->close = wav_close; - temp->length = wav_length(temp->filename); temp->format = "WAV"; } else if (strend(d_name, ".mp3")) { temp->open = mp3_open; temp->decode = mp3_decode; temp->seek = mp3_seek; temp->close = mp3_close; - temp->length = mp3_length(temp->filename); temp->format = "MP3"; } else if (strend(d_name, ".flac")) { temp->open = flac_open; temp->decode = flac_decode; temp->seek = flac_seek; temp->close = flac_close; - temp->length = flac_length(temp->filename); temp->format = "FLAC"; } else if (strend(d_name, ".ogg")) { temp->open = vorbis_open; temp->decode = vorbis_decode; temp->seek = vorbis_seek; temp->close = vorbis_close; - temp->length = vorbis_length(temp->filename); temp->format = "Vorbis"; } @@ -249,25 +240,13 @@ trackcmp(const void *a, const void *b) } static void -makeplaylist(struct track **tracks, char **tracklist) -{ - for (int i = 0; i < ntracks; i++) { - tracklist[i] = malloc(strlen(tracks[i]->name) + 30); - if (!tracklist[i]) - errx(1, "malloc failure"); - - sprintf(tracklist[i], "%8d:%02d %s\n", tracks[i]->length / 60, - tracks[i]->length % 60, tracks[i]->name); - } -} - -static void -opentrack(struct track track) +opentrack(struct track *track) { int bits, channels, rate; - if (track.open(track.filename, &bits, &rate, &channels) < 0) - errx(1, "failed opening: %s", track.filename); + track->length = track->open(track->filename, &bits, &rate, &channels); + if (track->length < 0) + errx(1, "failed opening: %s", track->filename); if (alsa_open(bits, rate, channels) < 0) errx(1, "alsa_open error"); @@ -283,14 +262,14 @@ playtrack(struct track curtrack, struct player player, int trackid) int buffer[BUFF_SIZE]; start = time(NULL); - opentrack(curtrack); + opentrack(&curtrack); display_track_info(player.curdir, curtrack); display_volume(player.volume); do { if(get_term_size(&rows, &cols)) { clear_term(); - display_tracks(player.tracklist, ntracks, 0); + print_playlist(player.tracks, 0); cursorid = 1; toptrack = 0; display_track_info(player.curdir, curtrack); @@ -318,14 +297,14 @@ playtrack(struct track curtrack, struct player player, int trackid) break; toptrack += rows - 5; cursorid = 1; - display_tracks(player.tracklist, ntracks, toptrack); + print_playlist(player.tracks, toptrack); break; case 'b': if (toptrack < rows - 5) break; toptrack -= rows - 5; cursorid = 1; - display_tracks(player.tracklist, ntracks, toptrack); + print_playlist(player.tracks, toptrack); break; /* volume */ case 'o': @@ -402,18 +381,16 @@ main(int argc, char **argv) else player.curdir = "./"; - struct track **tracks = get_tracks(player.curdir); - if (ntracks == 0 || tracks == NULL) + player.tracks = get_tracks(player.curdir); + if (ntracks == 0 || player.tracks == NULL) errx(1, "No audio files found in %s", player.curdir); - qsort(tracks, ntracks, sizeof(*tracks), trackcmp); - player.tracklist = malloc(ntracks * sizeof(char *)); - makeplaylist(tracks, player.tracklist); + qsort(player.tracks, ntracks, sizeof(*player.tracks), trackcmp); player.volume = alsa_vol(player.volume, 0); init_term(); while (trackid < ntracks) - trackid = playtrack(*tracks[trackid], player, trackid); + trackid = playtrack(*player.tracks[trackid], player, trackid); end_term(); diff --git a/mp3.c b/mp3.c @@ -13,20 +13,7 @@ mp3_open(const char *filename, int *bits, int *rate, int *channels) *rate = mp3.sampleRate; *channels = mp3.channels; - return 0; -} - -int -mp3_length(const char *filename) -{ - int length; - - if (!drmp3_init_file(&mp3, filename, NULL)) - return -1; - length = drmp3_get_pcm_frame_count(&mp3) / mp3.sampleRate; - drmp3_uninit(&mp3); - - return length; + return drmp3_get_pcm_frame_count(&mp3) / mp3.sampleRate; } long diff --git a/vorbis.c b/vorbis.c @@ -13,20 +13,7 @@ vorbis_open(const char *filename, int *bits, int *rate, int *channels) *rate = vorbis->sample_rate; *channels = vorbis->channels; - return 0; -} - -int -vorbis_length(const char *filename) -{ - int length; - - if (!(vorbis = stb_vorbis_open_filename(filename, NULL, NULL))) - return -1; - length = stb_vorbis_stream_length_in_seconds(vorbis); - stb_vorbis_close(vorbis); - - return length; + return stb_vorbis_stream_length_in_seconds(vorbis); } long diff --git a/wav.c b/wav.c @@ -13,20 +13,7 @@ wav_open(const char *filename, int *bits, int *rate, int *channels) *rate = wav.sampleRate; *channels = wav.channels; - return 0; -} - -int -wav_length(const char *filename) -{ - int length; - - if (!drwav_init_file(&wav, filename)) - return -1; - length = wav.totalPCMFrameCount / wav.sampleRate; - drwav_uninit(&wav); - - return length; + return wav.totalPCMFrameCount / wav.sampleRate; } long