diff --git a/cava.c b/cava.c index 162b549..d0d0d3e 100644 --- a/cava.c +++ b/cava.c @@ -137,12 +137,13 @@ int *separate_freq_bands(int FFTbassbufferSize, fftw_complex out_bass[FFTbassbuf int FFTmidbufferSize, fftw_complex out_mid[FFTmidbufferSize / 2 + 1], int FFTtreblebufferSize, fftw_complex out_treble[FFTtreblebufferSize / 2 + 1], int bass_cut_off_bar, - int treble_cut_off_bar, int number_of_bars, int lcf[256], int hcf[256], + int treble_cut_off_bar, int number_of_bars, + int FFTbuffer_lower_cut_off[256], int FFTbuffer_upper_cut_off[256], double eq[256], int channel, double sens, double ignore) { int n, i; double peak[257]; - static int fl[256]; - static int fr[256]; + static int bars_left[256]; + static int bars_right[256]; double y[FFTbassbufferSize / 2 + 1]; double temp; @@ -153,7 +154,7 @@ int *separate_freq_bands(int FFTbassbufferSize, fftw_complex out_bass[FFTbassbuf i = 0; // process: get peaks - for (i = lcf[n]; i <= hcf[n]; i++) { + for (i = FFTbuffer_lower_cut_off[n]; i <= FFTbuffer_upper_cut_off[n]; i++) { if (n <= bass_cut_off_bar) { y[i] = hypot(out_bass[i][0], out_bass[i][1]); } else if (n > bass_cut_off_bar && n <= treble_cut_off_bar) { @@ -165,21 +166,22 @@ int *separate_freq_bands(int FFTbassbufferSize, fftw_complex out_bass[FFTbassbuf peak[n] += y[i]; // adding upp band } - peak[n] = peak[n] / (hcf[n] - lcf[n] + 1); // getting average - temp = peak[n] * sens * eq[n]; // multiplying with k and sens + peak[n] = peak[n] / + (FFTbuffer_upper_cut_off[n] - FFTbuffer_lower_cut_off[n] + 1); // getting average + temp = peak[n] * sens * eq[n]; // multiplying with k and sens // printf("%d peak o: %f * sens: %f * k: %f = f: %f\n", o, peak[o], sens, eq[o], temp); if (temp <= ignore) temp = 0; if (channel == LEFT_CHANNEL) - fl[n] = temp; + bars_left[n] = temp; else - fr[n] = temp; + bars_right[n] = temp; } if (channel == LEFT_CHANNEL) - return fl; + return bars_left; else - return fr; + return bars_right; } int *monstercat_filter(int *bars, int number_of_bars, int waves, double monstercat) { @@ -227,7 +229,7 @@ int main(int argc, char **argv) { int thr_id GCC_UNUSED; float cut_off_frequency[256]; float relative_cut_off[256]; - int bars[256], lcf[256], hcf[256]; + int bars[256], FFTbuffer_lower_cut_off[256], FFTbuffer_upper_cut_off[256]; int *bars_left, *bars_right, *bars_mono; int bars_mem[256]; int bars_last[256]; @@ -530,7 +532,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co case OUTPUT_NCURSES: init_terminal_ncurses(p.color, p.bcolor, p.col, p.bgcol, p.gradient, p.gradient_count, p.gradient_colors, &width, &lines); - //we have 8 times as much height due to using 1/8 block characters + // we have 8 times as much height due to using 1/8 block characters height = lines * 8; break; #endif @@ -581,19 +583,20 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co default: exit(EXIT_FAILURE); // Can't happen. - } + } // handle for user setting too many bars if (p.fixedbars) { p.autobars = 0; - if (p.fixedbars * p.bar_width + p.fixedbars * p.bs - p.bs > width) + if (p.fixedbars * p.bar_width + p.fixedbars * p.bar_spacing - p.bar_spacing > width) p.autobars = 1; } // getting orignial numbers of barss incase of resize if (p.autobars == 1) { - number_of_bars = (width + p.bs) / (p.bar_width + p.bs); - // if (p.bs != 0) number_of_bars = (width - number_of_bars * p.bs + p.bs) / bar_width; + number_of_bars = (width + p.bar_spacing) / (p.bar_width + p.bar_spacing); + // if (p.bar_spacing != 0) number_of_bars = (width - number_of_bars * p.bar_spacing + // + p.bar_spacing) / bar_width; } else number_of_bars = p.fixedbars; @@ -608,7 +611,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co } // checks if there is stil extra room, will use this to center - rest = (width - number_of_bars * p.bar_width - number_of_bars * p.bs + p.bs) / 2; + rest = (width - number_of_bars * p.bar_width - number_of_bars * p.bar_spacing + + p.bar_spacing) / + 2; if (rest < 0) rest = 0; @@ -619,19 +624,22 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co double integral = p.integral * sqrt(log10(lines / 3 + 1)); #ifndef NDEBUG - printw("height: %d width: %d bars:%d bar width: %d rest: %d\n", width, lines, number_of_bars, p.bar_width, rest); + printw("height: %d width: %d bars:%d bar width: %d rest: %d\n", width, lines, + number_of_bars, p.bar_width, rest); #endif if (p.stereo) - number_of_bars = number_of_bars / 2; // in stereo onle half number of number_of_bars per channel + number_of_bars = + number_of_bars / 2; // in stereo onle half number of number_of_bars per channel if (p.userEQ_enabled && (number_of_bars > 0)) { - userEQ_keys_to_bars_ratio = (double)(((double)p.userEQ_keys) / ((double)number_of_bars)); + userEQ_keys_to_bars_ratio = + (double)(((double)p.userEQ_keys) / ((double)number_of_bars)); } - //calculate frequency constant (used to distribute bars across the frequency band) - double frequency_constant = - log10((float)p.lower_cut_off / (float)p.upper_cut_off) / (1 / ((float)number_of_bars + 1) - 1); + // calculate frequency constant (used to distribute bars across the frequency band) + double frequency_constant = log10((float)p.lower_cut_off / (float)p.upper_cut_off) / + (1 / ((float)number_of_bars + 1) - 1); // process: calculate cutoff frequencies and eq int bass_cut_off_bar = -1; @@ -641,7 +649,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co for (n = 0; n < number_of_bars + 1; n++) { double bar_distribution_coefficient = frequency_constant * (-1); - bar_distribution_coefficient += ((float)n + 1) / ((float)number_of_bars + 1) * frequency_constant; + bar_distribution_coefficient += + ((float)n + 1) / ((float)number_of_bars + 1) * frequency_constant; cut_off_frequency[n] = p.upper_cut_off * pow(10, bar_distribution_coefficient); relative_cut_off[n] = cut_off_frequency[n] / (audio.rate / 2); // remember nyquist!, pr my calculations this should be rate/2 @@ -652,34 +661,45 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co eq[n] *= (float)height / pow(2, 28); if (p.userEQ_enabled) eq[n] *= p.userEQ[(int)floor(((double)n) * userEQ_keys_to_bars_ratio)]; + eq[n] /= log2(audio.FFTbassbufferSize); - // lfc stores the lower cut frequency foo each bar in the fft out buffer + if (cut_off_frequency[n] < bass_cut_off) { - lcf[n] = relative_cut_off[n] * (audio.FFTbassbufferSize / 2) + 1; + // BASS + FFTbuffer_lower_cut_off[n] = + relative_cut_off[n] * (audio.FFTbassbufferSize / 2) + 1; bass_cut_off_bar++; treble_cut_off_bar++; + eq[n] *= log2(audio.FFTbassbufferSize); - } else if (cut_off_frequency[n] > bass_cut_off && cut_off_frequency[n] < treble_cut_off) { - lcf[n] = relative_cut_off[n] * (audio.FFTmidbufferSize / 2) + 1; + } else if (cut_off_frequency[n] > bass_cut_off && + cut_off_frequency[n] < treble_cut_off) { + // MID + FFTbuffer_lower_cut_off[n] = + relative_cut_off[n] * (audio.FFTmidbufferSize / 2) + 1; treble_cut_off_bar++; if ((treble_cut_off_bar - bass_cut_off_bar) == 1) { first_bar = true; - hcf[n - 1] = relative_cut_off[n] * (audio.FFTbassbufferSize / 2); - if (hcf[n - 1] < lcf[n - 1]) - hcf[n - 1] = lcf[n - 1]; + FFTbuffer_upper_cut_off[n - 1] = + relative_cut_off[n] * (audio.FFTbassbufferSize / 2); + if (FFTbuffer_upper_cut_off[n - 1] < FFTbuffer_lower_cut_off[n - 1]) + FFTbuffer_upper_cut_off[n - 1] = FFTbuffer_lower_cut_off[n - 1]; } else { first_bar = false; } eq[n] *= log2(audio.FFTmidbufferSize); } else { - lcf[n] = relative_cut_off[n] * (audio.FFTtreblebufferSize / 2) + 1; + // TREBLE + FFTbuffer_lower_cut_off[n] = + relative_cut_off[n] * (audio.FFTtreblebufferSize / 2) + 1; first_treble_bar++; if (first_treble_bar == 1) { first_bar = true; - hcf[n - 1] = relative_cut_off[n] * (audio.FFTmidbufferSize / 2); - if (hcf[n - 1] < lcf[n - 1]) - hcf[n - 1] = lcf[n - 1]; + FFTbuffer_upper_cut_off[n - 1] = + relative_cut_off[n] * (audio.FFTmidbufferSize / 2); + if (FFTbuffer_upper_cut_off[n - 1] < FFTbuffer_lower_cut_off[n - 1]) + FFTbuffer_upper_cut_off[n - 1] = FFTbuffer_lower_cut_off[n - 1]; } else { first_bar = false; } @@ -688,18 +708,21 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co } if (n != 0 && !first_bar) { - hcf[n - 1] = lcf[n] - 1; + FFTbuffer_upper_cut_off[n - 1] = FFTbuffer_lower_cut_off[n] - 1; - // pushing the spectrum up if the expe function gets "clumped" - if (lcf[n] <= lcf[n - 1]) - lcf[n] = lcf[n - 1] + 1; - hcf[n - 1] = lcf[n] - 1; + // pushing the spectrum up if the exponential function gets "clumped" in the + // bass + if (FFTbuffer_lower_cut_off[n] <= FFTbuffer_lower_cut_off[n - 1]) + FFTbuffer_lower_cut_off[n] = FFTbuffer_lower_cut_off[n - 1] + 1; + FFTbuffer_upper_cut_off[n - 1] = FFTbuffer_lower_cut_off[n] - 1; } #ifndef NDEBUG if (n != 0) { - mvprintw(n, 0, "%d: %f -> %f (%d -> %d) bass: %d, treble:%d \n", n, cut_off_frequency[n - 1], - cut_off_frequency[n], lcf[n - 1], hcf[n - 1], bass_cut_off_bar, treble_cut_off_bar); + mvprintw(n, 0, "%d: %f -> %f (%d -> %d) bass: %d, treble:%d \n", n, + cut_off_frequency[n - 1], cut_off_frequency[n], + FFTbuffer_lower_cut_off[n - 1], FFTbuffer_upper_cut_off[n - 1], + bass_cut_off_bar, treble_cut_off_bar); } #endif } @@ -814,17 +837,17 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co fftw_execute(p_treble_l); fftw_execute(p_treble_r); - bars_left = separate_freq_bands(audio.FFTbassbufferSize, out_bass_l, - audio.FFTmidbufferSize, out_mid_l, - audio.FFTtreblebufferSize, out_treble_l, - bass_cut_off_bar, treble_cut_off_bar, number_of_bars / 2, - lcf, hcf, eq, LEFT_CHANNEL, p.sens, p.ignore); + bars_left = separate_freq_bands( + audio.FFTbassbufferSize, out_bass_l, audio.FFTmidbufferSize, out_mid_l, + audio.FFTtreblebufferSize, out_treble_l, bass_cut_off_bar, + treble_cut_off_bar, number_of_bars / 2, FFTbuffer_lower_cut_off, + FFTbuffer_upper_cut_off, eq, LEFT_CHANNEL, p.sens, p.ignore); - bars_right = separate_freq_bands(audio.FFTbassbufferSize, out_bass_r, - audio.FFTmidbufferSize, out_mid_r, - audio.FFTtreblebufferSize, out_treble_r, - bass_cut_off_bar, treble_cut_off_bar, number_of_bars / 2, - lcf, hcf, eq, RIGHT_CHANNEL, p.sens, p.ignore); + bars_right = separate_freq_bands( + audio.FFTbassbufferSize, out_bass_r, audio.FFTmidbufferSize, out_mid_r, + audio.FFTtreblebufferSize, out_treble_r, bass_cut_off_bar, + treble_cut_off_bar, number_of_bars / 2, FFTbuffer_lower_cut_off, + FFTbuffer_upper_cut_off, eq, RIGHT_CHANNEL, p.sens, p.ignore); } else { fftw_execute(p_bass_l); @@ -833,7 +856,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co bars_mono = separate_freq_bands( audio.FFTbassbufferSize, out_bass_l, audio.FFTmidbufferSize, out_mid_l, audio.FFTtreblebufferSize, out_treble_l, bass_cut_off_bar, - treble_cut_off_bar, number_of_bars, lcf, hcf, eq, LEFT_CHANNEL, p.sens, p.ignore); + treble_cut_off_bar, number_of_bars, FFTbuffer_lower_cut_off, + FFTbuffer_upper_cut_off, eq, LEFT_CHANNEL, p.sens, p.ignore); } } else { //**if in sleep mode wait and continue**// @@ -851,10 +875,13 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co if (p.monstercat) { if (p.stereo) { - bars_left = monstercat_filter(bars_left, number_of_bars / 2, p.waves, p.monstercat); - bars_right = monstercat_filter(bars_right, number_of_bars / 2, p.waves, p.monstercat); + bars_left = + monstercat_filter(bars_left, number_of_bars / 2, p.waves, p.monstercat); + bars_right = monstercat_filter(bars_right, number_of_bars / 2, p.waves, + p.monstercat); } else { - bars_mono = monstercat_filter(bars_mono, number_of_bars, p.waves, p.monstercat); + bars_mono = + monstercat_filter(bars_mono, number_of_bars, p.waves, p.monstercat); } } @@ -903,7 +930,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co #ifndef NDEBUG mvprintw(n, 0, "%d: f:%f->%f (%d->%d), k-value:\ %15e, peak:%d \n", - n, cut_off_frequency[n], cut_off_frequency[n + 1], lcf[n], hcf[n], eq[n], bars[n]); + n, cut_off_frequency[n], cut_off_frequency[n + 1], + FFTbuffer_lower_cut_off[n], FFTbuffer_upper_cut_off[n], eq[n], + bars[n]); // if(bars[n] > maxvalue) maxvalue = bars[n]; #endif } @@ -931,20 +960,22 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co switch (output_mode) { case OUTPUT_NCURSES: #ifdef NCURSES - rc = draw_terminal_ncurses(inAtty, lines, width, number_of_bars, p.bar_width, p.bs, rest, bars, previous_frame, + rc = draw_terminal_ncurses(inAtty, lines, width, number_of_bars, p.bar_width, + p.bar_spacing, rest, bars, previous_frame, p.gradient); break; #endif case OUTPUT_NONCURSES: - rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars, p.bar_width, p.bs, rest, bars, previous_frame); + rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars, p.bar_width, + p.bar_spacing, rest, bars, previous_frame); break; case OUTPUT_RAW: - rc = print_raw_out(number_of_bars, fp, p.is_bin, p.bit_format, p.ascii_range, p.bar_delim, - p.frame_delim, bars); + rc = print_raw_out(number_of_bars, fp, p.is_bin, p.bit_format, p.ascii_range, + p.bar_delim, p.frame_delim, bars); break; default: - exit(EXIT_FAILURE); // Can't happen. + exit(EXIT_FAILURE); // Can't happen. } // terminal has been resized breaking to recalibrating values diff --git a/config.c b/config.c index 0b7b260..53bec2a 100644 --- a/config.c +++ b/config.c @@ -177,7 +177,7 @@ bool validate_config(struct config_params *p, struct error_s *error) { } if (strcmp(outputMethod, "raw") == 0) { // raw: p->om = OUTPUT_RAW; - p->bs = 0; + p->bar_spacing = 0; p->bar_width = 1; // checking data format @@ -208,17 +208,18 @@ bool validate_config(struct config_params *p, struct error_s *error) { } if (p->om == OUTPUT_NOT_SUPORTED) { #ifndef NCURSES - write_errorf(error, - "output method %s is not supported, supported methods are: 'noncurses' and 'raw'\n", - outputMethod); + write_errorf( + error, + "output method %s is not supported, supported methods are: 'noncurses' and 'raw'\n", + outputMethod); return false; #endif #ifdef NCURSES - write_errorf( - error, - "output method %s is not supported, supported methods are: 'ncurses', 'noncurses' and 'raw'\n", - outputMethod); + write_errorf(error, + "output method %s is not supported, supported methods are: 'ncurses', " + "'noncurses' and 'raw'\n", + outputMethod); return false; #endif } @@ -423,7 +424,7 @@ bool load_config(char configPath[PATH_MAX], struct config_params *p, bool colors p->fixedbars = iniparser_getint(ini, "general:bars", 0); p->bar_width = iniparser_getint(ini, "general:bar_width", 2); - p->bs = iniparser_getint(ini, "general:bar_spacing", 1); + p->bar_spacing = iniparser_getint(ini, "general:bar_spacing", 1); p->framerate = iniparser_getint(ini, "general:framerate", 60); p->sens = iniparser_getint(ini, "general:sensitivity", 100); p->autosens = iniparser_getint(ini, "general:autosens", 1); diff --git a/config.h b/config.h index 0226d16..1944a2e 100644 --- a/config.h +++ b/config.h @@ -49,12 +49,7 @@ enum input_method { INPUT_MAX }; -enum output_method { - OUTPUT_NCURSES, - OUTPUT_NONCURSES, - OUTPUT_RAW, - OUTPUT_NOT_SUPORTED -}; +enum output_method { OUTPUT_NCURSES, OUTPUT_NONCURSES, OUTPUT_RAW, OUTPUT_NOT_SUPORTED }; struct config_params { char *color, *bcolor, *raw_target, *audio_source, @@ -66,8 +61,8 @@ struct config_params { enum input_method im; enum output_method om; int userEQ_keys, userEQ_enabled, col, bgcol, autobars, stereo, is_bin, ascii_range, bit_format, - gradient, gradient_count, fixedbars, framerate, bar_width, bs, autosens, overshoot, waves, - FFTbufferSize, fifoSample, fifoSampleBits; + gradient, gradient_count, fixedbars, framerate, bar_width, bar_spacing, autosens, overshoot, + waves, FFTbufferSize, fifoSample, fifoSampleBits; }; struct error_s { diff --git a/output/terminal_noncurses.c b/output/terminal_noncurses.c index c071208..312d2cb 100644 --- a/output/terminal_noncurses.c +++ b/output/terminal_noncurses.c @@ -104,8 +104,8 @@ void get_terminal_dim_noncurses(int *w, int *h) { system("clear"); // clearing in case of resieze } -int draw_terminal_noncurses(int tty, int h, int w, int bars, int bar_width, int bs, int rest, int f[200], - int flastd[200]) { +int draw_terminal_noncurses(int tty, int h, int w, int bars, int bar_width, int bs, int rest, + int f[200], int flastd[200]) { int c, move, n, o; struct winsize dim; diff --git a/output/terminal_noncurses.h b/output/terminal_noncurses.h index d2cc562..8e8c4fb 100644 --- a/output/terminal_noncurses.h +++ b/output/terminal_noncurses.h @@ -1,5 +1,5 @@ int init_terminal_noncurses(int col, int bgcol, int w, int h, int bar_width); void get_terminal_dim_noncurses(int *w, int *h); -int draw_terminal_noncurses(int virt, int height, int width, int bars, int bar_width, int bs, int rest, - int f[200], int flastd[200]); +int draw_terminal_noncurses(int virt, int height, int width, int bars, int bar_width, int bs, + int rest, int f[200], int flastd[200]); void cleanup_terminal_noncurses(void);