ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
visualization.c
Go to the documentation of this file.
2
7#include <libplatform/memory.h>
8#include <options/options.h>
9
10#include <math.h>
11#include <stdio.h>
12#include <string.h>
13
15
22typedef struct {
23 float frequency;
24 unsigned char red;
25 unsigned char green;
26 unsigned char blue;
28
30static size_t write_position[AUDIO_VISUALIZATION_SOURCE_COUNT];
31static static_mutex_t history_mutex = STATIC_MUTEX_INIT;
32
33static void visualization_color_for_frequency(float centroid_hz,
34 unsigned char *red,
35 unsigned char *green,
36 unsigned char *blue) {
37 static const waveform_color_stop_t stops[] = {
38 {45.0f, 255, 72, 176}, {100.0f, 255, 54, 106},
39 {220.0f, 255, 104, 72}, {350.0f, 190, 86, 220},
40 {700.0f, 74, 112, 255}, {1400.0f, 55, 190, 255},
41 {2800.0f, 55, 226, 180}, {5200.0f, 150, 244, 92},
42 {10000.0f, 255, 224, 76}, {18000.0f, 255, 246, 150},
43 };
44 const size_t stop_count = sizeof(stops) / sizeof(stops[0]);
45 if (centroid_hz <= stops[0].frequency) {
46 *red = stops[0].red;
47 *green = stops[0].green;
48 *blue = stops[0].blue;
49 return;
50 }
51 for (size_t i = 1; i < stop_count; i++) {
52 if (centroid_hz <= stops[i].frequency) {
53 float low = logf(stops[i - 1].frequency);
54 float high = logf(stops[i].frequency);
55 float amount = (logf(centroid_hz) - low) / (high - low);
56 *red = (unsigned char)lrintf(
57 (float)stops[i - 1].red +
58 amount * ((float)stops[i].red - stops[i - 1].red));
59 *green = (unsigned char)lrintf(
60 (float)stops[i - 1].green +
61 amount * ((float)stops[i].green - stops[i - 1].green));
62 *blue = (unsigned char)lrintf(
63 (float)stops[i - 1].blue +
64 amount * ((float)stops[i].blue - stops[i - 1].blue));
65 return;
66 }
67 }
68 *red = stops[stop_count - 1].red;
69 *green = stops[stop_count - 1].green;
70 *blue = stops[stop_count - 1].blue;
71}
72
73void visualization_color_for_level(float frequency_hz, float level,
74 int color_mode, unsigned char *red,
75 unsigned char *green, unsigned char *blue) {
76 visualization_color_for_frequency(frequency_hz, red, green, blue);
77 float brightness = 0.5f + 0.5f * level;
78 float white_mix = 0.0f;
80 brightness = 0.24f + 0.76f * level;
82 brightness = 0.62f + 0.38f * level;
83 white_mix = 0.10f;
84 }
85 *red = (unsigned char)lrintf(
86 fminf(255.0f, *red * brightness + 255.0f * white_mix));
87 *green = (unsigned char)lrintf(
88 fminf(255.0f, *green * brightness + 255.0f * white_mix));
89 *blue = (unsigned char)lrintf(
90 fminf(255.0f, *blue * brightness + 255.0f * white_mix));
91}
92
94 const float *samples, size_t count) {
95 if (!samples || count % 2)
96 return;
97 float mono[480];
98 for (size_t offset = 0; offset < count;) {
99 size_t frames = (count - offset) / 2;
100 if (frames > 480)
101 frames = 480;
102 for (size_t i = 0; i < frames; i++)
103 mono[i] =
104 samples[offset + i * 2] * 0.5f + samples[offset + i * 2 + 1] * 0.5f;
105 audio_visualization_submit(source, mono, frames);
106 offset += frames * 2;
107 }
108}
109
111 const float *samples, size_t count) {
112 if ((!GET_OPTION(waveform) && !GET_OPTION(fft)) || !samples ||
115 return;
116 static_mutex_lock_named(&history_mutex, "audio_visualization_history");
117 size_t position = write_position[source];
118 for (size_t i = 0; i < count; i++) {
119 float sample = samples[i];
120 history[source][position] =
121 isfinite(sample) ? fmaxf(-1.0f, fminf(1.0f, sample)) : 0.0f;
122 position = (position + 1) % VISUALIZATION_HISTORY;
123 }
124 write_position[source] = position;
125 static_mutex_unlock(&history_mutex);
126}
127
129 float *samples, size_t count) {
130 if (!samples)
131 return;
134 memset(samples, 0, count * sizeof(float));
135 return;
136 }
137 static_mutex_lock_named(&history_mutex, "audio_visualization_history");
138 size_t available_count =
140 size_t output_offset = count - available_count;
141 for (size_t i = 0; i < count; i++)
142 samples[i] = 0.0f;
143 size_t first_source = source;
144 size_t last_source = source;
146 first_source = AUDIO_VISUALIZATION_SOURCE_MIC;
149 first_source = AUDIO_VISUALIZATION_SOURCE_MIC;
151 }
152 for (size_t current = first_source; current <= last_source; current++) {
153 size_t start =
154 (write_position[current] + VISUALIZATION_HISTORY - available_count) %
156 for (size_t i = 0; i < available_count; i++)
157 samples[output_offset + i] +=
158 history[current][(start + i) % VISUALIZATION_HISTORY];
159 }
160 static_mutex_unlock(&history_mutex);
161 for (size_t i = 0; i < count; i++)
162 samples[i] = fmaxf(-1.0f, fminf(1.0f, samples[i]));
163}
164
165static char *visualization_color(char *out, const unsigned char *color,
166 bool background, int color_mode) {
167 if (color_mode == COLOR_MODE_16) {
168 uint8_t index = rgb_to_16color(color[0], color[1], color[2]);
169 return background ? append_16color_bg(out, index)
171 }
172 if (color_mode == COLOR_MODE_256) {
173 uint8_t index = rgb_to_256color(color[0], color[1], color[2]);
174 return background ? append_256color_bg(out, index)
176 }
177 return background ? append_truecolor_bg(out, color[0], color[1], color[2])
178 : append_truecolor_fg(out, color[0], color[1], color[2]);
179}
180
181/* Encode the same visualization grid for native terminals and browser
182 * rasterization. Half-block grids have two vertical samples per terminal row.
183 */
184char *visualization_encode(const char *cells, const unsigned char *colors,
185 bool column_colors, unsigned int width,
186 unsigned int height, bool use_color, bool flip_x,
187 bool flip_y) {
188 int mode = GET_OPTION(render_mode);
192 use_color = use_color && color_mode != COLOR_MODE_NONE;
193 palette_type_t palette_type = (palette_type_t)GET_OPTION(palette_type);
194 const palette_def_t *builtin = get_builtin_palette(palette_type);
195 const char *palette =
196 palette_type == PALETTE_CUSTOM
197 ? GET_OPTION(palette_custom)
198 : (builtin ? builtin->chars : PALETTE_CHARS_STANDARD);
199 if (!palette || !*palette)
200 palette = PALETTE_CHARS_STANDARD;
201 /*
202 * Audio visualizations already contain their final intensity characters.
203 * Do not run the expensive image glyph matcher/palette path for them: it
204 * adds work without adding information and can collapse the visualization
205 * frame rate when --renderer glyph is enabled. Keep the selected renderer
206 * for normal video frames; this fast path only applies to these generated
207 * visualization grids.
208 */
209 bool use_visualization_glyphs = GET_OPTION(renderer) == ASCII_RENDERER_GLYPH;
210 const char *characters[256];
211 size_t lengths[256], count = 0;
212 if (!use_visualization_glyphs) {
213 for (const char *p = palette; *p && count < 256;) {
214 uint32_t codepoint;
215 int length = utf8_decode((const uint8_t *)p, &codepoint);
216 if (length <= 0)
217 break;
218 characters[count] = p;
219 lengths[count++] = (size_t)length;
220 p += length;
221 }
222 }
223 bool half = mode == RENDER_MODE_HALF_BLOCK;
224 unsigned int rows = half ? height / 2 : height;
225 size_t capacity = (size_t)width * rows * 64 + rows + 1;
226 char *frame = SAFE_MALLOC(capacity, char *);
227 if (!frame)
228 return NULL;
229 size_t used = 0;
230 for (unsigned int y = 0; y < rows; y++) {
231 for (unsigned int x = 0; x < width; x++) {
232 unsigned int sx = flip_x ? width - x - 1 : x;
233 unsigned int sy = half ? y * 2 : y;
234 unsigned int top = flip_y ? height - sy - 1 : sy;
235 size_t cell = (size_t)top * width + sx;
236 const unsigned char *color = colors + (column_colors ? sx : cell) * 3;
237 char ch = cells[cell];
238 if (half) {
239 unsigned int bottom = flip_y ? height - sy - 2 : sy + 1;
240 size_t lower = (size_t)bottom * width + sx;
241 const unsigned char *lower_color =
242 colors + (column_colors ? sx : lower) * 3;
243 bool upper_on = ch != ' ', lower_on = cells[lower] != ' ';
244 if (use_color) {
245 const unsigned char black[3] = {0};
246 char *out = visualization_color(
247 frame + used, upper_on ? color : black, false, color_mode);
248 out = visualization_color(out, lower_on ? lower_color : black, true,
249 color_mode);
250 memcpy(out, "▀\033[0m", sizeof("▀\033[0m") - 1);
251 used = (size_t)(out - frame) + sizeof("▀\033[0m") - 1;
252 } else {
253 const char *glyph =
254 upper_on ? (lower_on ? "█" : "▀") : (lower_on ? "▄" : " ");
255 size_t length = strlen(glyph);
256 memcpy(frame + used, glyph, length);
257 used += length;
258 }
259 } else if (ch != ' ') {
260 char *out = frame + used;
261 if (use_color)
262 out = visualization_color(out, color, mode == RENDER_MODE_BACKGROUND,
263 color_mode);
264 if (mode == RENDER_MODE_BACKGROUND && use_color) {
265 *out++ = ' ';
266 } else if (use_visualization_glyphs) {
267 *out++ = ch;
268 } else if (count) {
269 unsigned int luminance =
270 2126u * color[0] + 7152u * color[1] + 722u * color[2];
271 size_t index = (luminance * (count - 1) + 1275000u) / 2550000u;
272 memcpy(out, characters[index], lengths[index]);
273 out += lengths[index];
274 } else {
275 *out++ = ch;
276 }
277 if (use_color) {
278 memcpy(out, "\033[0m", 4);
279 out += 4;
280 }
281 used = (size_t)(out - frame);
282 } else {
283 frame[used++] = ch;
284 }
285 }
286 if (y + 1 < rows) {
287 frame[used++] = '\r';
288 frame[used++] = '\n';
289 }
290 }
291 frame[used] = '\0';
292 return frame;
293}
ANSI escape sequence utilities and fast color code generation.
Platform initialization and static synchronization helpers.
int color_mode
Definition fft.c:26
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
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
char * append_16color_fg(char *dst, uint8_t color_index)
Definition ansi.c:321
char * append_16color_bg(char *dst, uint8_t color_index)
Definition ansi.c:337
char * append_256color_bg(char *dst, uint8_t index)
Definition ansi.c:242
char * append_truecolor_fg(char *dst, uint8_t r, uint8_t g, uint8_t b)
Definition ansi.c:141
uint8_t rgb_to_16color(uint8_t r, uint8_t g, uint8_t b)
Definition ansi.c:353
char * append_256color_fg(char *dst, uint8_t index)
Definition ansi.c:235
uint8_t rgb_to_256color(uint8_t r, uint8_t g, uint8_t b)
Definition ansi.c:250
char * append_truecolor_bg(char *dst, uint8_t r, uint8_t g, uint8_t b)
Definition ansi.c:145
unsigned int uint32_t
#define SAFE_MALLOC(size, cast)
unsigned char uint8_t
#define GET_OPTION(field)
Definition options.h:1339
#define COLOR_MODE_TRUECOLOR
24-bit truecolor mode
Definition options.h:229
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:223
#define COLOR_MODE_16
16-color mode (alias)
Definition options.h:225
#define COLOR_MODE_NONE
Monochrome mode.
Definition options.h:224
#define COLOR_MODE_256
256-color mode (alias)
Definition options.h:227
const palette_def_t * get_builtin_palette(palette_type_t type)
Get built-in palette definition.
Definition palette.c:45
palette_type_t
Built-in palette type enumeration.
Definition palette.h:96
#define PALETTE_CHARS_STANDARD
Standard ASCII palette character string.
Definition palette.h:179
const char * chars
Character sequence (ordered from dark to light).
Definition palette.h:134
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
#define STATIC_MUTEX_INIT
int utf8_decode(const uint8_t *s, uint32_t *codepoint)
Decode a UTF-8 sequence to a Unicode codepoint.
Definition utf8.c:28
🔢 Mathematical Utility Functions
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
ASCII Palette Management for Video-to-ASCII Conversion.
static bool size_t index
Definition queries.h:10
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
Validated luminance palette used to turn brightness into characters.
Definition palette.h:130
Static mutex structure for global mutexes requiring static initialization.
Definition common/sync.h:51
waveform color stop keeps the values that move through the subsystem: frequency, red,...
🔤 UTF-8 Encoding and Decoding Utilities
@ ASCII_RENDERER_GLYPH
void audio_visualization_submit(audio_visualization_source_t source, const float *samples, size_t count)
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 audio_visualization_submit_stereo(audio_visualization_source_t source, const float *samples, size_t count)
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
@ AUDIO_VISUALIZATION_SOURCE_MEDIA
@ AUDIO_VISUALIZATION_SOURCE_LOCAL_MIX
@ AUDIO_VISUALIZATION_SOURCE_COUNT
@ AUDIO_VISUALIZATION_SOURCE_MIC
@ AUDIO_VISUALIZATION_SOURCE_REMOTE
#define AUDIO_VISUALIZATION_COLOR_BRIGHTER
#define AUDIO_VISUALIZATION_COLOR_DARKER
#define VISUALIZATION_HISTORY