styled again with astyle

master
karlstav 11 years ago
parent d04c6ef2fb
commit 2921e98659
  1. 174
      cava.c

174
cava.c

@ -10,6 +10,7 @@
__typeof__ (b) _b = (b); \ __typeof__ (b) _b = (b); \
_a > _b ? _a : _b; }) _a > _b ? _a : _b; })
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
@ -54,7 +55,7 @@ music(void* data)
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_CAPTURE , 0) < 0)) if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_CAPTURE , 0) < 0))
printf("error opening stream: %s\n", snd_strerror(err) ); printf("error opening stream: %s\n", snd_strerror(err) );
else if(debug == 1) { else if (debug == 1) {
printf("open stream succes\n"); printf("open stream succes\n");
} }
snd_pcm_hw_params_alloca(&params);//assembling params snd_pcm_hw_params_alloca(&params);//assembling params
@ -81,9 +82,9 @@ music(void* data)
snd_pcm_hw_params_get_format(params, snd_pcm_hw_params_get_format(params,
(snd_pcm_format_t * )&val); //getting actual format (snd_pcm_format_t * )&val); //getting actual format
//convverting result to number of bits //convverting result to number of bits
if(val < 6)format = 16; if (val < 6)format = 16;
else if(val > 5 && val < 10)format = 24; else if (val > 5 && val < 10)format = 24;
else if(val > 9)format = 32; else if (val > 9)format = 32;
snd_pcm_hw_params_get_rate( params, &rate, &dir); //getting rate snd_pcm_hw_params_get_rate( params, &rate, &dir); //getting rate
@ -95,22 +96,22 @@ music(void* data)
radj = format / 4; //adjustments for interleaved radj = format / 4; //adjustments for interleaved
ladj = format / 8; ladj = format / 8;
o = 0; o = 0;
while(1) { while (1) {
err = snd_pcm_readi(handle, buffer, frames); err = snd_pcm_readi(handle, buffer, frames);
if (err == -EPIPE) { if (err == -EPIPE) {
/* EPIPE means overrun */ /* EPIPE means overrun */
if(debug == 1) { if (debug == 1) {
fprintf(stderr, "overrun occurred\n"); fprintf(stderr, "overrun occurred\n");
} }
snd_pcm_prepare(handle); snd_pcm_prepare(handle);
} else if (err < 0) { } else if (err < 0) {
if(debug == 1) { if (debug == 1) {
fprintf(stderr, "error from read: %s\n", fprintf(stderr, "error from read: %s\n",
snd_strerror(err)); snd_strerror(err));
} }
} else if (err != (int)frames) { } else if (err != (int)frames) {
if(debug == 1) { if (debug == 1) {
fprintf(stderr, "short read, read %d %d frames\n", err, (int)frames); fprintf(stderr, "short read, read %d %d frames\n", err, (int)frames);
} }
} }
@ -126,20 +127,20 @@ music(void* data)
2)); //using the 10 upper bits this whould give me a vert res of 1024, enough... 2)); //using the 10 upper bits this whould give me a vert res of 1024, enough...
lo = ((buffer[i + (radj) - 2] >> 6)); lo = ((buffer[i + (radj) - 2] >> 6));
if (lo < 0)lo = lo + 4; if (lo < 0)lo = lo + 4;
if(tempr >= 0)tempr = tempr + lo; if (tempr >= 0)tempr = tempr + lo;
if(tempr < 0)tempr = tempr - lo; if (tempr < 0)tempr = tempr - lo;
//other channel //other channel
templ = (buffer[i + (ladj) - 1] << 2); templ = (buffer[i + (ladj) - 1] << 2);
lo = (buffer[i + (ladj) - 2] >> 6); lo = (buffer[i + (ladj) - 2] >> 6);
if(lo < 0)lo = lo + 4; if (lo < 0)lo = lo + 4;
if(templ >= 0)templ = templ + lo; if (templ >= 0)templ = templ + lo;
else templ = templ - lo; else templ = templ - lo;
//adding channels and storing it int the buffer //adding channels and storing it int the buffer
shared[o] = (tempr + templ) / 2; shared[o] = (tempr + templ) / 2;
o++; o++;
if(o == M - 1)o = 0; if (o == M - 1)o = 0;
//shifing ringbuffer one to the left, this ended up using to much cpu.. //shifing ringbuffer one to the left, this ended up using to much cpu..
//for(o=0;o<M-1;o++) shared[o]=shared[o+1]; //for(o=0;o<M-1;o++) shared[o]=shared[o+1];
@ -174,21 +175,21 @@ fifomusic(void* data)
lo = ( buf[size - 4 * q ] >> 6); lo = ( buf[size - 4 * q ] >> 6);
//printf("%d\n",buf[4*q+1]); //printf("%d\n",buf[4*q+1]);
if (lo < 0)lo = lo + 4; if (lo < 0)lo = lo + 4;
if(tempr >= 0)tempr = tempr + lo; if (tempr >= 0)tempr = tempr + lo;
if(tempr < 0)tempr = tempr - lo; if (tempr < 0)tempr = tempr - lo;
templ = ( buf[size - 4 * q - 3] << 2); templ = ( buf[size - 4 * q - 3] << 2);
lo = ( buf[size - 4 * q - 2] >> 6); lo = ( buf[size - 4 * q - 2] >> 6);
if(lo < 0)lo = lo + 4; if (lo < 0)lo = lo + 4;
if(templ >= 0)templ = templ + lo; if (templ >= 0)templ = templ + lo;
else templ = templ - lo; else templ = templ - lo;
//printf("%d\n",((tempr+templ)/2)); //printf("%d\n",((tempr+templ)/2));
shared[n] = (tempr + templ) / 2; shared[n] = (tempr + templ) / 2;
n++; n++;
if(n == M - 1)n = 0; if (n == M - 1)n = 0;
} }
} }
fclose(fp); fclose(fp);
@ -255,9 +256,9 @@ int main(int argc, char **argv)
case 'i': case 'i':
im = 0; im = 0;
input = optarg; input = optarg;
if(strcmp(input, "alsa") == 0) im = 1; if (strcmp(input, "alsa") == 0) im = 1;
if(strcmp(input, "fifo") == 0) im = 2; if (strcmp(input, "fifo") == 0) im = 2;
if(im == 0) { if (im == 0) {
printf("input method %s not supprted, supported methods are: 'alsa' and 'fifo'\n", printf("input method %s not supprted, supported methods are: 'alsa' and 'fifo'\n",
input); input);
exit(1); exit(1);
@ -280,15 +281,15 @@ int main(int argc, char **argv)
case 'c': case 'c':
col = 0; col = 0;
color = optarg; color = optarg;
if(strcmp(color, "black") == 0) col = 30; if (strcmp(color, "black") == 0) col = 30;
if(strcmp(color, "red") == 0) col = 31; if (strcmp(color, "red") == 0) col = 31;
if(strcmp(color, "green") == 0) col = 32; if (strcmp(color, "green") == 0) col = 32;
if(strcmp(color, "yellow") == 0) col = 33; if (strcmp(color, "yellow") == 0) col = 33;
if(strcmp(color, "blue") == 0) col = 34; if (strcmp(color, "blue") == 0) col = 34;
if(strcmp(color, "magenta") == 0) col = 35; if (strcmp(color, "magenta") == 0) col = 35;
if(strcmp(color, "cyan") == 0) col = 36; if (strcmp(color, "cyan") == 0) col = 36;
if(strcmp(color, "white") == 0) col = 37; if (strcmp(color, "white") == 0) col = 37;
if(col == 0) { if (col == 0) {
printf("color %s not suprted\n", color); printf("color %s not suprted\n", color);
exit(1); exit(1);
} }
@ -296,15 +297,15 @@ int main(int argc, char **argv)
case 'C': case 'C':
bgcol = 0; bgcol = 0;
color = optarg; color = optarg;
if(strcmp(color, "black") == 0) bgcol = 40; if (strcmp(color, "black") == 0) bgcol = 40;
if(strcmp(color, "red") == 0) bgcol = 41; if (strcmp(color, "red") == 0) bgcol = 41;
if(strcmp(color, "green") == 0) bgcol = 42; if (strcmp(color, "green") == 0) bgcol = 42;
if(strcmp(color, "yellow") == 0) bgcol = 43; if (strcmp(color, "yellow") == 0) bgcol = 43;
if(strcmp(color, "blue") == 0) bgcol = 44; if (strcmp(color, "blue") == 0) bgcol = 44;
if(strcmp(color, "magenta") == 0) bgcol = 45; if (strcmp(color, "magenta") == 0) bgcol = 45;
if(strcmp(color, "cyan") == 0) bgcol = 46; if (strcmp(color, "cyan") == 0) bgcol = 46;
if(strcmp(color, "white") == 0) bgcol = 47; if (strcmp(color, "white") == 0) bgcol = 47;
if(bgcol == 0) { if (bgcol == 0) {
printf("color %s not suprted\n", color); printf("color %s not suprted\n", color);
exit(1); exit(1);
} }
@ -328,24 +329,24 @@ int main(int argc, char **argv)
n = 0; n = 0;
if(im == 1) { if (im == 1) {
//**watintg for audio to be ready**// //**watintg for audio to be ready**//
thr_id = pthread_create(&p_thread, NULL, music, thr_id = pthread_create(&p_thread, NULL, music,
(void*)device); //starting alsamusic listner (void*)device); //starting alsamusic listner
while (format == -1 || rate == -1) { while (format == -1 || rate == -1) {
usleep(1000); usleep(1000);
n++; n++;
if(n > 2000) { if (n > 2000) {
if(debug == 1) if (debug == 1)
printf("could not get rate and or format, problems with audoi thread? quiting...\n"); printf("could not get rate and or format, problems with audoi thread? quiting...\n");
exit(1); exit(1);
} }
} }
if(debug == 1)printf("got format: %d and rate %d\n", format, rate); if (debug == 1)printf("got format: %d and rate %d\n", format, rate);
} }
if(im == 2) { if (im == 2) {
thr_id = pthread_create(&p_thread, NULL, fifomusic, thr_id = pthread_create(&p_thread, NULL, fifomusic,
(void*)path); //starting fifomusic listner (void*)path); //starting fifomusic listner
rate = 44100; rate = 44100;
@ -364,13 +365,13 @@ int main(int argc, char **argv)
} else bands = fixedbands; } else bands = fixedbands;
if(bands > (int)w.ws_col / 2 - 1)bands = (int)w.ws_col / 2 - if (bands > (int)w.ws_col / 2 - 1)bands = (int)w.ws_col / 2 -
1; //handle for user setting to many bars 1; //handle for user setting to many bars
height = (int)w.ws_row - 1; height = (int)w.ws_row - 1;
width = (int)w.ws_col - bands - 1; width = (int)w.ws_col - bands - 1;
int matrix[width][height]; int matrix[width][height];
for(i = 0; i < width; i++) { for (i = 0; i < width; i++) {
for(n = 0; n < height; n++) { for (n = 0; n < height; n++) {
matrix[i][n] = 0; matrix[i][n] = 0;
} }
} }
@ -380,21 +381,22 @@ int main(int argc, char **argv)
2.5); //calculating gravity 2.5); //calculating gravity
//if no bands are selected it tries to padd the default 20 if there is extra room //if no bands are selected it tries to padd the default 20 if there is extra room
if(autoband == 1) bands = bands + (((w.ws_col) - (bw * bands + bands - 1)) / if (autoband == 1) bands = bands + (((w.ws_col) - (bw * bands + bands - 1)) /
(bw + 1)); (bw + 1));
width = (int)w.ws_col - bands - 1; width = (int)w.ws_col - bands - 1;
//checks if there is stil extra room, will use this to center //checks if there is stil extra room, will use this to center
rest = (((w.ws_col) - (bw * bands + bands - 1))); rest = (((w.ws_col) - (bw * bands + bands - 1)));
if(rest < 0)rest = 0; if (rest < 0)rest = 0;
if(debug == 1) printf("hoyde: %d bredde: %d bands:%d bandbredde: %d rest: %d\n", if (debug == 1)
printf("hoyde: %d bredde: %d bands:%d bandbredde: %d rest: %d\n",
(int)w.ws_row, (int)w.ws_row,
(int)w.ws_col, bands, bw, rest); (int)w.ws_col, bands, bw, rest);
//**calculating cutof frequencies**/ //**calculating cutof frequencies**/
for(n = 0; n < bands + 1; n++) { for (n = 0; n < bands + 1; n++) {
fc[n] = 10000 * pow(10, -2.37 + ((((float)n + 1) / ((float)bands + 1)) * fc[n] = 10000 * pow(10, -2.37 + ((((float)n + 1) / ((float)bands + 1)) *
2.37)); //decided to cut it at 10k, little interesting to hear above 2.37)); //decided to cut it at 10k, little interesting to hear above
fr[n] = fc[n] / (rate / fr[n] = fc[n] / (rate /
@ -402,14 +404,14 @@ int main(int argc, char **argv)
lcf[n] = fr[n] * (M / lcf[n] = fr[n] * (M /
4); //lfc stores the lower cut frequency fro each band in the fft out buffer 4); //lfc stores the lower cut frequency fro each band in the fft out buffer
if(n != 0) { if (n != 0) {
hcf[n - 1] = lcf[n] - 1; hcf[n - 1] = lcf[n] - 1;
if(lcf[n] <= lcf[n - 1])lcf[n] = lcf[n - 1] + if (lcf[n] <= lcf[n - 1])lcf[n] = lcf[n - 1] +
1; //pushing the spectrum up if the expe function gets "clumped" 1; //pushing the spectrum up if the expe function gets "clumped"
hcf[n - 1] = lcf[n] - 1; hcf[n - 1] = lcf[n] - 1;
} }
if(debug == 1 && n != 0) { if (debug == 1 && n != 0) {
printf("%d: %f -> %f (%d -> %d) \n", n, fc[n - 1], fc[n], lcf[n - 1], printf("%d: %f -> %f (%d -> %d) \n", n, fc[n - 1], fc[n], lcf[n - 1],
hcf[n - 1]); hcf[n - 1]);
} }
@ -417,7 +419,7 @@ int main(int argc, char **argv)
//exit(1); //exit(1);
//constants to weigh signal to frequency //constants to weigh signal to frequency
for(n = 0; n < bands; for (n = 0; n < bands;
n++)k[n] = ((float)height * pow(log(lcf[n] + 1), n++)k[n] = ((float)height * pow(log(lcf[n] + 1),
2 + ((float)bands / 75))) / (1024 * (M / 2 + ((float)bands / 75))) / (1024 * (M /
16)); // the log(lcf[n]) is because higher frequencys are usally lower ine effect in music 16)); // the log(lcf[n]) is because higher frequencys are usally lower ine effect in music
@ -425,7 +427,7 @@ int main(int argc, char **argv)
//**preparing screen**// //**preparing screen**//
if(debug == 0) { if (debug == 0) {
virt = system("setfont cava.psf >/dev/null 2>&1"); virt = system("setfont cava.psf >/dev/null 2>&1");
system("setterm -cursor off"); system("setterm -cursor off");
system("setterm -blank 0"); system("setterm -blank 0");
@ -437,7 +439,7 @@ int main(int argc, char **argv)
printf("\033[%dm", col); //setting color printf("\033[%dm", col); //setting color
printf("\033[1m"); //setting "bright" color mode, looks cooler... I think printf("\033[1m"); //setting "bright" color mode, looks cooler... I think
if(bgcol != 0) if (bgcol != 0)
printf("\033[%dm", bgcol); printf("\033[%dm", bgcol);
{ {
for (n = (height); n >= 0; n--) { for (n = (height); n >= 0; n--) {
@ -460,15 +462,15 @@ int main(int argc, char **argv)
//**checkint if terminal windows has been resized**// //**checkint if terminal windows has been resized**//
if(virt != 0) { if (virt != 0) {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if( ((int)w.ws_row - 1) != height || ((int)w.ws_col - bands - 1) != width) { if ( ((int)w.ws_row - 1) != height || ((int)w.ws_col - bands - 1) != width) {
break; break;
} }
} }
if(debug == 1) { if (debug == 1) {
system("clear"); system("clear");
} }
@ -478,10 +480,10 @@ int main(int argc, char **argv)
lpeak = 0; lpeak = 0;
hpeak = 0; hpeak = 0;
for (i = 0; i < (2 * (M / 2 + 1)); i++) { for (i = 0; i < (2 * (M / 2 + 1)); i++) {
if(i < M) { if (i < M) {
in[i] = shared[i]; in[i] = shared[i];
if(shared[i] > hpeak) hpeak = shared[i]; if (shared[i] > hpeak) hpeak = shared[i];
if(shared[i] < lpeak) lpeak = shared[i]; if (shared[i] < lpeak) lpeak = shared[i];
} else in[i] = 0; } else in[i] = 0;
// if(debug==1) printf("%d %f\n",i,in[i]); // if(debug==1) printf("%d %f\n",i,in[i]);
} }
@ -495,7 +497,7 @@ int main(int argc, char **argv)
fftw_execute(p); //applying FFT to signal fftw_execute(p); //applying FFT to signal
//seperating freq bands //seperating freq bands
for(o = 0; o < bands; o++) { for (o = 0; o < bands; o++) {
peak[o] = 0; peak[o] = 0;
//getting peaks //getting peaks
@ -506,11 +508,11 @@ int main(int argc, char **argv)
peak[o] = peak[o] / i; //getting average peak[o] = peak[o] / i; //getting average
temp = peak[o] * k[o] * ((float)sens / temp = peak[o] * k[o] * ((float)sens /
100); //multiplying with k and adjusting to sens settings 100); //multiplying with k and adjusting to sens settings
if(temp > height)temp = height; //just in case if (temp > height)temp = height; //just in case
//**falloff function**// //**falloff function**//
if(temp < flast[o]) { if (temp < flast[o]) {
f[o] = fpeak[o] - (g * fall[o] * fall[o]); f[o] = fpeak[o] - (g * fall[o] * fall[o]);
fall[o]++; fall[o]++;
} else if (temp >= flast[o]) { } else if (temp >= flast[o]) {
@ -529,8 +531,8 @@ int main(int argc, char **argv)
flast[o] = f[o]; //memmory for falloff func flast[o] = f[o]; //memmory for falloff func
if(f[o] < 0.125)f[o] = 0.125; if (f[o] < 0.125)f[o] = 0.125;
if(debug == 1) { if (debug == 1) {
printf("%d: f:%f->%f (%d->%d)peak:%f adjpeak: %f \n", o, fc[o], fc[o + 1], printf("%d: f:%f->%f (%d->%d)peak:%f adjpeak: %f \n", o, fc[o], fc[o + 1],
lcf[o], hcf[o], peak[o], f[o]); lcf[o], hcf[o], peak[o], f[o]);
} }
@ -539,7 +541,7 @@ int main(int argc, char **argv)
//if(debug==1){ printf("topp overall alltime:%f \n",sum);} //if(debug==1){ printf("topp overall alltime:%f \n",sum);}
} else { //**if in sleep mode wait and continiue**// } else { //**if in sleep mode wait and continiue**//
if(debug == 1)printf("no sound detected for 3 sec, going to sleep mode\n"); if (debug == 1)printf("no sound detected for 3 sec, going to sleep mode\n");
//for (i=0;i<200;i++)flast[i]=0; //zeroing memory no more nesceseary after faloff on pauses //for (i=0;i<200;i++)flast[i]=0; //zeroing memory no more nesceseary after faloff on pauses
//pthread_cancel(thr_id);// this didnt work to well, killing sound thread //pthread_cancel(thr_id);// this didnt work to well, killing sound thread
usleep(1 * 1000000); //wait 1 sec, then check sound again. usleep(1 * 1000000); //wait 1 sec, then check sound again.
@ -551,7 +553,7 @@ int main(int argc, char **argv)
float m_o = 64 / bands; float m_o = 64 / bands;
for (z = 0; z < bands; z++) { for (z = 0; z < bands; z++) {
f[z] = f[z] * sm / smooth[(int)floor(z * m_o)]; f[z] = f[z] * sm / smooth[(int)floor(z * m_o)];
if(f[z] < 0.125)f[z] = 0.125; if (f[z] < 0.125)f[z] = 0.125;
for (m_y = z - 1; m_y >= 0; m_y--) { for (m_y = z - 1; m_y >= 0; m_y--) {
f[m_y] = max(f[z] / pow(2, z - m_y), f[m_y]); f[m_y] = max(f[z] / pow(2, z - m_y), f[m_y]);
} }
@ -569,59 +571,59 @@ int main(int argc, char **argv)
for (i = 0; i < width; i++) { for (i = 0; i < width; i++) {
//next bar? make a space //next bar? make a space
if(i != 0 && i % bw == 0) { if (i != 0 && i % bw == 0) {
o++; o++;
if(o < bands)move++; if (o < bands)move++;
} }
//draw color or blank or move+1 //draw color or blank or move+1
if(o < bands) { //watch so it doesnt draw to far if (o < bands) { //watch so it doesnt draw to far
if(f[o] - n < 0.125) { //blank if (f[o] - n < 0.125) { //blank
if(matrix[i][n] != 0) { //change? if (matrix[i][n] != 0) { //change?
if(move != 0)printf("\033[%dC", move); if (move != 0)printf("\033[%dC", move);
move = 0; move = 0;
printf(" "); printf(" ");
} else move++; //no change, moving along } else move++; //no change, moving along
matrix[i][n] = 0; matrix[i][n] = 0;
} else if (f[o] - n > 1) { //color } else if (f[o] - n > 1) { //color
if(matrix[i][n] != 1) { //change? if (matrix[i][n] != 1) { //change?
if(move != 0)printf("\033[%dC", move); if (move != 0)printf("\033[%dC", move);
move = 0; move = 0;
printf("\u2588"); printf("\u2588");
} else move++; //no change, moving along } else move++; //no change, moving along
matrix[i][n] = 1; matrix[i][n] = 1;
} else { //top color, finding fraction } else { //top color, finding fraction
if(move != 0)printf("\033[%dC", move); if (move != 0)printf("\033[%dC", move);
move = 0; move = 0;
c = ((((f[o] - (float)n) - 0.125) / 0.875 * 7) + 1); c = ((((f[o] - (float)n) - 0.125) / 0.875 * 7) + 1);
switch (c) { switch (c) {
case 1: case 1:
if(virt == 0)printf("1"); if (virt == 0)printf("1");
else printf("\u2581"); else printf("\u2581");
break; break;
case 2: case 2:
if(virt == 0)printf("2"); if (virt == 0)printf("2");
else printf("\u2582"); else printf("\u2582");
break; break;
case 3: case 3:
if(virt == 0)printf("3"); if (virt == 0)printf("3");
else printf("\u2583"); else printf("\u2583");
break; break;
case 4: case 4:
if(virt == 0)printf("4"); if (virt == 0)printf("4");
else printf("\u2584"); else printf("\u2584");
break; break;
case 5: case 5:
if(virt == 0)printf("5"); if (virt == 0)printf("5");
else printf("\u2585"); else printf("\u2585");
break; break;
case 6: case 6:
if(virt == 0)printf("6"); if (virt == 0)printf("6");
else printf("\u2586"); else printf("\u2586");
break; break;
case 7: case 7:
if(virt == 0)printf("7"); if (virt == 0)printf("7");
else printf("\u2587"); else printf("\u2587");
break; break;
default: default:

Loading…
Cancel
Save