diff --git a/src/music_visualizer.c b/src/music_visualizer.c index 0e9e0be..428e90b 100644 --- a/src/music_visualizer.c +++ b/src/music_visualizer.c @@ -42,13 +42,13 @@ #endif void -process_fifo (uint16_t* buf, unsigned int* fftBuf, unsigned int* fftAvg) { +process_fifo (uint16_t* buf, unsigned int* fftBuf, unsigned int* fftAvg, int nsamples) { // performs a Fourier Transform of the buffer data - fast_fft(N_SAMPLES, (uint16_t*)buf, fftBuf); + fast_fft(nsamples, (uint16_t*)buf, fftBuf); // computes an average of the signals in fftBuf // based on the number of columns of the screen - average_signal(fftBuf, N_SAMPLES, maxC, fftAvg); + average_signal(fftBuf, nsamples, maxC, fftAvg); } void @@ -83,6 +83,8 @@ fd_set set; int ret; int cnt = 0; // used to set resolution (wether to skip / not to skip a read) + uint32_t sampleRate = 0; + int nsamples = N_SAMPLES; // adapt processing to sample rate // open mpd fifo while((fifo = open(MPD_FIFO, O_RDONLY)) == -1); @@ -112,10 +114,14 @@ // if > 0 means data to be read if ((ret = select(fifo+1, &set, NULL, NULL, NULL)) > 0) { // read data when avaiable - ret = read(fifo, (uint16_t*)buf, 2*N_SAMPLES); + if (sampleRate == 96000) { + ret = read(fifo, (uint16_t*)buf, N_SAMPLES); + } else if (sampleRate < 96000) { + ret = read(fifo, (uint16_t*)buf, 2*N_SAMPLES); + } // process read buffer if(cnt == NICENESS) { - process_fifo(buf, fftBuf, fftAvg); + process_fifo(buf, fftBuf, fftAvg, nsamples); cnt = 0; } else { cnt++; @@ -127,6 +133,11 @@ // get mpd status free_status_st(status); status = get_current_status(session); + if ((sampleRate = status->sampleRate) == 96000) { + nsamples = N_SAMPLES/2; + } else if (sampleRate < 96000) { + nsamples = N_SAMPLES; + } getstatus = false; // set alarm for status refresh alarm(STATUS_REFRESH); @@ -179,6 +190,5 @@ // free resources endwin(); delwin(mainwin); - return 0; } diff --git a/src/settings.h b/src/settings.h index d6f86f4..ba6b160 100644 --- a/src/settings.h +++ b/src/settings.h @@ -16,6 +16,7 @@ // NOTE: modifying the original value of 1024 // might result in messy output #define N_SAMPLES 1024 + // path to the MPD named pipe // (defined: 'mpd.conf') #define MPD_FIFO "/tmp/mpd.fifo" diff --git a/src/utils_mpd.c b/src/utils_mpd.c index c16305a..5a76036 100644 --- a/src/utils_mpd.c +++ b/src/utils_mpd.c @@ -23,6 +23,14 @@ char *_DBlocation; unsigned int _port; +uint32_t +get_sample_rate(const struct mpd_status* status) +{ + struct mpd_audio_format* format = mpd_status_get_audio_format(status); + uint32_t rate = format->sample_rate; + return rate; +} + void import_var_from_settings () { @@ -311,133 +319,9 @@ status->elapsedTime_min = eltime/60; status->elapsedTime_sec = eltime%60; status->queueLenght = mpd_status_get_queue_length(mpdStatus); + status->sampleRate = get_sample_rate(mpdStatus); mpd_status_free (mpdStatus); return status; } - -/* add a new element to the playlist queue - * returns 1 if error - * returns 0 if successful - * */ -bool -enqueue(QUEUE *q, SONG *s) -{ - QUEUE *t; - if((t = (QUEUE*)malloc(sizeof(QUEUE))) == NULL){ - STANDARD_USAGE_ERROR("calloc"); - return false; - } - t->song = s; - if(q->next == NULL){ - q->next = t; - q->next->next = t; - } else { - t->next = q->next->next; - q->next->next = t; - q->next = t; - } - return true; -} - -/* delete an element from the queue and save the linked song - * returns NULL if q empty - * returns SONG* element if successful - */ -SONG* -dequeue(QUEUE* q) -{ - QUEUE *t; - SONG* song = NULL; - if(q->next == NULL){ - return NULL; - } - if(q->next != q->next->next){ - t = q->next->next; - q->next->next = t->next; - song = t->song; - free(t); - } else { - song = q->next->song; - free(q->next); - q->next = NULL; - } - return song; -} - -void destroy_queue (QUEUE *q) -{ - if (q != NULL) { - destroy_queue (q->next); - /*free_song_st (q->song);*/ - free (q); - } -} - -/* receives the songs in the playlist, one by one - * after the get_current_playlist function - * returns NULL if error or empty playlist - * returns a queue of songs if successful - */ -static QUEUE* -retrieve_songs(QUEUE *q, struct mpd_connection *mpdConnection) -{ - SONG* song = NULL; - struct mpd_song* mpdSong = NULL; - - if((mpdSong = mpd_recv_song(mpdConnection)) == NULL){ - return q; - } - - song = parse_mpd_song(mpdSong); - if(enqueue(q, song) == 0){ - return NULL; - } - - retrieve_songs(q, mpdConnection); - return q; -} - -/* sends to the server the request for a playlist queue, - * calls retrieve_songs and builds q - * returns NULL if error - * returns a playlist queue if successful - */ -QUEUE* -get_current_playlist() -{ - QUEUE *q = NULL; - struct mpd_connection *mpdConnection = NULL; - - mpdConnection = open_connection (); - mpd_send_list_queue_meta(mpdConnection); - - if((q = (QUEUE*)malloc(sizeof(QUEUE))) == NULL){ - STANDARD_USAGE_ERROR("calloc"); - return NULL; - } - q->next = NULL; - retrieve_songs(q, mpdConnection); - close_connection (mpdConnection); - - return q; -} - -int -count_playlist_elements (QUEUE *q) -{ - QUEUE *tmp = q; - int i = 0; - - if (q == NULL) { - return 0; - } else { - while (tmp != NULL) { - i++; - tmp = tmp->next; - } - return i; - } -} - #endif diff --git a/src/utils_mpd.h b/src/utils_mpd.h index 9d9e337..0ebdb73 100644 --- a/src/utils_mpd.h +++ b/src/utils_mpd.h @@ -32,6 +32,7 @@ int elapsedTime_min; int elapsedTime_sec; int queueLenght; + uint32_t sampleRate; SONG* song; } STATUS; @@ -57,46 +58,6 @@ void free_status_st (STATUS *s); STATUS* get_current_status(); void print_current_status(STATUS* status); -QUEUE* get_current_playlist(); -int count_playlist_elements (QUEUE *q); -bool enqueue(QUEUE* q, SONG* s); -SONG* dequeue(QUEUE* q); -void destroy_queue (QUEUE *q); -void print_current_playlist(QUEUE *q); -bool list (char **argv, int n); - -bool play (char **args, int n); -//bool pause (char **args, int n); -bool next ( char **args, int n); -bool previous (char **args, int n); -bool stop(); -//bool clear(); -bool delete_song( int pos); -int convert_to_int(char *arg); -bool delete( char **args, int n); -bool delete_range( char **args, int n); -int compare_pos(const void *pos1, const void *pos2); -bool random_kpd( char **args, int n); -bool consume( char **args, int n); -bool single( char **args, int n); -bool repeat( char **args, int n); -bool seek( char **args, int n); -bool forward(char **args, int n); -bool backward(char **args, int n); -bool swap( char **args, int n); -//bool move( char **args, int n); -bool update( char **args, int n); -bool shuffle( char **args, int n); -bool shuffle_range( char **args, int n); -bool output_enable( char **args, int n); - -bool search_util ( char **args, int n); -void destroy_search_results (); -void print_search_results (char **results, int size); -bool filter_helper (char **args, int n); -bool vfilter_helper (char **args, int n); -bool add( char **args, int n); -bool print_full_names (char **args, int n); - +uint32_t get_sample_rate(const struct mpd_status* status); #endif #endif