diff --git a/README.md b/README.md index 90b2e20..f3c3c08 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ ## Requirements +No external dependencies other than GCC (or another C99 compatible compiler). + +**Optional**: [libmpdclient](https://github.com/MusicPlayerDaemon/libmpdclient) to display current status (song / elapsed time / etc) + The fifo output of MPD needs to be enabled. To do so, simply place this in your mpd.conf: ``` @@ -15,17 +19,21 @@ format "44100:16:1" } ``` -This is allowing MPD to create a file (the location is hardcoded right now so it MUST be /tmp/mpd.fifo) and write it with raw PCM data in realtime. -Path and format variables MUST be the same displayed above. -By reading that, mvc is able to compute a frequency spectrum of the current track played. ## Building Makefile-based. + +**Build with status display (requires libmpdclient):** ``` make all ``` +**Build without status display (no external dependencies):** +``` +make nostatus +``` + To install: ``` sudo make install @@ -36,10 +44,9 @@ Simply run in your terminal: ``` -mvc [color] +mvc ``` -[color] is the number of a color in your terminal colorscheme. The keypress '**q**'' at anytime **quits** the program and restores the terminal. -Note that an instance of MPD must be running for mvc to work, otherwise the fifo file doesn't exist. +Note that an instance of MPD must be running for mvc to work, otherwise the fifo file wouldn't exist. diff --git a/makefile b/makefile index 9400f69..2aed278 100644 --- a/makefile +++ b/makefile @@ -1,19 +1,29 @@ -SRC = src/fft.c src/utils_curses.c src/music_visualizer.c src/utils_mpd.c src/settings.h#src/alsa_fifo.c +SRC = src/settings.h src/fft.c src/utils_curses.c src/music_visualizer.c src/utils_mpd.c ALL = $(SRC) LOCPATH = ./bin INSTALLPATH = /usr/local -LFLAGS = -lm -lcurses -ldl -lmpdclient #-ltinfo #-lasound +CFLAGS = -O2 +LFLAGS = -lm -lcurses -ldl #-ltinfo #-lasound DEBFLAGS = -g -Wall NAME = mvc +LFLAGS_STATUS = -lmpdclient + +LIBMPDCLIENT=$(shell [ -e /usr/include/mpd/client.h ] && echo 0 || echo 1) +ifeq ($(LIBMPDCLIENT), 0) + CFLAGS += -DSTATUS_CHECK +endif all: mkdir -p $(LOCPATH) - gcc $(ALL) -o $(LOCPATH)/$(NAME) $(LFLAGS) + gcc $(ALL) -o $(LOCPATH)/$(NAME) $(CFLAGS) $(LFLAGS) $(LFLAGS_STATUS) +nostatus: + mkdir -p $(LOCPATH) + gcc $(ALL) -o $(LOCPATH)/$(NAME) -O2 $(LFLAGS) debug: mkdir -p $(LOCPATH)/debug - gcc $(ALL) -o $(LOCPATH)/debug/$(NAME)_debug $(LFLAGS) $(DEBFLAGS) + gcc $(ALL) -o $(LOCPATH)/debug/$(NAME)_debug $(CFLAGS) $(LFLAGS) $(DEBFLAGS) asan: - gcc $(ALL) -o $(LOCPATH)/debug/$(NAME)_debug_asan $(LFLAGS) $(DEBFLAGS) -fsanitize=address + gcc $(ALL) -o $(LOCPATH)/debug/$(NAME)_debug_asan $(CFLAGS) $(LFLAGS) $(DEBFLAGS) -fsanitize=address clean: rm $(LOCPATH)/$(NAME) rm $(LOCPATH)/debug/$(NAME)_debug diff --git a/src/music_visualizer.c b/src/music_visualizer.c index ef4df18..93be409 100755 --- a/src/music_visualizer.c +++ b/src/music_visualizer.c @@ -1,5 +1,4 @@ #include -#include // status #include // exit #include // read #include // open @@ -15,14 +14,18 @@ #include "fft.h" // fast fourier transform #include "utils_curses.h" #include "alsa_fifo.h" -#include "utils_mpd.h" #include "settings.h" - +#ifdef STATUS_CHECK +#include // status +#include "utils_mpd.h" static _Atomic bool getstatus = true; +#endif + static int maxR = 0; static int maxC = 0; +#ifdef STATUS_CHECK void get_mpd_status(struct mpd_connection* session, STATUS* status) { @@ -37,6 +40,7 @@ { getstatus = true; } +#endif void process_fifo (uint16_t* buf, unsigned int* fftBuf, unsigned int* fftAvg) { @@ -74,9 +78,11 @@ { uint16_t buf[N_SAMPLES]; int fifo; +#ifdef STATUS_CHECK struct mpd_connection *session; - bool over = false; STATUS* status = NULL; +#endif + bool over = false; fd_set set; int ret; int cnt = 0; // used to set resolution (wether to skip / not to skip a read) @@ -95,10 +101,11 @@ memset(fftAvg, 0, N_SAMPLES*sizeof(unsigned int)); // open connection to mpd and set alarm to refresh status +#ifdef STATUS_CHECK session = open_connection(); - signal(SIGALRM, alarm_status); alarm(STATUS_REFRESH); +#endif while(!over) { if (wgetch(stdscr) == 'q') { @@ -108,7 +115,7 @@ // if > 0 means data to be read if ((ret = select(fifo+1, &set, NULL, NULL, NULL)) > 0) { // read data when avaiable - read(fifo, (uint16_t*)buf, 2*N_SAMPLES); + ret = read(fifo, (uint16_t*)buf, 2*N_SAMPLES); // process read buffer if(cnt == NICENESS) { process_fifo(buf, fftBuf, fftAvg); @@ -118,6 +125,7 @@ } } // refresh status at SIGALRM +#ifdef STATUS_CHECK if(getstatus) { // get mpd status free_status_st(status); @@ -126,6 +134,7 @@ // set alarm for status refresh alarm(STATUS_REFRESH); } +#endif if (ret > 0) { // clear screen for printing (only if new data on fifo) if(cnt == NICENESS) { @@ -133,24 +142,29 @@ print_visual(fftBuf, fftAvg); } } +#ifdef STATUS_CHECK // print mpd status even if no new data is avaiable print_mpd_status(status, maxC, maxR/2+maxR/6); +#endif // refresh screen refresh(); } +#ifdef STATUS_CHECK + close_connection(session); + free_status_st(status); +#endif free(fftAvg); free(fftBuf); - free_status_st(status); close(fifo); - close_connection(session); } int main(int argc, char *argv[]) { WINDOW *mainwin; - +#ifdef STATUS_CHECK import_var_from_settings(); +#endif if((mainwin = curses_init()) == NULL){ exit(EXIT_FAILURE); diff --git a/src/settings.h b/src/settings.h index 93805dc..7689d46 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,14 +1,12 @@ /* **** MPD options */ - static char defHost[] = "localhost"; static unsigned int defPort = 6600; // database location, needed for libmpdclient static char defDBlocation[] = "/home/francesco/.mpd/mpd.db"; // timeout for MPD connection to fail #define TIMEOUT 1000 - /* **** Visualizer options */ diff --git a/src/utils_curses.c b/src/utils_curses.c index 6cdfdbe..0b628cd 100755 --- a/src/utils_curses.c +++ b/src/utils_curses.c @@ -60,6 +60,7 @@ } +#ifdef STATUS_CHECK /* prints a STATUS structure to stdout */ void print_mpd_status(STATUS* status, const int maxC, const int row) @@ -137,4 +138,4 @@ } return; } - +#endif diff --git a/src/utils_curses.h b/src/utils_curses.h index 9248bbd..cff7357 100755 --- a/src/utils_curses.h +++ b/src/utils_curses.h @@ -10,4 +10,7 @@ WINDOW* curses_init(); void print_col(int col, int length, const int maxR, const int maxC); void print_row(int row, int length, const int maxR, const int maxC); + +#ifdef STATUS_CHECK void print_mpd_status(STATUS* status, const int maxC, const int row); +#endif diff --git a/src/utils_mpd.c b/src/utils_mpd.c index 7c385d9..c16305a 100644 --- a/src/utils_mpd.c +++ b/src/utils_mpd.c @@ -1,3 +1,5 @@ + +#ifdef STATUS_CHECK #include "utils_mpd.h" #include // libmpdclient #include // fprintf @@ -437,3 +439,5 @@ return i; } } + +#endif diff --git a/src/utils_mpd.h b/src/utils_mpd.h index f88b8b0..9d9e337 100644 --- a/src/utils_mpd.h +++ b/src/utils_mpd.h @@ -1,3 +1,5 @@ +#ifdef STATUS_CHECK + #ifndef KPD_UTIL_H #define KPD_UTIL_H #include @@ -45,8 +47,8 @@ bool change_port (); bool change_host (); -void import_var_from_settings (); void free_var_from_settings (); +void import_var_from_settings (); struct mpd_connection *open_connection(); void close_connection(struct mpd_connection *mpdConnection); @@ -97,3 +99,4 @@ bool print_full_names (char **args, int n); #endif +#endif