Skip to content

Commit 1fb19a5

Browse files
committed
Added setting for bar width and bar spacing.
Number of bars is now automatic (fill up console), but can also be specified. Bar width is always static, defaults to 3 charachters. Cleared up usage of the words bands and bars in sourcecode. Now mostly using bars. if the config file already exists, the new settings must be added manually.
1 parent 88d1b2c commit 1fb19a5

File tree

6 files changed

+76
-70
lines changed

6 files changed

+76
-70
lines changed

cava.c

+55-51
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int rc;
4848

4949
char *inputMethod, *outputMethod, *modeString, *color, *bcolor;
5050
double monstercat, integral, gravity, ignore, smh;
51-
int fixedbands, sens, framerate;
51+
int fixedbars, sens, framerate, bw, bs;
5252
unsigned int lowcf, highcf;
5353
double smoothDef[64] = {0.8, 0.8, 1, 1, 0.8, 0.8, 1, 0.8, 0.8, 1, 1, 0.8,
5454
1, 1, 0.8, 0.6, 0.6, 0.7, 0.8, 0.8, 0.8, 0.8, 0.8,
@@ -63,8 +63,8 @@ int om = 1;
6363
int mode = 1;
6464
int col = 6;
6565
int bgcol = -1;
66-
int bands = 25;
67-
int autoband = 1;
66+
int bars = 25;
67+
int autobars = 1;
6868

6969
// general: cleanup
7070
void cleanup(void)
@@ -125,7 +125,9 @@ void load_config()
125125
ignore = iniparser_getdouble(ini, "smoothing:ignore", 0);
126126
color = (char *)iniparser_getstring(ini, "color:foreground", "default");;
127127
bcolor = (char *)iniparser_getstring(ini, "color:background", "default");;
128-
fixedbands = iniparser_getint(ini, "general:bars", 0);
128+
fixedbars = iniparser_getint(ini, "general:bars", 0);
129+
bw = iniparser_getint(ini, "general:bar_width", 3);
130+
bs = iniparser_getint(ini, "general:bar_spacing", 1);
129131
sens = iniparser_getint(ini, "general:sensitivity", 100);
130132
framerate = iniparser_getint(ini, "general:framerate", 60);
131133
lowcf = iniparser_getint(ini, "general:lower_cutoff_freq", 20);
@@ -184,9 +186,11 @@ void validate_config()
184186
exit(EXIT_FAILURE);
185187
}
186188

187-
// validate: bands
188-
if (fixedbands > 0) autoband = 0;
189-
if (fixedbands > 200)fixedbands = 200;
189+
// validate: bars
190+
if (fixedbars > 0) autobars = 0;
191+
if (fixedbars > 200) fixedbars = 200;
192+
if (bw > 200) bw = 200;
193+
if (bw < 1) bw = 1;
190194

191195
// validate: mode
192196
if (strcmp(modeString, "normal") == 0) mode = 1;
@@ -283,7 +287,7 @@ int main(int argc, char **argv)
283287
int y[M / 2 + 1];
284288
long int lpeak, hpeak;
285289
int sleep = 0;
286-
int i, n, o, bw, height, h, w, c, rest, virt;
290+
int i, n, o, height, h, w, c, rest, virt;
287291
float temp;
288292
double in[2 * (M / 2 + 1)];
289293
fftw_complex out[M / 2 + 1][2];
@@ -299,7 +303,7 @@ Usage : " PACKAGE " [options]\n\
299303
Visualize audio input in terminal. \n\
300304
\n\
301305
Options:\n\
302-
-b 1..(console columns/2-1) or 200 number of bars in the spectrum (default 25 + fills up the console), program will automatically adjust if there are too many frequency bands)\n\
306+
-b 1..(console columns/2-1) or 200 number of bars in the spectrum (default 25 + fills up the console), program will automatically adjust if there are too many bars)\n\
303307
-i 'input method' method used for listening to audio, supports: 'alsa' and 'fifo'\n\
304308
-o 'output method' method used for outputting processed data, supports: 'ncurses', 'noncurses' and 'circle'\n\
305309
-d 'alsa device' name of alsa capture device (default 'hw:Loopback,1')\n\
@@ -348,7 +352,8 @@ Options:\n\
348352
modeString = optarg;
349353
break;
350354
case 'b': // argument: bar count
351-
fixedbands = atoi(optarg);
355+
fixedbars = atoi(optarg);
356+
if (fixedbars) autobars = 0;
352357
break;
353358
case 'd': // argument: alsa device
354359
audio.source = optarg;
@@ -461,68 +466,63 @@ Options:\n\
461466
f[i] = 0;
462467
}
463468

