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.c
Go to the documentation of this file.
2
4
5#include <options/options.h>
6#include <libplatform/memory.h>
7
8#include <math.h>
9#include <string.h>
10
11static float waveform_spectral_centroid(const float *samples, size_t sample_count, size_t center) {
12 const size_t window_size = 1024;
13 const size_t half_window = window_size / 2;
14 size_t start = center > half_window ? center - half_window : 0;
15 if (start + window_size > sample_count)
16 start = sample_count - window_size;
17
18 /* A zero-crossing estimate is sufficient for the decorative color ramp
19 * and is linear in the window size. The previous nine resonator passes
20 * made every waveform column do substantially more work than necessary. */
21 size_t crossings = 0;
22 for (size_t i = 1; i < window_size; i++) {
23 float previous = samples[start + i - 1];
24 float current = samples[start + i];
25 if ((previous < 0.0f && current >= 0.0f) || (previous >= 0.0f && current < 0.0f))
26 crossings++;
27 }
28 return 22.0f * (float)crossings;
29}
30
31char *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) {
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
🔢 Mathematical Utility Functions
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
🔤 String Manipulation and Shell Escaping Utilities
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_t
@ AUDIO_VISUALIZATION_SOURCE_MIX
#define MAX_TERMINAL_WIDTH
#define VISUALIZATION_HISTORY
#define MAX_TERMINAL_HEIGHT
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 waveform.c:31