-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnausea.c
610 lines (528 loc) · 11.7 KB
/
nausea.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include <err.h>
#include <complex.h>
#include <curses.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
#include <fftw3.h>
#define LEN(x) (sizeof (x) / sizeof *(x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#include "config.h"
static unsigned msec = 1000 / 25; /* 25 fps */
static unsigned nsamples = 44100 * 2; /* stereo */
static wchar_t chbar = CHBAR;
static wchar_t chpeak = CHPEAK;
static wchar_t chpoint = CHPOINT;
static char *fname = "/tmp/audio.fifo";
static char *argv0;
static int colors;
static int peaks;
static int keep;
static int left;
static int bounce;
static int die;
struct frame {
int fd;
size_t width, width_old;
size_t height;
int *peak;
int *sav;
#define PK_HIDDEN -1
int16_t *buf;
unsigned *res;
double *in;
ssize_t gotsamples;
complex *out;
fftw_plan plan;
};
/* Supported visualizations:
* 1 -- spectrum
* 2 -- wave
* 2 -- fountain */
static void draw_spectrum(struct frame *fr);
static void draw_wave(struct frame *fr);
static void draw_fountain(struct frame *fr);
static struct visual {
void (* draw)(struct frame *fr);
int dft; /* needs the DFT */
int color; /* supports colors */
} visuals[] = {
{draw_spectrum, 1, 1},
{draw_wave, 0, 0},
{draw_fountain, 1, 1},
};
static int vidx = 0; /* default visual index */
/* We assume the screen is 100 pixels in the y direction.
* To follow the curses convention (0, 0) is in the top left
* corner of the screen. The `min' and `max' values correspond
* to percentages. To illustrate this the [0, 20) range gives
* the top 20% of the screen to the color red. These values
* are scaled automatically in the draw() routine to the actual
* size of the terminal window. */
static struct color_range {
short pair; /* index in the color table */
int min; /* min % */
int max; /* max % */
short fg; /* foreground color */
short bg; /* background color */
/* these are calculated internally, do not set */
int scaled_min;
int scaled_max;
/* These are the colors that you can use:
* COLOR_BLACK
* COLOR_RED
* COLOR_GREEN
* COLOR_YELLOW
* COLOR_BLUE
* COLOR_MAGENTA
* COLOR_CYAN
* COLOR_WHITE */
} color_ranges[] = {
{ 1, 0, 20, COLOR_RED, -1, 0, 0 },
{ 2, 20, 60, COLOR_BLUE, -1, 0, 0 },
{ 3, 60, 100, COLOR_CYAN, -1, 0, 0 }
};
static void
clearall(struct frame *fr)
{
unsigned i;
fr->gotsamples = 0;
for (i = 0; i < nsamples / 2; i++) {
fr->in[i] = 0.;
fr->out[i] = 0. + 0. * I;
}
}
static void
init(struct frame *fr)
{
fr->fd = open(fname, O_RDONLY | O_NONBLOCK);
if (fr->fd == -1)
err(1, "open %s", fname);
fr->buf = malloc(nsamples * sizeof(int16_t));
fr->res = malloc(nsamples / 2 * sizeof(unsigned));
fr->in = malloc(nsamples / 2 * sizeof(double));
fr->out = malloc(nsamples / 2 * sizeof(complex));
clearall(fr);
fr->plan = fftw_plan_dft_r2c_1d(nsamples / 2, fr->in, fr->out,
FFTW_ESTIMATE);
}
static void
done(struct frame *fr)
{
fftw_destroy_plan(fr->plan);
free(fr->out);
free(fr->in);
free(fr->res);
free(fr->buf);
free(fr->peak);
free(fr->sav);
close(fr->fd);
}
static void
update(struct frame *fr)
{
ssize_t n;
unsigned i;
n = read(fr->fd, fr->buf, nsamples * sizeof(int16_t));
if (n == -1) {
clearall(fr);
return;
}
fr->gotsamples = n / sizeof(int16_t);
for (i = 0; i < nsamples / 2; i++) {
fr->in[i] = 0.;
if (i < fr->gotsamples / 2) {
/* average the two channels */
fr->in[i] = fr->buf[i * 2 + 0];
fr->in[i] += fr->buf[i * 2 + 1];
fr->in[i] /= 2.;
}
}
/* compute the DFT if needed */
if (visuals[vidx].dft)
fftw_execute(fr->plan);
}
static void
setcolor(int on, int y)
{
unsigned i;
struct color_range *cr;
if (!colors)
return;
for (i = 0; i < LEN(color_ranges); i++) {
cr = &color_ranges[i];
if (y >= cr->scaled_min && y < cr->scaled_max) {
if (on)
attron(COLOR_PAIR(cr->pair));
else
attroff(COLOR_PAIR(cr->pair));
return;
}
}
}
static void
draw_spectrum(struct frame *fr)
{
unsigned i, j;
unsigned freqs_per_col;
struct color_range *cr;
/* read dimensions to catch window resize */
fr->width = COLS;
fr->height = LINES;
if (peaks) {
/* change in width needs new peaks */
if (fr->width != fr->width_old) {
fr->peak = realloc(fr->peak, fr->width * sizeof(int));
for (i = 0; i < fr->width; i++)
fr->peak[i] = PK_HIDDEN;
fr->width_old = fr->width;
}
}
if (colors) {
/* scale color ranges */
for (i = 0; i < LEN(color_ranges); i++) {
cr = &color_ranges[i];
cr->scaled_min = cr->min * fr->height / 100;
cr->scaled_max = cr->max * fr->height / 100;
}
}
/* take most of the left part of the band */
#define BANDCUT 0.5
freqs_per_col = (nsamples / 2) / fr->width * BANDCUT;
#undef BANDCUT
/* scale each frequency to screen */
#define BARSCALE 0.2
for (i = 0; i < nsamples / 2; i++) {
/* complex absolute value */
fr->res[i] = cabs(fr->out[i]);
/* normalize it */
fr->res[i] /= (nsamples / 2);
/* scale it */
fr->res[i] *= fr->height * BARSCALE;
}
#undef BARSCALE
erase();
attron(A_BOLD);
for (i = 0; i < fr->width; i++) {
size_t bar_height = 0;
size_t ybegin, yend;
/* compute bar height */
for (j = 0; j < freqs_per_col; j++)
bar_height += fr->res[i * freqs_per_col + j];
bar_height /= freqs_per_col;
/* we draw from top to bottom */
ybegin = fr->height - MIN(bar_height, fr->height);
yend = fr->height;
/* If the current freq reaches the peak, the peak is
* updated to that height, else it drops by one line. */
if (peaks) {
if (fr->peak[i] >= ybegin)
fr->peak[i] = ybegin;
else
fr->peak[i]++;
/* this freq died out */
if (fr->height == ybegin && fr->peak[i] == ybegin)
fr->peak[i] = PK_HIDDEN;
}
/* output bars */
for (j = ybegin; j < yend; j++) {
move(j, i);
setcolor(1, j);
printw("%lc", chbar);
setcolor(0, j);
}
/* output peaks */
if (peaks && fr->peak[i] != PK_HIDDEN) {
move(fr->peak[i], i);
setcolor(1, fr->peak[i]);
printw("%lc", chpeak);
setcolor(0, fr->peak[i]);
}
}
attroff(A_BOLD);
refresh();
}
static void
draw_wave(struct frame *fr)
{
unsigned i, j;
unsigned samples_per_col;
double pt_pos, pt_pos_prev = 0, pt_pos_mid;
/* read dimensions to catch window resize */
fr->width = COLS;
fr->height = LINES;
erase();
/* not enough samples */
if (fr->gotsamples < fr->width)
return;
samples_per_col = (fr->gotsamples / 2) / fr->width;
attron(A_BOLD);
for (i = 0; i < fr->width; i++) {
size_t y;
/* compute point position */
pt_pos = 0;
for (j = 0; j < samples_per_col; j++)
pt_pos += fr->in[i * samples_per_col + j];
pt_pos /= samples_per_col;
/* normalize it */
pt_pos /= INT16_MAX;
/* scale it */
#define PTSCALE 0.8
pt_pos *= (fr->height / 2) * PTSCALE;
#undef PTSCALE
/* output points */
y = fr->height / 2 + pt_pos; /* centering */
move(y, i);
printw("%lc", chpoint);
/* Output a helper point by averaging with the previous
* position. This creates a nice effect. We don't care
* about overlaps with the current point. */
pt_pos_mid = (pt_pos_prev + pt_pos) / 2.0;
y = fr->height / 2 + pt_pos_mid; /* centering */
move(y, i);
printw("%lc", chpoint);
pt_pos_prev = pt_pos;
}
attroff(A_BOLD);
refresh();
}
static void
draw_fountain(struct frame *fr)
{
unsigned i, j;
struct color_range *cr;
static int col = 0;
size_t bar_height = 0;
unsigned freqs;
/* read dimensions to catch window resize */
fr->width = COLS;
fr->height = LINES;
/* change in width needs new keep state */
if (fr->width != fr->width_old) {
fr->sav = realloc(fr->sav, fr->width * sizeof(int));
for (i = 0; i < fr->width; i++)
fr->sav[i] = fr->height;
fr->width_old = fr->width;
}
if (colors) {
/* scale color ranges */
for (i = 0; i < LEN(color_ranges); i++) {
cr = &color_ranges[i];
cr->scaled_min = cr->min * fr->height / 100;
cr->scaled_max = cr->max * fr->height / 100;
}
}
/* scale each frequency to screen */
#define BARSCALE 0.3
for (i = 0; i < nsamples / 2; i++) {
/* complex absolute value */
fr->res[i] = cabs(fr->out[i]);
/* normalize it */
fr->res[i] /= (nsamples / 2);
/* scale it */
fr->res[i] *= fr->height * BARSCALE;
}
#undef BARSCALE
/* take most of the left part of the band */
#define BANDCUT 0.5
freqs = (nsamples / 2) * BANDCUT;
#undef BANDCUT
/* compute bar height */
for (j = 0; j < freqs; j++)
bar_height += fr->res[j];
bar_height /= freqs;
erase();
attron(A_BOLD);
/* ensure we are inside the frame */
col %= fr->width;
for (i = 0; i < fr->width; i++) {
size_t ybegin, yend;
/* we draw from top to bottom */
if (i == col) {
ybegin = fr->height - MIN(bar_height, fr->height);
fr->sav[col] = ybegin;
} else {
if (keep)
ybegin = fr->sav[i];
else
ybegin = fr->sav[i]++;
}
yend = fr->height;
/* output bars */
for (j = ybegin; j < yend; j++) {
move(j, i);
setcolor(1, j);
printw("%lc", chbar);
setcolor(0, j);
}
}
/* current column bounces back */
if (bounce)
if (left)
if (col == 0)
left = 0;
else
col--;
else
if (col == fr->width - 1)
left = 1;
else
col++;
/* current column wraps around */
else
if (left)
col = (col == 0) ? fr->width - 1 : col - 1;
else
col = (col == fr->width - 1) ? 0 : col + 1;
attroff(A_BOLD);
refresh();
}
static void
initcolors(void)
{
unsigned i;
struct color_range *cr;
start_color();
for (i = 0; i < LEN(color_ranges); i++) {
cr = &color_ranges[i];
init_pair(cr->pair, cr->fg, cr->bg);
}
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-hcpklb] [-d num] [fifo]\n", argv0);
fprintf(stderr, "default fifo path is `/tmp/audio.fifo'\n");
exit(1);
}
int
main(int argc, char *argv[])
{
int c;
struct frame fr;
int vidx_prev;
int fd;
argv0 = argv[0];
while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0]))
switch (c) {
case 'd':
if (*++argv == NULL)
usage();
argc--;
switch (*argv[0]) {
case '1':
vidx = 0;
break;
case '2':
vidx = 1;
break;
case '3':
vidx = 2;
break;
}
break;
case 'c':
colors = 1;
break;
case 'p':
peaks = 1;
break;
case 'k':
keep = 1;
break;
case 'l':
left = 1;
break;
case 'b':
bounce = 1;
break;
case 'h':
/* fall-through */
default:
usage();
}
if (argc == 1)
fname = argv[0];
else if (argc > 1)
usage();
/* init frame context */
memset(&fr, 0, sizeof(fr));
init(&fr);
setlocale(LC_ALL, "");
/* init curses */
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
curs_set(FALSE); /* hide cursor */
timeout(msec);
use_default_colors();
if (colors && has_colors() == FALSE) {
endwin();
done(&fr);
errx(1, "your terminal does not support colors");
}
vidx_prev = vidx;
while (!die) {
switch (getch()) {
case 'q':
die = 1;
break;
case 'c':
if (has_colors() == TRUE)
colors = !colors;
break;
case 'p':
peaks = !peaks;
break;
case 'k':
keep = !keep;
break;
case 'l':
left = !left;
break;
case 'b':
bounce = !bounce;
break;
case '1':
vidx = 0;
break;
case '2':
vidx = 1;
break;
case '3':
vidx = 2;
break;
case 'n':
case KEY_RIGHT:
vidx = vidx == (LEN(visuals) - 1) ? 0 : vidx + 1;
break;
case 'N':
case KEY_LEFT:
vidx = vidx == 0 ? LEN(visuals) - 1 : vidx - 1;
break;
}
/* detect visualization change */
if (vidx != vidx_prev)
fr.width_old = 0;
/* only spectrum and fountain support colors */
if (colors && visuals[vidx].color)
initcolors();
else
(void)use_default_colors();
update(&fr);
visuals[vidx].draw(&fr);
vidx_prev = vidx;
}
endwin(); /* restore terminal */
done(&fr); /* destroy context */
return (0);
}