added noncurses output mode, (old printf version)

master
karlstav 11 years ago
parent 478cf4995a
commit 12c11a1232
  1. 50
      cava.c
  2. 2
      example_files/config
  3. 2
      output/terminal_ncurses.h
  4. 142
      output/terminal_noncurses.c
  5. 4
      output/terminal_noncurses.h
  6. 48
      output/terminal_printf.c
  7. 1
      output/terminal_printf.h

@ -21,6 +21,8 @@
#include <time.h>
#include <getopt.h>
#include <pthread.h>
#include "output/terminal_noncurses.h"
#include "output/terminal_noncurses.c"
#include "output/terminal_ncurses.h"
#include "output/terminal_ncurses.c"
#include "output/terminal_bcircle.h"
@ -44,9 +46,10 @@ struct termios oldtio, newtio;
int rc;
// general: cleanup
void cleanup()
void cleanup(void)
{
cleanup_terminal_ncurses();
cleanup_terminal_noncurses();
}
// general: handle signals
@ -99,7 +102,7 @@ int main(int argc, char **argv)
pthread_t p_thread;
int thr_id GCC_UNUSED;
char *inputMethod = (char *)iniparser_getstring(ini, "input:method", "alsa");
char *outputMethod = (char *)iniparser_getstring(ini, "output:method", "terminal");
char *outputMethod = (char *)iniparser_getstring(ini, "output:method", "ncurses");
char *modeString = (char *)iniparser_getstring(ini, "general:mode", "normal");
int im = 1;
int om = 1;
@ -153,7 +156,7 @@ Visualize audio input in terminal. \n\
Options:\n\
-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\
-i 'input method' method used for listening to audio, supports: 'alsa' and 'fifo'\n\
-o 'output method' method used for outputting processed data, supports: 'terminal' and 'circle'\n\
-o 'output method' method used for outputting processed data, supports: 'ncurses', 'noncurses' and 'circle'\n\
-d 'alsa device' name of alsa capture device (default 'hw:1,1')\n\
-p 'fifo path' path to fifo (default '/tmp/mpd.fifo')\n\
-c foreground color supported colors: red, green, yellow, magenta, cyan, white, blue, black (default: cyan)\n\
@ -259,8 +262,12 @@ Options:\n\
}
// validate: output method
if (strcmp(outputMethod, "terminal") == 0) om = 1;
if (strcmp(outputMethod, "ncurses") == 0) om = 1;
if (strcmp(outputMethod, "circle") == 0) om = 2;
if (strcmp(outputMethod, "noncurses") == 0) {
om = 3;
bgcol = 0;
}
if (om == 0) {
fprintf(stderr,
"output method %s is not supported, supported methods are: 'terminal', 'circle'\n",
@ -376,8 +383,15 @@ Options:\n\
//output: start ncurses mode
if (om == 1 || om == 2) {
init_terminal_ncurses(col, bgcol);
}
//output: start noncurses mode
if (om == 3) {
get_terminal_dim_noncurses(&w, &h);
init_terminal_noncurses(col, bgcol, w, h);
}
while (1) {//jumbing back to this loop means that you resized the screen
@ -398,7 +412,9 @@ Options:\n\
// output: get terminal's geometry
get_terminal_dim_ncurses(&w, &h);
if (om == 1 || om == 2) get_terminal_dim_ncurses(&w, &h);
if (om == 3) get_terminal_dim_noncurses(&w, &h);
if (bands > w / 2 - 1)bands = w / 2 -
@ -441,8 +457,7 @@ Options:\n\
fr[n] = fc[n] / (audio.rate /
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
lcf[n] = fr[n] * (M /
4); //lfc stores the lower cut frequency foo each band in the fft out buffer
4); //lfc stores the lower cut frequency foo each band in the fft out buffer
if (n != 0) {
hcf[n - 1] = lcf[n] - 1;
if (lcf[n] <= lcf[n - 1])lcf[n] = lcf[n - 1] +
@ -450,26 +465,24 @@ Options:\n\
hcf[n - 1] = lcf[n] - 1;
}
#ifdef DEBUG
if (n != 0) {
#ifdef DEBUG
if (n != 0) {
printw("%d: %f -> %f (%d -> %d) \n", n, fc[n - 1], fc[n], lcf[n - 1],
hcf[n - 1]);
hcf[n - 1]);
}
#endif
}
// process: weigh signal to frequencies
for (n = 0; n < bands;
n++)k[n] = pow(fc[n],0.85) * ((float)height/(M*4000)) * smooth[(int)floor(((double)n) * smh)];
// general: main loop
while (1) {
// general: keyboard controls
ch = getch();
// general: keyboard controls
if (om == 1 || om == 2) ch = getch();
switch (ch) {
case 65: // key up
sens += 10;
@ -626,6 +639,9 @@ Options:\n\
case 2:
rc = draw_terminal_bcircle(virt, h, w, f);
break;
case 3:
rc = draw_terminal_noncurses(virt, h, w, bands, bw, rest, f, flastd);
break;
}
if (rc == -1) break; //terminal has been resized breaking to recalibrating values

@ -11,7 +11,7 @@
; source = hw:1,1 # ALSA device or FIFO path.
[output]
; method = terminal # may be terminal or circle.
; method = ncurses # may be ncurses, noncurses or circle.
[color]
# supported colors are: red, green, yellow, magenta, cyan, white, blue, black.

@ -1,7 +1,5 @@
int init_terminal_ncurses(int col, int bgcol);
int get_terminal_height_ncurses(void);
int get_terminal_width_ncurses(void);
void get_terminal_dim_ncurses(int *w, int *h);
int draw_terminal_ncurses(int virt, int height, int width, int bands, int bw, int rest, int f[200], int flastd[200]);
void cleanup_terminal_ncurses(void);

@ -0,0 +1,142 @@
#include <locale.h>
#include <wchar.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int setecho(int fd, int onoff) {
struct termios t;
if (tcgetattr(fd, &t) == -1)
return -1;
if (onoff == 0)
t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
else
t.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
if (tcsetattr(fd, TCSANOW, &t) == -1)
return -1;
return 0;
}
int init_terminal_noncurses(int col, int bgcol, int w, int h) {
int n, i;
col += 30;
bgcol += 40;
system("setterm -cursor off");
system("setterm -blank 0");
// output: reset console
printf("\033[0m\n");
system("clear");
printf("\033[%dm", col); //setting color
printf("\033[1m"); //setting "bright" color mode, looks cooler... I think
if (bgcol != 0)
printf("\033[%dm", bgcol);
{
for (n = (h); n >= 0; n--) {
for (i = 0; i < w; i++) {
printf(" "); //setting backround color
}
printf("\n");
}
printf("\033[%dA", h); //moving cursor back up
}
setecho(STDIN_FILENO, 0);
return 0;
}
void get_terminal_dim_noncurses(int *w, int *h) {
struct winsize dim;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &dim);
*h = (int)dim.ws_row;
*w = (int)dim.ws_col;
system("clear"); //clearing in case of resieze
}
int draw_terminal_noncurses(int virt, int h, int w, int bands, int bw, int rest, int f[200], int flastd[200]) {
int c, move, i, n, o;
struct winsize dim;
// output: check if terminal has been resized
if (virt != 0) {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &dim);
if ( (int)dim.ws_row != h || (int)dim.ws_col != w) {
return -1;
}
}
for (n = h - 2; n >= 0; n--) {
move = rest; //center adjustment
for (o = 0; o < bands; o++) {
// output: draw and move to another one, check whether we're not too far
if (f[o] != flastd[o]) { //change?
if (move != 0)printf("\033[%dC", move);
move = 0;
c = f[o] - n * 8;
for (i = 0; i < bw; i++) {
if (c < 1) {
printf(" "); //blank
} else if (c > 7) {
if (virt == 0) printf("%d", 8); // block tty
else printf("\u2588"); //block
} else {
if (virt == 0) printf("%d", c); // fractioned block tty
else printf("%lc", L'\u2580' + c); // fractioned block vt
}
}
} else move += bw; //no change, moving along
move++;//move to next bar
}
printf("\n");
}
printf("\033[%dA", h);
return 0;
}
// general: cleanup
void cleanup_terminal_noncurses(void)
{
setecho(STDIN_FILENO, 1);
printf("\033[0m\n");
system("setfont /usr/share/consolefonts/Lat2-Fixed16.psf.gz >/dev/null 2>&1");
system("setterm -cursor on");
system("setterm -blank 10");
system("clear");
}

