diff --git a/src/fft.c b/src/fft.c index 06e9f91..4d75351 100644 --- a/src/fft.c +++ b/src/fft.c @@ -1,5 +1,6 @@ #include "fft.h" #include "settings.h" +#include /** * === @@ -16,6 +17,7 @@ { int i, cnt = 0; cplx *ret = malloc((len/2)*sizeof(cplx)); + memset(ret, 0, (len/2)*sizeof(cplx)); if(!ret) return NULL; @@ -46,17 +48,21 @@ evenA = _fast_ft(split_array(compArray, len, EVEN), len/2); oddA = _fast_ft(split_array(compArray, len, ODD), len/2); + // input components are not needed anymore + free(compArray); + /*the final array*/ transformedA = malloc(len*sizeof(cplx)); + memset(transformedA, 0, len*sizeof(cplx)); for(i=0; i<(len/2); i++){ transformedA[i] = evenA[i] + omega*oddA[i]; transformedA[i+(len/2)] = evenA[i] - omega*oddA[i]; omega = omegaN*omega; } + free(evenA); free(oddA); - free(compArray); return transformedA; } @@ -68,10 +74,10 @@ unsigned int amplitude(cplx c, const unsigned int n) { - // compute a normalized amplitude (power spectrum, not squared) - double sq = sqrt(pow(creal(c)/n, 2) + pow(cimag(c)/n, 2)); + // compute a normalized amplitude (power spectrum, not squared) + double sq = sqrt(pow(creal(c)/n, 2) + pow(cimag(c)/n, 2)); - return round(20*log10(sq)); // dB scale + return round(20*log10(sq)); // dB scale } /** @@ -82,31 +88,38 @@ * - stores the result in the output buffer */ void -fast_fft(const int inLen, uint16_t *sig, unsigned int *fftSig) +fast_fft(uint16_t *sig, unsigned int *fftSig) { - int i; - cplx *inputComponents; - cplx *outputComponents; + int i,b; + cplx *inputComponents; + cplx *outputComponents; + int inLen = N_SAMPLES*PADDING; - if(inLen % 2 != 0){ - fprintf(stderr, "The length of the array MUST be a power of 2."); - exit(EXIT_FAILURE); - } - inputComponents = (cplx*)malloc((inLen)*sizeof(cplx)); + if(inLen % 2 != 0){ + fprintf(stderr, "The length of the array MUST be a power of 2."); + exit(EXIT_FAILURE); + } - for(i=1; i inLen/4) break; + fftSig[b+i] = amplitude(outputComponents[i], bandSize); + } + free(outputComponents); + } + } diff --git a/src/fft.h b/src/fft.h index 77f5da5..0e5cb26 100644 --- a/src/fft.h +++ b/src/fft.h @@ -15,6 +15,6 @@ cplx *_fast_ft(cplx *compArray, int len); void print_components(cplx *a, int len); unsigned int amplitude(cplx c, unsigned int n); -void fast_fft(int inLen, uint16_t *sig, unsigned int* fftSig); +void fast_fft(uint16_t *sig, unsigned int* fftSig); void average_signal(unsigned int *fftBuf, int inLen, int maxC, const double f, const int oratio, unsigned int *fftAvg); void avgEnergy(unsigned int* fftBuf, const int inLen, unsigned int* energyBuf); diff --git a/src/music_visualizer.c b/src/music_visualizer.c index 815b829..7ebfab1 100644 --- a/src/music_visualizer.c +++ b/src/music_visualizer.c @@ -30,8 +30,6 @@ } #endif -#define PADDING 1 - static int maxR = 0; static int maxC = 0; @@ -41,7 +39,7 @@ const double basefreq, const int oratio) { // fft of samples array - fast_fft(nsamples, (uint16_t*)buf, fftBuf); + fast_fft((uint16_t*)buf, fftBuf); // computes an average of the signals in fftBuf // based on the number of columns of the screen @@ -97,7 +95,6 @@ int cnt = 0; // used to set resolution (wether to skip / not to skip a read) uint32_t sampleRate = 0; int nsamples = N_SAMPLES*PADDING; // adapt processing to sample rate, zero-pad - int sEnergyLen = N_SAMPLES; PATTERN pattern = LINE; int statusHeight = 0; int statusCol = 0; @@ -114,15 +111,15 @@ FD_SET(fifo, &set); // allocate buffers used - unsigned int* fftBuf= (unsigned int*)malloc(N_SAMPLES/2*sizeof(unsigned int)); - unsigned int* fftAvg = (unsigned int*)malloc(maxC*sizeof(unsigned int)); + unsigned int* fftBuf= (unsigned int*)malloc((N_SAMPLES/2)*sizeof(unsigned int)); + unsigned int* fftAvg = (unsigned int*)malloc((N_SAMPLES/2)*sizeof(unsigned int)); cebuffer energyBuffer; cb_init(&energyBuffer, 43); // set the result buffer to 0s memset(fftBuf, 0, (N_SAMPLES/2)*sizeof(unsigned int)); - memset(fftAvg, 0, maxC*sizeof(unsigned int)); + memset(fftAvg, 0, (N_SAMPLES/2)*sizeof(unsigned int)); // open connection to mpd and set alarm to refresh status #ifdef STATUS_CHECK @@ -213,13 +210,6 @@ // get mpd status free_status_st(status); status = get_current_status(session); - if (status) { - if (((sampleRate = status->sampleRate) >= 96000) && ADAPTIVE_SAMPLING) { - nsamples = N_SAMPLES/2; - } else if (sampleRate < 96000) { - nsamples = N_SAMPLES; - } - } getstatus = false; // set alarm for status refresh alarm(STATUS_REFRESH); @@ -295,7 +285,6 @@ keypad(stdscr, TRUE); // arrow keys #endif - unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; init_genrand(time(NULL)); // get screen properties diff --git a/src/settings.h b/src/settings.h index bba59d0..b61d39e 100644 --- a/src/settings.h +++ b/src/settings.h @@ -7,6 +7,7 @@ // number of samples // (POWER OF 2) #define N_SAMPLES 2048 +#define PADDING 1 // if adaptive sampling should be used // (adjust N_SAMPLES to sample rate of current song) diff --git a/src/utils_curses.c b/src/utils_curses.c index 46b715c..260ca4e 100644 --- a/src/utils_curses.c +++ b/src/utils_curses.c @@ -42,7 +42,6 @@ void print_pattern(int col, int row, int l, const int maxR, const int maxC, PATTERN pattern, int seed) { - bool randc; switch(pattern) { case CURVE: if (row > maxR-l && row < maxR-l/3) { // center of the screen @@ -191,7 +190,6 @@ print_rate_info(const int rate, const int nsamples, const int maxC, int seed, const double basefreq, const int oratio) { int center = (int) maxC/2-33; // adjust to screen center - int i; srand(seed); color_set(rand() % 7, NULL); diff --git a/src/utils_mpd.c b/src/utils_mpd.c index fe8c4a0..759d60e 100644 --- a/src/utils_mpd.c +++ b/src/utils_mpd.c @@ -40,9 +40,9 @@ import_var_from_settings () { // import variables from settings.h to be passed to other functions - _host = strdup (defHost); + _host = strdup(defHost); _port = defPort; - _DBlocation = strdup (defDBlocation); + _DBlocation = strdup(defDBlocation); } void