464-
465-
//getting orignial numbers of bands incase of resize
466-
if (autoband == 1) {
467-
bands = 25;
468-
} else bands = fixedbands;
469-
470-
469+
471470
// output: get terminal's geometry
472471
if (om == 1 || om == 2) get_terminal_dim_ncurses(&w, &h);
473472

474473
if (om == 3) get_terminal_dim_noncurses(&w, &h);
475474

475+
//handle for user setting too many bars
476+
if (fixedbars) {
477+
autobars = 0;
478+
if (fixedbars * bw + fixedbars * bs - bs > w) autobars = 1;
479+
}
476480

477-
if (bands > w / 2 - 1)bands = w / 2 -
478-
1; //handle for user setting to many bars
479-
480-
if (bands < 1) bands = 1; // must have at least 1 bar;
481+
//getting orignial numbers of barss incase of resize
482+
if (autobars == 1) {
483+
bars = (w + bs) / (bw + bs);
484+
//if (bs != 0) bars = (w - bars * bs + bs) / bw;
485+
} else bars = fixedbars;
481486

482-
height = h - 1;
483487

484-
bw = (w - bands - 1) / bands;
488+
if (bars < 1) bars = 1; // must have at least 1 bar;
485489

486-
if (bw < 1) bw = 1; //bars must have width
490+
height = h - 1;
487491

488492
// process [smoothing]: calculate gravity
489493
g = gravity * ((float)height / 270) * pow((60 / (float)framerate), 2.5);
490494

491-
//if no bands are selected it tries to padd the default 20 if there is extra room
492-
if (autoband == 1) bands = bands + ((w - (bw * bands + bands - 1)) /
493-
(bw + 1));
494-
495495

496496
//checks if there is stil extra room, will use this to center
497-
rest = (w - bands * bw - bands + 1) / 2;
497+
rest = (w - bars * bw - bars * bs + bs) / 2;
498498
if (rest < 0)rest = 0;
499499