@ -0,0 +1,4 @@
int init_terminal_noncurses(int col, int bgcol, int w, int h);
void get_terminal_dim_noncurses(int *w, int *h);
int draw_terminal_noncurses(int virt, int height, int width, int bands, int bw, int rest, int f[200], int flastd[200]);
void cleanup_terminal_noncurses(void);

@ -1,48 +0,0 @@
int draw_terminal_printf(int height, int width, int bars) {
for (n = (height - 1); n >= 0; n--) {
o = 0;
move = rest / 2; //center adjustment
for (i = 0; i < width; i++) {
// output: check if we're already at the next bar
if (i != 0 && i % bw == 0) {
o++;
if (o < bands)move++;
}
// output: draw and move to another one, check whether we're not too far
if (o < bands) {
if (f[o] - n < 0.125) { //blank
if (matrix[i][n] != 0) { //change?
if (move != 0)printf("\033[%dC", move);
move = 0;
printf(" ");
} else move++; //no change, moving along
matrix[i][n] = 0;
} else if (f[o] - n > 1) { //color
if (matrix[i][n] != 1) { //change?
if (move != 0)printf("\033[%dC", move);
move = 0;
printf("\u2588");
} else move++; //no change, moving along
matrix[i][n] = 1;
} else { //top color, finding fraction
if (move != 0)printf("\033[%dC", move);
move = 0;
c = ((((f[o] - (float)n) - 0.125) / 0.875 * 7) + 1);
if (0 < c && c < 8) {
if (virt == 0)printf("%d", c);
else printf("%lc", L'\u2580' + c);
} else printf(" ");
matrix[i][n] = 2;
}
}
}
printf("\n");
}
printf("\033[%dA", height);
}

@ -1 +0,0 @@
int draw_terminal_printf(int height, int width, int bars);
Loading…
Cancel
Save