This commit is contained in:
Your Name 2024-03-24 09:37:10 +03:00
parent 7ad4f70d75
commit ad955f0592
4 changed files with 135 additions and 81 deletions

View File

@ -3,7 +3,8 @@
.asoundrc (Должно быть в ~/) .asoundrc (Должно быть в ~/)
Нужно поменять rockchipes8316c, на вашу карту (aplay -L) Нужно поменять rockchipes8316c, на вашу карту (aplay -L)
cc spark.c -lm -s -o spark
modprobe snd-aloop modprobe snd-aloop
cc spark_ncurses.c -lm -lncurses -s -ospark
chmod +x audio_vis.sh chmod +x audio_vis.sh
./audio_vis.sh ./audio_vis.sh

View File

@ -1,4 +1,2 @@
#!/bin/sh #!/bin/sh
cols=180 arecord --rate 16000 --buffer-size=1024 -D loopout -MNqc 1 -f S24_LE -t raw | hexdump -e '/4 " %d"' | tr -d '*' | ./spark
arecord --rate 48000 --buffer-size=2048 -D loopout -N -q -c 1 -f S24_LE -t raw | hexdump -e '/4 " %d"' | tr -d '*' | tr -d '-' | xargs -n $cols ./spark

77
spark.c
View File

@ -1,77 +0,0 @@
#include <stdio.h>
#include <float.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double min = 90000;
double max = 3000000;
int parse_double(const char *str, double *res) {
char *ptr;
*res = strtod(str, &ptr);
if (*ptr)
return 0;
return 1;
}
void print(double diff, double val) {
int lvl = (int)round((val - min + 1) / diff * 7);
if (lvl < 0)
lvl = 0;
else if (lvl > 7)
lvl = 7;
putchar('\xe2');
putchar('\x96');
putchar('\x81' + lvl);
}
int main(int argc, char **argv) {
argv++;
argc--;
if (argc == 0) {
fprintf(stderr, "spark: missing operands\n");
return 1;
}
size_t as = argc / 2;
double *a = malloc((as + 1) * sizeof(double));
if (a == NULL)
return 1;
size_t j = 0;
for (size_t i = 0; i < as; i++) {
double val = 0;
if (!parse_double(argv[j], &val)) {
j++;
continue;
}
double val2 = 0;
if (!parse_double(argv[j + 1], &val)) {
j++;
continue;
}
a[i] = (val + val2) / 2;
j++;
}
double diff = max - min + 1;
if (diff < 1)
diff = 1;
for (size_t i = 0; i < as; i++)
print(diff, a[i]);
putchar('\r');
free(a);
return 0;
}

132
spark_ncurses.c Normal file
View File

@ -0,0 +1,132 @@
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <math.h>
#define NARGS 4096
#define ARG_SIZE 50
#define MAX 1000000
#define MIN 50000
int args;
double values[NARGS + 1];
struct winsize ws;
int parse_double(const char *str, double *res) {
char *ptr;
*res = strtod(str, &ptr);
if (*ptr)
return 0;
return 1;
}
int add_value(const char *str) {
if (args >= sizeof(values) / sizeof(double) || args > ws.ws_col)
return 1;
double val = 0;
if (!parse_double(str, &val))
return 1;
values[args] = val;
args++;
return 0;
}
void stdin_read(void) {
char arg[ARG_SIZE + 1];
char *p = arg;
fflush(stdin);
while (1) {
int c = getchar();
if (c == EOF)
return;
else if (c == ' ' && p != arg) {
*p = '\0';
p = arg;
if (add_value(arg))
break;
}
else {
*p = c;
if (p + 1 == arg + sizeof(arg))
break;
p++;
}
}
}
void mvprint(int y, int x, int lvl) {
char *fmt = "@";
if (lvl < 4) {
fmt = "/";
if (y > ws.ws_row / 2)
fmt = "\\";
}
else if (lvl - 4 <= 2)
fmt = "+";
mvprintw(y, x, "%s", fmt);
}
void print(double value, double diff, int col) {
size_t m_screen = ws.ws_row / 2;
if (value >= 0) {
int lvl = (int)round((value - MIN + 1) / diff * 2);
mvprint(m_screen - lvl, col, lvl);
}
else {
int lvl = (int)round(((value * -1) - MIN + 1) / diff * 2);
mvprint(m_screen + lvl, col, lvl);
}
}
void sig_handler(int sig) {
(void)sig;
endwin();
exit(0);
}
int main(void) {
signal(SIGINT, sig_handler);
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
return 1;
initscr();
curs_set(0);
timeout(0);
double diff = MAX - MIN + 1;
if (diff < 1)
diff = 1;
while (1) {
args = 0;
stdin_read();
clear();
for (int i = 1; i < args - 1; i++)
print((values[i - 1] + values[i] + values[i + 1]) / 4, diff, i);
refresh();
}
}