500-
if ((smcount > 0) && (bands > 0)) {
501-
smh = (double)(((double)smcount)/((double)bands));
500+
if ((smcount > 0) && (bars > 0)) {
501+
smh = (double)(((double)smcount)/((double)bars));
502502
}
503503

504504

505505
#ifdef DEBUG
506-
printw("hoyde: %d bredde: %d bands:%d bandbredde: %d rest: %d\n",
506+
printw("height: %d width: %d bars:%d bar width: %d rest: %d\n",
507507
w,
508-
h, bands, bw, rest);
508+
h, bars, bw, rest);
509509
#endif
510510

511511
//output: start noncurses mode
512512
if (om == 3) init_terminal_noncurses(col, bgcol, w, h, bw);
513513

514-
double freqconst = log10((float)lowcf / (float)highcf) / ((float)1 / ((float)bands + (float)1) - 1);
514+
double freqconst = log10((float)lowcf / (float)highcf) / ((float)1 / ((float)bars + (float)1) - 1);
515515

516516
//freqconst = -2;
517517

518518
// process: calculate cutoff frequencies
519-
for (n = 0; n < bands + 1; n++) {
520-
fc[n] = highcf * pow(10, freqconst * (-1) + ((((float)n + 1) / ((float)bands + 1)) *
519+
for (n = 0; n < bars + 1; n++) {
520+
fc[n] = highcf * pow(10, freqconst * (-1) + ((((float)n + 1) / ((float)bars + 1)) *
521521
freqconst)); //decided to cut it at 10k, little interesting to hear above
522522
fr[n] = fc[n] / (audio.rate /
523523
2); //remember nyquist!, pr my calculations this should be rate/2 and nyquist freq in M/2 but testing shows it is not... or maybe the nq freq is in M/4
524524
lcf[n] = fr[n] * (M /
525-
4); //lfc stores the lower cut frequency foo each band in the fft out buffer
525+
4); //lfc stores the lower cut frequency foo each bar in the fft out buffer
526526
if (n != 0) {
527527
hcf[n - 1] = lcf[n] - 1;
528528
if (lcf[n] <= lcf[n - 1])lcf[n] = lcf[n - 1] +
@@ -539,7 +539,7 @@ Options:\n\
539539
}
540540

541541
// process: weigh signal to frequencies
542-
for (n = 0; n < bands;
542+
for (n = 0; n < bars;
543543
n++)k[n] = pow(fc[n],0.85) * ((float)height/(M*4000)) * smooth[(int)floor(((double)n) * smh)];
544544

545545
cont = 1;
@@ -556,8 +556,12 @@ Options:\n\
556556
sens -= 10;
557557
break;
558558
case 67: // key right
559+
bw++;
560+
cont = 0;
559561
break;
560562
case 68: // key left
563+
if (bw > 1) bw--;
564+
cont = 0;
561565
break;
562566
case 'm':
563567
if (mode == modes) {
@@ -576,7 +580,7 @@ Options:\n\
576580
return EXIT_SUCCESS;
577581
}
578582

579-
583+
if (cont == 0) break;
580584

581585
#ifdef DEBUG
582586
//clear();
@@ -593,8 +597,8 @@ Options:\n\
593597
if (audio.audio_out[i] < lpeak) lpeak = audio.audio_out[i];
594598
} else in[i] = 0;
595599
}
596-
peak[bands] = (hpeak + abs(lpeak));
597-
if (peak[bands] == 0)sleep++;
600+
peak[bars] = (hpeak + abs(lpeak));
601+
if (peak[bars] == 0)sleep++;
598602
else sleep = 0;
599603

600604
// process: if input was present for the last 5 seconds apply FFT to it
@@ -604,7 +608,7 @@ Options:\n\
604608
fftw_execute(p);
605609

606610
// process: separate frequency bands
607-
for (o = 0; o < bands; o++) {
611+
for (o = 0; o < bars; o++) {
608612
flastd[o] = f[o]; //saving last value for drawing
609613
peak[o] = 0;
610614

@@ -641,26 +645,26 @@ Options:\n\
641645

642646
int m_y, de;
643647
if (mode == 3) {
644-
for (z = 0; z < bands; z++) { // waves
648+
for (z = 0; z < bars; z++) { // waves
645649
f[z] = f[z] / 1.25;
646650
if (f[z] < 0.125)f[z] = 0.125;
647651
for (m_y = z - 1; m_y >= 0; m_y--) {
648652
de = z - m_y;
649653
f[m_y] = max(f[z] - pow(de, 2), f[m_y]);
650654
}
651-
for (m_y = z + 1; m_y < bands; m_y++) {
655+
for (m_y = z + 1; m_y < bars; m_y++) {
652656
de = m_y - z;
653657
f[m_y] = max(f[z] - pow(de, 2), f[m_y]);
654658
}
655659
}
656660
} else if (monstercat > 0) {
657-
for (z = 0; z < bands; z++) {
661+
for (z = 0; z < bars; z++) {
658662
if (f[z] < 0.125)f[z] = 0.125;
659663
for (m_y = z - 1; m_y >= 0; m_y--) {
660664
de = z - m_y;
661665
f[m_y] = max(f[z] / pow(monstercat, de), f[m_y]);
662666
}
663-
for (m_y = z + 1; m_y < bands; m_y++) {
667+
for (m_y = z + 1; m_y < bars; m_y++) {
664668
de = m_y - z;
665669
f[m_y] = max(f[z] / pow(monstercat, de), f[m_y]);
666670
}
@@ -669,7 +673,7 @@ Options:\n\
669673

670674
// process [smoothing]: falloff
671675
if (g > 0) {
672-
for (o = 0; o < bands; o++) {
676+
for (o = 0; o < bars; o++) {
673677
temp = f[o];
674678

675679
if (temp < flast[o]) {
@@ -687,7 +691,7 @@ Options:\n\
687691

688692
// process [smoothing]: integral
689693
if (integral > 0) {
690-
for (o = 0; o < bands; o++) {
694+
for (o = 0; o < bars; o++) {
691695
fmem[o] = fmem[o] * integral + f[o];
692696
f[o] = fmem[o];
693697

@@ -699,7 +703,7 @@ Options:\n\
699703
}
700704

701705
// zero values causes divided by zero segfault.
702-
for (o = 0; o < bands; o++) {
706+
for (o = 0; o < bars; o++) {
703707
if (f[o] < 1)f[o] = 1;
704708
}
705709

@@ -709,13 +713,13 @@ Options:\n\
709713
#ifndef DEBUG
710714
switch (om) {
711715
case 1:
712-
rc = draw_terminal_ncurses(virt, h, w, bands, bw, rest, f, flastd);
716+
rc = draw_terminal_ncurses(virt, h, w, bars, bw, bs, rest, f, flastd);
713717
break;
714718
case 2:
715719
rc = draw_terminal_bcircle(virt, h, w, f);
716720
break;
717721
case 3:
718-
rc = draw_terminal_noncurses(virt, h, w, bands, bw, rest, f, flastd);
722+
rc = draw_terminal_noncurses(virt, h, w, bars, bw, bs, rest, f, flastd);
719723
break;
720724
}
721725

example_files/config

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
## Configuration file for CAVA. Default values are commented out. Use either ; or # for commenting.
22

33
[general]
4-
; mode = normal # defines smoothing mode, can be normal, scientific or waves.
4+
; mode = normal # smoothing mode, can be normal, scientific or waves.
55
; framerate = 60 # Default: 60. Accepts only non-negative values.
66
; sensitivity = 100 # is sensitivity %. Accepts only non-negative values.
7-
; bars = 0 # defines the amount of bars. 0 sets it to auto (25 + fill up leftover space).
8-
; lower_cutoff_freq = 50 # defines the lower cutof frequency for the far left bar
9-
; higher_cutoff_freq = 10000 # defines the higher cutoff frequency for the far right bar. Note: there is a minimum total bandwith of 43Mhz x number of bars. Cava will automaticly increase the higher cuttoff if a too low band is specified.
7+
; bars = 0 # the number of bars. 0 sets it to auto (fil upp console).
8+
; bar_width = 3 # width of bars.
9+
; bar_spacing = 1 # space between bars.
10+
; lower_cutoff_freq = 50 # the lower cutof frequency for the far left bar
11+
; higher_cutoff_freq = 10000 # the higher cutoff frequency for the far right bar. Note: there is a minimum total bandwith of 43Mhz x number of bars. Cava will automaticly increase the higher cuttoff if a too low band is specified.
1012

1113

1214

output/terminal_ncurses.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void get_terminal_dim_ncurses(int *w, int *h) {
2727
}
2828

2929

30-
int draw_terminal_ncurses(int virt, int h, int w, int bands, int bw, int rest, int f[200], int flastd[200]) {
30+
int draw_terminal_ncurses(int virt, int h, int w, int bars, int bw, int bs, int rest, int f[200], int flastd[200]) {
3131

3232
int i, n, q;
33-
const wchar_t* bars[] = {L"\u2581", L"\u2582", L"\u2583", L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
33+
const wchar_t* bh[] = {L"\u2581", L"\u2582", L"\u2583", L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
3434

3535
// output: check if terminal has been resized
3636
if (virt != 0) {
@@ -41,20 +41,20 @@ int draw_terminal_ncurses(int virt, int h, int w, int bands, int bw, int rest, i
4141

4242
h = h - 1;
4343

44-
for (i = 0; i < bands; i++) {
44+
for (i = 0; i < bars; i++) {
4545

4646
if(f[i] > flastd[i]){//higher then last one
47-
if (virt == 0) for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvprintw((h - n), (i * bw) + q + i + rest, "%d",8);
48-
else for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvaddwstr((h - n), (i * bw) + q + i + rest, bars[7]);
47+
if (virt == 0) for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvprintw((h - n), i*bw + q + i*bs + rest, "%d",8);
48+
else for (n = flastd[i] / 8; n < f[i] / 8; n++) for (q = 0; q < bw; q++) mvaddwstr((h - n), i*bw + q + i*bs + rest, bh[7]);
4949
if (f[i] % 8 != 0) {
50-
if (virt == 0) for (q = 0; q < bw; q++) mvprintw( (h - n), (i * bw) + q + i + rest, "%d",(f[i] % 8) );
51-
else for (q = 0; q < bw; q++) mvaddwstr( (h - n), (i * bw) + q + i + rest, bars[(f[i] % 8) - 1]);
50+
if (virt == 0) for (q = 0; q < bw; q++) mvprintw( (h - n), i*bw + q + i*bs + rest, "%d",(f[i] % 8) );
51+
else for (q = 0; q < bw; q++) mvaddwstr( (h - n), i*bw + q + i*bs + rest, bh[(f[i] % 8) - 1]);
5252
}
5353
}else if(f[i] < flastd[i]){//lower then last one
54-
for (n = f[i] / 8; n < flastd[i]/8 + 1; n++) for (q = 0; q < bw; q++) mvaddstr( (h - n), (i*bw) + q + i + rest, " ");
54+
for (n = f[i] / 8; n < flastd[i]/8 + 1; n++) for (q = 0; q < bw; q++) mvaddstr( (h - n), i*bw + q + i*bs + rest, " ");
5555
if (f[i] % 8 != 0) {
56-
if (virt == 0) for (q = 0; q < bw; q++) mvprintw((h - f[i] / 8), (i * bw) + q + i + rest, "%d",(f[i] % 8) );
57-
else for (q = 0; q < bw; q++) mvaddwstr((h - f[i] / 8), (i * bw) + q + i + rest, bars[(f[i] % 8) - 1]);
56+
if (virt == 0) for (q = 0; q < bw; q++) mvprintw((h - f[i] / 8), i*bw + q + i*bs + rest, "%d",(f[i] % 8) );
57+
else for (q = 0; q < bw; q++) mvaddwstr((h - f[i] / 8), i*bw + q + i*bs + rest, bh[(f[i] % 8) - 1]);
5858
}
5959
}
6060
flastd[i] = f[i]; //memmory for falloff func

output/terminal_ncurses.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
int init_terminal_ncurses(int col, int bgcol);
33
void get_terminal_dim_ncurses(int *w, int *h);
4-
int draw_terminal_ncurses(int virt, int height, int width, int bands, int bw, int rest, int f[200], int flastd[200]);
4+
int draw_terminal_ncurses(int virt, int height, int width, int bars, int bw, int bs, int rest, int f[200], int flastd[200]);
55
void cleanup_terminal_ncurses(void);

output/terminal_noncurses.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void get_terminal_dim_noncurses(int *w, int *h) {
105105
}
106106

107107

108-
int draw_terminal_noncurses(int virt, int h, int w, int bands, int bw, int rest, int f[200], int flastd[200]) {
108+
int draw_terminal_noncurses(int virt, int h, int w, int bars, int bw, int bs, int rest, int f[200], int flastd[200]) {
109109
int c, move, n, o;
110110

111111
struct winsize dim;
@@ -126,7 +126,7 @@ int draw_terminal_noncurses(int virt, int h, int w, int bands, int bw, int rest,
126126
for (n = h - 2; n >= 0; n--) {
127127

128128
move = rest; //center adjustment
129-
for (o = 0; o < bands; o++) {
129+
for (o = 0; o < bars; o++) {
130130

131131
// output: draw and move to another one, check whether we're not too far
132132

@@ -153,7 +153,7 @@ int draw_terminal_noncurses(int virt, int h, int w, int bands, int bw, int rest,
153153

154154
} else move += bw; //no change, moving along
155155

156-
move++;//move to next bar
156+
move += bs;//move to next bar
157157
}
158158

159159
printf("\n");

output/terminal_noncurses.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
int init_terminal_noncurses(int col, int bgcol, int w, int h, int bw);
22
void get_terminal_dim_noncurses(int *w, int *h);
3-
int draw_terminal_noncurses(int virt, int height, int width, int bands, int bw, int rest, int f[200], int flastd[200]);
3+
int draw_terminal_noncurses(int virt, int height, int width, int bars, int bw, int bs, int rest, int f[200], int flastd[200]);
44
void cleanup_terminal_noncurses(void);

0 commit comments

Comments
 (0)