ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
waveform.h File Reference

Go to the source code of this file.

Functions

char * audio_visualization_render_waveform (unsigned int width, unsigned int height, audio_visualization_source_t source, bool use_color, int color_mode, bool flip_x, bool flip_y)

Function Documentation

◆ audio_visualization_render_waveform()

char * audio_visualization_render_waveform ( unsigned int width,
unsigned int height,
audio_visualization_source_t source,
bool use_color,
int color_mode,
bool flip_x,
bool flip_y )

Definition at line 31 of file waveform.c.

31 {
33 return NULL;
34
35 /* Keep a fixed one-second view so new audio enters on the right smoothly. */
36 const size_t sample_count = VISUALIZATION_HISTORY;
37 float *samples = SAFE_MALLOC(sample_count * sizeof(float), float *);
38 if (!samples)
39 return NULL;
40 audio_visualization_read(source, samples, sample_count);
41
42 const unsigned int grid_height = GET_OPTION(render_mode) == RENDER_MODE_HALF_BLOCK ? height * 2 : height;
43 const unsigned int center = grid_height / 2;
44 const unsigned int half_height = grid_height - 1 - center;
45 const size_t cell_count = (size_t)width * grid_height;
46 char *cells = SAFE_MALLOC(cell_count, char *);
47 if (!cells) {
48 SAFE_FREE(samples);
49 return NULL;
50 }
51 memset(cells, ' ', cell_count);
52 unsigned char palette[MAX_TERMINAL_WIDTH][3] = {{0}};
53
54 float positive_peak = 0.0f;
55 float negative_peak = 0.0f;
56 size_t x = 0;
57
58 for (size_t i = 0; i < sample_count; i++) {
59 float sample = samples[i];
60 positive_peak = fmaxf(positive_peak, sample);
61 negative_peak = fmaxf(negative_peak, -sample);
62
63 size_t next_bucket = ((i + 1) * width) / sample_count;
64 if (next_bucket != x || i + 1 == sample_count) {
65 size_t begin = x * sample_count / width;
66 size_t end = (x + 1) * sample_count / width;
67 size_t bucket_center = begin + (end - begin) / 2;
68 float centroid = waveform_spectral_centroid(samples, sample_count, bucket_center);
69 /* A gentle square-root scale keeps speech visible without flattening loud peaks. */
70 float level = sqrtf(fminf(fmaxf(positive_peak, negative_peak), 1.0f));
71 visualization_color_for_level(centroid, level, color_mode, &palette[x][0], &palette[x][1], &palette[x][2]);
72 unsigned int positive_amplitude = (unsigned int)lrintf(sqrtf(fminf(positive_peak, 1.0f)) * (float)half_height);
73 unsigned int negative_amplitude = (unsigned int)lrintf(sqrtf(fminf(negative_peak, 1.0f)) * (float)half_height);
74 if (positive_amplitude > half_height)
75 positive_amplitude = half_height;
76 if (negative_amplitude > half_height)
77 negative_amplitude = half_height;
78 unsigned int top = center - positive_amplitude;
79 unsigned int bottom = center + negative_amplitude;
80 for (unsigned int y = top; y <= bottom; y++) {
81 char ch = (y == top || y == bottom) ? '+' : '#';
82 cells[(size_t)y * width + x] = ch;
83 }
84
85 /* Advance to the next time slice in the left-to-right history. */
86 positive_peak = 0.0f;
87 negative_peak = 0.0f;
88 x = next_bucket;
89 }
90 }
91
92 char *frame = visualization_encode(cells, &palette[0][0], true, width, grid_height, use_color, flip_x, flip_y);
94 SAFE_FREE(samples);
95 return frame;
96}
int color_mode
Definition fft.c:26
char cells[(512 *256 *2)]
Definition fft.c:22
audio_visualization_source_t source
Definition fft.c:25
unsigned int height
Definition fft.c:24
terminal_render_mode_t render_mode
Definition fft.c:27
unsigned int width
Definition fft.c:24
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define GET_OPTION(field)
Definition options.h:1339
char * visualization_encode(const char *cells, const unsigned char *colors, bool column_colors, unsigned int width, unsigned int height, bool use_color, bool flip_x, bool flip_y)
void visualization_color_for_level(float frequency_hz, float level, int color_mode, unsigned char *red, unsigned char *green, unsigned char *blue)
void audio_visualization_read(audio_visualization_source_t source, float *samples, size_t count)
@ AUDIO_VISUALIZATION_SOURCE_MIX
#define MAX_TERMINAL_WIDTH
#define VISUALIZATION_HISTORY
#define MAX_TERMINAL_HEIGHT

References audio_visualization_read(), AUDIO_VISUALIZATION_SOURCE_MIX, cells, color_mode, GET_OPTION, height, MAX_TERMINAL_HEIGHT, MAX_TERMINAL_WIDTH, render_mode, SAFE_FREE, SAFE_MALLOC, source, visualization_color_for_level(), visualization_encode(), VISUALIZATION_HISTORY, and width.

Referenced by mirror_render_audio_visualization().