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 File Reference

Go to the source code of this file.

Data Structures

struct  waveform_color_stop_t
 waveform color stop keeps the values that move through the subsystem: frequency, red, green, blue. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. More...

Functions

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_submit_stereo (audio_visualization_source_t source, const float *samples, size_t count)
void audio_visualization_submit (audio_visualization_source_t source, const float *samples, size_t count)
void audio_visualization_read (audio_visualization_source_t source, 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)

Function Documentation

◆ audio_visualization_read()

void audio_visualization_read ( audio_visualization_source_t source,
float * samples,
size_t count )

Definition at line 128 of file visualization.c.

129 {
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}
audio_visualization_source_t source
Definition fft.c:25
@ 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 VISUALIZATION_HISTORY

References AUDIO_VISUALIZATION_SOURCE_COUNT, AUDIO_VISUALIZATION_SOURCE_LOCAL_MIX, AUDIO_VISUALIZATION_SOURCE_MEDIA, AUDIO_VISUALIZATION_SOURCE_MIC, AUDIO_VISUALIZATION_SOURCE_MIX, AUDIO_VISUALIZATION_SOURCE_REMOTE, source, and VISUALIZATION_HISTORY.

Referenced by audio_visualization_render_fft(), and audio_visualization_render_waveform().

◆ audio_visualization_submit()

void audio_visualization_submit ( audio_visualization_source_t source,
const float * samples,
size_t count )

Definition at line 110 of file visualization.c.

111 {
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}
#define GET_OPTION(field)
Definition options.h:1339

References AUDIO_VISUALIZATION_SOURCE_MIC, AUDIO_VISUALIZATION_SOURCE_REMOTE, GET_OPTION, source, and VISUALIZATION_HISTORY.

Referenced by audio_visualization_submit_stereo(), and mirror_submit_audio_samples().

◆ audio_visualization_submit_stereo()

void audio_visualization_submit_stereo ( audio_visualization_source_t source,
const float * samples,
size_t count )

Downmix interleaved stereo only for display analysis, preserving its sample clock.

Definition at line 93 of file visualization.c.

94 {
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}
void audio_visualization_submit(audio_visualization_source_t source, const float *samples, size_t count)

References audio_visualization_submit(), and source.

Referenced by audio_write_samples().

◆ 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 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().