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_internal.h File Reference

Go to the source code of this file.

Macros

#define VISUALIZATION_HISTORY   48000
#define MAX_TERMINAL_WIDTH   512
#define MAX_TERMINAL_HEIGHT   256

Functions

void visualization_color_for_level (float frequency_hz, float level, int color_mode, unsigned char *red, unsigned char *green, unsigned char *blue)
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)

Macro Definition Documentation

◆ MAX_TERMINAL_HEIGHT

#define MAX_TERMINAL_HEIGHT   256

◆ MAX_TERMINAL_WIDTH

#define MAX_TERMINAL_WIDTH   512

◆ VISUALIZATION_HISTORY

Function Documentation

◆ visualization_color_for_level()

void visualization_color_for_level ( float frequency_hz,
float level,
int color_mode,
unsigned char * red,
unsigned char * green,
unsigned char * blue )

Definition at line 73 of file visualization.c.

75 {
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}
int color_mode
Definition fft.c:26
#define AUDIO_VISUALIZATION_COLOR_BRIGHTER
#define AUDIO_VISUALIZATION_COLOR_DARKER

References AUDIO_VISUALIZATION_COLOR_BRIGHTER, AUDIO_VISUALIZATION_COLOR_DARKER, and color_mode.

Referenced by audio_visualization_render_fft(), and audio_visualization_render_waveform().

◆ visualization_encode()

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 )

Definition at line 184 of file visualization.c.

187 {
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}
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int height
Definition fft.c:24
terminal_render_mode_t render_mode
Definition fft.c:27
unsigned int width
Definition fft.c:24
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_NONE
Monochrome mode.
Definition options.h:224
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
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
int utf8_decode(const uint8_t *s, uint32_t *codepoint)
Decode a UTF-8 sequence to a Unicode codepoint.
Definition utf8.c:28
static bool size_t index
Definition queries.h:10
static bool size_t gotel_query_t * out
Definition queries.h:10
Validated luminance palette used to turn brightness into characters.
Definition palette.h:130
@ ASCII_RENDERER_GLYPH

References ASCII_RENDERER_GLYPH, cells, palette_def_t::chars, color_mode, COLOR_MODE_AUTO, COLOR_MODE_NONE, COLOR_MODE_TRUECOLOR, colors, get_builtin_palette(), GET_OPTION, height, index, out, PALETTE_CHARS_STANDARD, PALETTE_CUSTOM, render_mode, SAFE_MALLOC, utf8_decode(), and width.

Referenced by audio_visualization_render_fft(), and audio_visualization_render_waveform().