ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
mixer.c
Go to the documentation of this file.
2#include <ascii-chat/otel.h>
3#include <ascii-chat/buffer.h>
9
13#include <ascii-chat/common.h>
14#include <ascii-chat/asciichat_errno.h> // For asciichat_errno system
16#include <ascii-chat/util/time.h> // For timing instrumentation
17#include <ascii-chat/util/bits.h> // For find_first_set_bit
18#include <ascii-chat/util/overflow.h> // For overflow checking
19#include <math.h>
20#include <string.h>
21#include <stdint.h>
23#include <options/options.h>
24
25#define MIXER_LISTENER_SAMPLES 8192
36
37// Called with the source write lock. Drain each input once and fan it out to
38// independent listener queues so one recipient cannot consume another's audio.
39static void mixer_distribute_samples(mixer_t *mixer) {
40 float samples[OPUS_FRAME_SAMPLES];
41 for (int source = 0; source < mixer->max_sources; source++) {
42 if (!mixer->source_ids[source] || !mixer->source_buffers[source])
43 continue;
45 size_t count = audio_ring_buffer_read(mixer->source_buffers[source], samples, OPUS_FRAME_SAMPLES);
46 if (!count)
47 break;
48 for (int listener = 0; listener < mixer->max_sources; listener++) {
49 if (listener == source || !mixer->source_ids[listener])
50 continue;
51 mixer_listener_buffer_t *queue = mixer->listener_buffers[listener][source];
52 if (!queue)
53 continue;
54 if (queue->count + count > MIXER_LISTENER_SAMPLES) {
55 size_t discard = queue->count + count - MIXER_LISTENER_SAMPLES;
56 queue->read = (queue->read + discard) % MIXER_LISTENER_SAMPLES;
57 queue->count -= discard;
58 }
59 for (size_t i = 0; i < count; i++)
60 queue->samples[(queue->read + queue->count + i) % MIXER_LISTENER_SAMPLES] = samples[i];
61 queue->count += count;
62 }
63 }
64 }
65}
66
67#ifndef M_PI
68#define M_PI 3.14159265358979323846
69#endif
70
71static void mixer_spectral_process(mixer_t *mixer, int slot, float *samples, size_t count) {
72 if (mixer->channels == 1) {
73 (void)spectral_process(mixer->spectral_left[slot], samples, samples, count);
74 return;
75 }
76 for (size_t base = 0; base < count;) {
77 size_t frames = (count - base) / 2;
78 if (frames > 480)
79 frames = 480;
80 if (!frames)
81 break;
82 float left[480], right[480];
83 for (size_t i = 0; i < frames; i++) {
84 left[i] = samples[base + i * 2];
85 right[i] = samples[base + i * 2 + 1];
86 }
87 (void)spectral_process(mixer->spectral_left[slot], left, left, frames);
88 (void)spectral_process(mixer->spectral_right[slot], right, right, frames);
89 for (size_t i = 0; i < frames; i++) {
90 samples[base + i * 2] = left[i];
91 samples[base + i * 2 + 1] = right[i];
92 }
93 base += frames * 2;
94 }
95}
96
97// Ducking implementation
98int ducking_init(ducking_t *duck, int num_sources, float sample_rate) {
99 if (!duck) {
100 return SET_ERRNO(ERROR_INVALID_PARAM, "duck is NULL");
101 }
102
103 // Set default parameters
104 // threshold_dB: sources below this aren't considered "speaking"
105 // leader_margin_dB: sources within this margin of loudest are all "leaders"
106 // atten_dB: how much to attenuate non-leaders (was -12dB, too aggressive)
107 duck->threshold_dB = -45.0f; // More lenient threshold
108 duck->leader_margin_dB = 6.0f; // Wider margin = more sources treated as leaders
109 duck->atten_dB = -6.0f; // Only -6dB attenuation (was -12dB)
110 duck->attack_ns = 10 * NS_PER_MS_INT; // Slower attack (10ms)
111 duck->release_ns = 200 * NS_PER_MS_INT; // Slower release (200ms)
112 duck->envelope = NULL;
113 duck->gain = NULL;
114
115 // Calculate time constants
116 float attack_tau = (float)duck->attack_ns / NS_PER_SEC;
117 float release_tau = (float)duck->release_ns / NS_PER_SEC;
118 duck->attack_coeff = expf(-1.0f / (attack_tau * sample_rate + 1e-12f));
119 duck->release_coeff = expf(-1.0f / (release_tau * sample_rate + 1e-12f));
120
121 // Allocate arrays with overflow checking
122 size_t envelope_size = 0;
123 if (checked_size_mul((size_t)num_sources, sizeof(float), &envelope_size) != ASCIICHAT_OK) {
124 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Ducking envelope array size overflow: %d sources", num_sources);
125 }
126 duck->envelope = SAFE_MALLOC(envelope_size, float *);
127 if (!duck->envelope) {
128 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate ducking envelope array");
129 }
130
131 size_t gain_size = 0;
132 if (checked_size_mul((size_t)num_sources, sizeof(float), &gain_size) != ASCIICHAT_OK) {
133 SAFE_FREE(duck->envelope);
134 duck->envelope = NULL;
135 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Ducking gain array size overflow: %d sources", num_sources);
136 }
137 duck->gain = SAFE_MALLOC(gain_size, float *);
138 if (!duck->gain) {
139 SAFE_FREE(duck->envelope);
140 duck->envelope = NULL;
141 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate ducking gain array");
142 }
143
144 // Initialize
145 SAFE_MEMSET(duck->envelope, (size_t)num_sources * sizeof(float), 0, (size_t)num_sources * sizeof(float));
146 for (int i = 0; i < num_sources; i++) {
147 duck->gain[i] = 1.0f;
148 }
149
150 return ASCIICHAT_OK;
151}
152
154 if (duck->envelope) {
155 SAFE_FREE(duck->envelope);
156 }
157 if (duck->gain) {
158 SAFE_FREE(duck->gain);
159 }
160}
161
162void ducking_set_params(ducking_t *duck, float threshold_dB, float leader_margin_dB, float atten_dB, uint64_t attack_ns, uint64_t release_ns) {
163 duck->threshold_dB = threshold_dB;
164 duck->leader_margin_dB = leader_margin_dB;
165 duck->atten_dB = atten_dB;
166 duck->attack_ns = attack_ns;
167 duck->release_ns = release_ns;
168}
169
170void ducking_process_frame(ducking_t *duck, float *envelopes, float *gains, int num_sources) {
171 // Find the loudest source
172 float max_dB = -120.0f;
173 float env_dB[MIXER_MAX_SOURCES];
174
175 for (int i = 0; i < num_sources; i++) {
176 env_dB[i] = linear_to_db(envelopes[i]);
177 if (env_dB[i] > max_dB)
178 max_dB = env_dB[i];
179 }
180
181 // Calculate ducking gain for each source
182 float leader_cut = db_to_linear(duck->atten_dB);
183
184 for (int i = 0; i < num_sources; i++) {
185 bool is_speaking = env_dB[i] > duck->threshold_dB;
186 bool is_leader = is_speaking && (env_dB[i] >= max_dB - duck->leader_margin_dB);
187
188 float target;
189 if (is_speaking && !is_leader) {
190 target = leader_cut;
191 } else {
192 target = 1.0f;
193 }
194
195 // Smooth gain transitions
196 if (target < gains[i])
197 gains[i] = duck->attack_coeff * gains[i] + (1.0f - duck->attack_coeff) * target;
198 else
199 gains[i] = duck->release_coeff * gains[i] + (1.0f - duck->release_coeff) * target;
200 }
201}
202
203// Mixer implementation
204mixer_t *mixer_create(int max_sources, int sample_rate) {
205 return mixer_create_channels(max_sources, sample_rate, 1);
206}
207
208mixer_t *mixer_create_channels(int max_sources, int sample_rate, unsigned channels) {
209 if (channels != 1 && channels != 2) {
210 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid mixer channel count");
211 return NULL;
212 }
213 ASCII_OTEL_SCOPE(MIXER_CREATE);
214 // Validate parameters
215 if (max_sources <= 0 || max_sources > MIXER_MAX_SOURCES) {
216 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid max_sources: %d (must be 1-%d)", max_sources, MIXER_MAX_SOURCES);
217 return NULL;
218 }
219
220 if (sample_rate <= 0 || sample_rate > 192000) {
221 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid sample_rate: %d (must be 1-192000)", sample_rate);
222 return NULL;
223 }
224
225 mixer_t *mixer;
226 mixer = SAFE_MALLOC(sizeof(mixer_t), mixer_t *);
227 if (!mixer) {
228 SET_ERRNO(ERROR_MEMORY, "Failed to allocate mixer structure");
229 return NULL;
230 }
231
232 memset(mixer->spectral_left, 0, sizeof(mixer->spectral_left));
233 memset(mixer->spectral_right, 0, sizeof(mixer->spectral_right));
234 mixer->num_sources = 0;
236 mixer->max_sources = max_sources;
237 mixer->sample_rate = sample_rate;
238 mixer->channels = channels;
239 memset(mixer->listener_buffers, 0, sizeof(mixer->listener_buffers));
240
241 // Allocate source management arrays with overflow checking
242 size_t buffers_size = 0;
243 if (checked_size_mul((size_t)max_sources, sizeof(audio_ring_buffer_t *), &buffers_size) != ASCIICHAT_OK) {
244 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Mixer source buffers array overflow: %d sources", max_sources);
245 SAFE_FREE(mixer);
246 return NULL;
247 }
248 mixer->source_buffers = SAFE_MALLOC(buffers_size, audio_ring_buffer_t **);
249 if (!mixer->source_buffers) {
250 SAFE_FREE(mixer);
251 return NULL;
252 }
253
254 size_t ids_size = 0;
255 if (checked_size_mul((size_t)max_sources, sizeof(const char *), &ids_size) != ASCIICHAT_OK) {
256 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Mixer source IDs array overflow: %d sources", max_sources);
258 SAFE_FREE(mixer);
259 return NULL;
260 }
261 mixer->source_ids = SAFE_MALLOC(ids_size, const char **);
262 if (!mixer->source_ids) {
264 SAFE_FREE(mixer);
265 return NULL;
266 }
267
268 size_t active_size = 0;
269 if (checked_size_mul((size_t)max_sources, sizeof(bool), &active_size) != ASCIICHAT_OK) {
270 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Mixer source active array overflow: %d sources", max_sources);
272 SAFE_FREE(mixer->source_ids);
273 SAFE_FREE(mixer);
274 return NULL;
275 }
276 mixer->source_active = SAFE_MALLOC(active_size, bool *);
277 if (!mixer->source_active) {
279 SAFE_FREE(mixer->source_ids);
280 SAFE_FREE(mixer);
281 return NULL;
282 }
283
284 // Initialize arrays
285 SAFE_MEMSET((void *)mixer->source_buffers, buffers_size, 0, buffers_size);
286 SAFE_MEMSET(mixer->source_ids, ids_size, 0, ids_size);
287 SAFE_MEMSET(mixer->source_active, active_size, 0, active_size);
288
289 // OPTIMIZATION 1: Initialize bitset optimization structures
290 memset(mixer->active_sources_mask, 0, sizeof(mixer->active_sources_mask)); // No sources active initially
291 for (int i = 0; i < 256; i++) {
293 }
294
295 // Allocate mix buffer BEFORE rwlock_init so cleanup path is correct
296 size_t mix_buffer_size = 0;
297 if (checked_size_mul(MIXER_FRAME_SIZE, sizeof(float), &mix_buffer_size) != ASCIICHAT_OK) {
298 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Mixer mix buffer size overflow: %zu samples", (size_t)MIXER_FRAME_SIZE);
300 SAFE_FREE(mixer->source_ids);
301 SAFE_FREE(mixer->source_active);
302 SAFE_FREE(mixer);
303 return NULL;
304 }
305 mixer->mix_buffer = buffer_alloc(NULL, mix_buffer_size);
306 if (!mixer->mix_buffer) {
307 SET_ERRNO(ERROR_MEMORY, "Failed to allocate mix buffer");
309 SAFE_FREE(mixer->source_ids);
310 SAFE_FREE(mixer->source_active);
311 SAFE_FREE(mixer);
312 return NULL;
313 }
314
315 // OPTIMIZATION 2: Initialize reader-writer lock
316 if (rwlock_init(&mixer->source_lock, "audio_mixer_sources") != 0) {
317 SET_ERRNO(ERROR_THREAD, "Failed to initialize mixer source lock");
319 SAFE_FREE(mixer->source_ids);
320 SAFE_FREE(mixer->source_active);
322 SAFE_FREE(mixer);
323 return NULL;
324 }
325
326 // Set crowd scaling parameters
327 mixer->crowd_alpha = 0.5f; // Square root scaling
328 mixer->base_gain = 1.0f; // Unity gain - let compressor handle loudness, avoid clipping
329
330 // Initialize processing
331 if (ducking_init(&mixer->ducking, max_sources, (float)sample_rate) != ASCIICHAT_OK) {
332 rwlock_destroy(&mixer->source_lock);
334 SAFE_FREE(mixer->source_ids);
335 SAFE_FREE(mixer->source_active);
337 SAFE_FREE(mixer);
338 return NULL;
339 }
340 compressor_init(&mixer->compressor, (float)sample_rate);
341 if (GET_OPTION(audio_spectral_mixer)) {
342 spectral_config_t config = spectral_default_config(sample_rate);
343 config.fft_size = GET_OPTION(audio_fft_size);
344 config.noise_gate = GET_OPTION(audio_spectral_gate);
345 config.compressor = GET_OPTION(audio_multiband);
346 config.adaptive_eq = GET_OPTION(audio_adaptive_eq);
347 config.auto_gain = GET_OPTION(audio_spectral_agc);
348 if (channels == 2) {
349 config.auto_gain = false;
350 config.compressor = false;
351 config.noise_gate = false;
352 }
353 for (int i = 0; i <= max_sources; ++i) {
354 if (spectral_create(&config, &mixer->spectral_left[i]) != ASCIICHAT_OK || (channels == 2 && spectral_create(&config, &mixer->spectral_right[i]) != ASCIICHAT_OK)) {
355 mixer_destroy(mixer);
356 return NULL;
357 }
358 }
359 }
360
361 log_debug("Audio mixer created: max_sources=%d, sample_rate=%d", max_sources, sample_rate);
362
363 // Register mixer and its sync primitives for debugging
364 NAMED_REGISTER_MIXER(mixer, "audio_mixer", NULL);
365 NAMED_REGISTER_RWLOCK(&mixer->source_lock, "source_lock", (uintptr_t)(const void *)(mixer));
366
367 return mixer;
368}
369
370void mixer_destroy(mixer_t *mixer) {
371 if (!mixer)
372 return;
373
374 NAMED_UNREGISTER(mixer);
375 for (int i = 0; i <= mixer->max_sources; ++i) {
378 }
379
380 // OPTIMIZATION 2: Destroy reader-writer lock
381 rwlock_destroy(&mixer->source_lock);
382
383 ducking_destroy(&mixer->ducking);
384
385 // Free all allocated client_id strings before freeing the array itself
386 for (int i = 0; i < mixer->max_sources; i++) {
387 for (int j = 0; j < mixer->max_sources; j++)
388 SAFE_FREE(mixer->listener_buffers[i][j]);
389 if (mixer->source_ids[i] != NULL) {
390 SAFE_FREE(mixer->source_ids[i]);
391 }
392 }
393
395 SAFE_FREE(mixer->source_ids);
396 SAFE_FREE(mixer->source_active);
398 SAFE_FREE(mixer);
399 log_debug("Audio mixer destroyed");
400}
401
402int mixer_add_source(mixer_t *mixer, const char *client_id, audio_ring_buffer_t *buffer) {
403 ASCII_OTEL_SCOPE(MIXER_ADD_SOURCE);
404 if (!mixer || !buffer || buffer->channels != mixer->channels)
405 return -1;
406
407 // OPTIMIZATION 2: Acquire write lock for source modification
408 rwlock_wrlock(&mixer->source_lock);
409
410 // Find an empty slot
411 int slot = -1;
412 for (int i = 0; i < mixer->max_sources; i++) {
413 if (mixer->source_ids[i] == NULL) {
414 slot = i;
415 break;
416 }
417 }
418
419 if (slot == -1) {
420 rwlock_wrunlock(&mixer->source_lock);
421 log_warn("Mixer: No available slots for client %s", client_id);
422 return -1;
423 }
424
425 mixer->source_buffers[slot] = buffer;
426 // Duplicate the client_id string - we own this memory now
427 size_t client_id_len = strlen(client_id) + 1;
428 mixer->source_ids[slot] = SAFE_MALLOC(client_id_len, char *);
429 if (!mixer->source_ids[slot]) {
430 log_warn("Mixer: Failed to allocate memory for client_id %s", client_id);
431 mixer->source_buffers[slot] = NULL;
432 rwlock_wrunlock(&mixer->source_lock);
433 return -1;
434 }
435 asciichat_error_t strcpy_result = SAFE_STRCPY((char *)mixer->source_ids[slot], client_id_len, client_id);
436 if (strcpy_result != ASCIICHAT_OK) {
437 log_error("Mixer: Failed to copy client_id: %s", asciichat_error_format(NULL, strcpy_result));
438 SAFE_FREE(mixer->source_ids[slot]);
439 mixer->source_buffers[slot] = NULL;
440 rwlock_wrunlock(&mixer->source_lock);
441 return -1;
442 }
443 for (int other = 0; other < mixer->max_sources; other++) {
444 if (other == slot || !mixer->source_ids[other])
445 continue;
446 mixer->listener_buffers[slot][other] = SAFE_CALLOC(1, sizeof(mixer_listener_buffer_t), void *);
447 mixer->listener_buffers[other][slot] = SAFE_CALLOC(1, sizeof(mixer_listener_buffer_t), void *);
448 }
449 mixer->source_active[slot] = true;
450 mixer->num_sources++;
452
453 // OPTIMIZATION 1: Update bitset optimization structures
454 mixer->active_sources_mask[slot / 64] |= (1ULL << (slot % 64)); // Set bit for this slot
455 // Hash table optimization disabled for string-based client_ids (linear search is acceptable for small mixer sizes)
456
457 rwlock_wrunlock(&mixer->source_lock);
458
459 log_info("Mixer: Added source for client %s at slot %d", client_id, slot);
460 return slot;
461}
462
463void mixer_remove_source(mixer_t *mixer, const char *client_id) {
464 ASCII_OTEL_SCOPE(MIXER_REMOVE_SOURCE);
465 if (!mixer)
466 return;
467
468 // OPTIMIZATION 2: Acquire write lock for source modification
469 rwlock_wrlock(&mixer->source_lock);
470
471 for (int i = 0; i < mixer->max_sources; i++) {
472 if (mixer->source_ids[i] != NULL && strcmp(mixer->source_ids[i], client_id) == 0) {
473 for (int j = 0; j < mixer->max_sources; j++) {
474 SAFE_FREE(mixer->listener_buffers[i][j]);
475 if (j != i)
476 SAFE_FREE(mixer->listener_buffers[j][i]);
477 }
478 spectral_reset(mixer->spectral_left[i]);
480 mixer->source_buffers[i] = NULL;
481 // Free the allocated client_id string
482 SAFE_FREE(mixer->source_ids[i]);
483 mixer->source_active[i] = false;
484 mixer->num_sources--;
486
487 // OPTIMIZATION 1: Update bitset optimization structures
488 mixer->active_sources_mask[i / 64] &= ~(1ULL << (i % 64)); // Clear bit for this slot
489 // Hash table optimization disabled for string-based client_ids
490
491 // Reset ducking state for this source
492 mixer->ducking.envelope[i] = 0.0f;
493 mixer->ducking.gain[i] = 1.0f;
494
495 rwlock_wrunlock(&mixer->source_lock);
496
497 log_info("Mixer: Removed source for client %s from slot %d", client_id, i);
498 return;
499 }
500 }
501
502 rwlock_wrunlock(&mixer->source_lock);
503}
504
505int mixer_source_count(const mixer_t *mixer) {
506 return mixer ? atomic_load_int((atomic_t *)&mixer->active_sources) : 0;
507}
508
509void mixer_set_source_active(mixer_t *mixer, const char *client_id, bool active) {
510 if (!mixer)
511 return;
512
513 // OPTIMIZATION 2: Acquire write lock for source modification
514 rwlock_wrlock(&mixer->source_lock);
515
516 for (int i = 0; i < mixer->max_sources; i++) {
517 if (mixer->source_ids[i] != NULL && strcmp(mixer->source_ids[i], client_id) == 0) {
518 mixer->source_active[i] = active;
519
520 // OPTIMIZATION 1: Update bitset for active state change
521 if (active) {
522 mixer->active_sources_mask[i / 64] |= (1ULL << (i % 64)); // Set bit
523 } else {
524 mixer->active_sources_mask[i / 64] &= ~(1ULL << (i % 64)); // Clear bit
525 }
526
527 rwlock_wrunlock(&mixer->source_lock);
528 log_debug("Mixer: Set source %s active=%d", client_id, active);
529 return;
530 }
531 }
532
533 rwlock_wrunlock(&mixer->source_lock);
534}
535
536int mixer_process(mixer_t *mixer, float *output, int num_samples) {
537 ASCII_OTEL_SCOPE(MIXER_PROCESS);
538 if (!mixer || !output || num_samples <= 0)
539 return -1;
540
541 // Serialize source access and mutable compressor/spectral state.
542 rwlock_wrlock(&mixer->source_lock);
543
544 // Clear output buffer
545 SAFE_MEMSET(output, num_samples * sizeof(float), 0, num_samples * sizeof(float));
546
547 // Count active sources
548 int active_count = 0;
549 for (int i = 0; i < mixer->max_sources; i++) {
550 if (mixer->source_ids[i] != 0 && mixer->source_active[i] && mixer->source_buffers[i]) {
551 active_count++;
552 }
553 }
554
555 if (active_count == 0 && !mixer->spectral_left[mixer->max_sources]) {
556 rwlock_wrunlock(&mixer->source_lock);
557 return 0;
558 }
559
560 // Process in frames for efficiency
561 for (int frame_start = 0; frame_start < num_samples; frame_start += MIXER_FRAME_SIZE) {
562 int frame_size = (frame_start + MIXER_FRAME_SIZE > num_samples) ? (num_samples - frame_start) : MIXER_FRAME_SIZE;
563
564 // Clear mix buffer
565 SAFE_MEMSET(mixer->mix_buffer, frame_size * sizeof(float), 0, frame_size * sizeof(float));
566
567 // Temporary buffers for source audio
568 float source_samples[MIXER_MAX_SOURCES][MIXER_FRAME_SIZE];
569 int source_count = 0;
570 int source_map[MIXER_MAX_SOURCES]; // Maps source index to slot
571
572 // Read from each active source
573 for (int i = 0; i < mixer->max_sources; i++) {
574 if (mixer->source_ids[i] != 0 && mixer->source_active[i] && mixer->source_buffers[i]) {
575 // Read samples from this source's ring buffer
576 size_t samples_read_size = audio_ring_buffer_read(mixer->source_buffers[i], source_samples[source_count], frame_size);
577 int samples_read = (int)samples_read_size;
578
579 // Accept partial frames - pad with silence if needed
580 // This prevents audio dropouts when ring buffers are temporarily under-filled
581 if (samples_read > 0) {
582 // Pad remaining samples with silence if we got a partial frame
583 if (samples_read < frame_size) {
584 SAFE_MEMSET(&source_samples[source_count][samples_read], (frame_size - samples_read) * sizeof(float), 0, (frame_size - samples_read) * sizeof(float));
585 }
586
587 source_map[source_count] = i;
588 source_count++;
589
590 if (source_count >= MIXER_MAX_SOURCES)
591 break;
592 }
593 }
594 }
595
596 // OPTIMIZATION: Batch envelope calculation per-frame instead of per-sample
597 // Calculate peak amplitude for each source over the entire frame
598 int speaking_count = 0;
599
600 for (int i = 0; i < source_count; i++) {
601 int slot = source_map[i];
602 float peak = 0.0f;
603
604 // Find peak amplitude in frame (much faster than per-sample envelope)
605 for (int s = 0; s < frame_size; s++) {
606 float abs_sample = fabsf(source_samples[i][s]);
607 if (abs_sample > peak)
608 peak = abs_sample;
609 }
610
611 // Update envelope using frame peak (one update per frame instead of per-sample)
612 if (peak > mixer->ducking.envelope[slot]) {
613 mixer->ducking.envelope[slot] = mixer->ducking.attack_coeff * mixer->ducking.envelope[slot] + (1.0f - mixer->ducking.attack_coeff) * peak;
614 } else {
615 mixer->ducking.envelope[slot] = mixer->ducking.release_coeff * mixer->ducking.envelope[slot] + (1.0f - mixer->ducking.release_coeff) * peak;
616 }
617
618 // Count speaking sources
619 if (mixer->ducking.envelope[slot] > db_to_linear(-60.0f))
620 speaking_count++;
621 }
622
623 // Apply ducking ONCE per frame (not per-sample)
624 ducking_process_frame(&mixer->ducking, mixer->ducking.envelope, mixer->ducking.gain, mixer->max_sources);
625
626 // Calculate crowd scaling ONCE per frame
627 float crowd_gain = (speaking_count > 0) ? (1.0f / powf((float)speaking_count, mixer->crowd_alpha)) : 1.0f;
628 float pre_bus = mixer->base_gain * crowd_gain;
629
630 // Pre-calculate combined gains for each source (ducking * pre_bus)
631 float combined_gains[MIXER_MAX_SOURCES];
632 for (int i = 0; i < source_count; i++) {
633 int slot = source_map[i];
634 combined_gains[i] = mixer->ducking.gain[slot] * pre_bus;
635 }
636
637 // OPTIMIZATION: Fast mixing loop - simple multiply-add with pre-calculated gains
638 // NO per-sample compressor to avoid expensive log/pow calls (480x per iteration)
639 for (int s = 0; s < frame_size; s++) {
640 float mix = 0.0f;
641 for (int i = 0; i < source_count; i++) {
642 mix += source_samples[i][s] * combined_gains[i];
643 }
644
645 // Store in mix buffer for frame-level compression below
646 mixer->mix_buffer[s] = mix;
647 }
648
649 // OPTIMIZATION: Apply compression ONCE per frame instead of per-sample
650 // This reduces expensive log10f/powf calls from 480x to 1x per iteration
651 // Calculate frame peak for compressor sidechain
652 float frame_peak = 0.0f;
653 for (int s = 0; s < frame_size; s++) {
654 float abs_val = fabsf(mixer->mix_buffer[s]);
655 if (abs_val > frame_peak)
656 frame_peak = abs_val;
657 }
658
659 // Process compressor with frame peak (1 call instead of 480)
660 float comp_gain = compressor_process_sample(&mixer->compressor, frame_peak);
661
662 uint64_t clipped = 0;
663 // Apply compression gain and soft clipping to all samples
664 for (int s = 0; s < frame_size; s++) {
665 float mix = mixer->mix_buffer[s] * comp_gain;
666 if (fabsf(mix) > 0.7f)
667 ++clipped;
668
669 // Soft clip at 0.7 to provide 3dB headroom and prevent hard clipping
670 // With +6dB makeup gain, peaks can exceed 1.0, so we need headroom
671 // threshold=0.7 maps asymptotically toward 1.0, preventing harsh distortion
672 output[frame_start + s] = soft_clip(mix, 0.7f, 3.0f);
673 }
674 ascii_otel_metric(ASCII_OTEL_METRIC_AUDIO_CLIP, (double)clipped);
675 }
676
677 if (mixer->spectral_left[mixer->max_sources])
678 mixer_spectral_process(mixer, mixer->max_sources, output, (size_t)num_samples);
679 rwlock_wrunlock(&mixer->source_lock);
680 return num_samples;
681}
682
683int mixer_process_excluding_source(mixer_t *mixer, float *output, int num_samples, uint32_t exclude_client_id) {
684 ASCII_OTEL_SCOPE(MIXER_PROCESS_EXCLUDING_SOURCE);
685 if (!mixer || !output || num_samples <= 0) {
686 return -1;
687 }
688
689 // Serialize sample distribution and mutable ducking/compressor state.
690 rwlock_wrlock(&mixer->source_lock);
691
692 // Clear output buffer
693 SAFE_MEMSET(output, num_samples * sizeof(float), 0, num_samples * sizeof(float));
694
695 // Source IDs are strings; match the complete ID hash used by the caller.
696 uint8_t exclude_index = MIXER_HASH_INVALID;
697 for (int i = 0; i < mixer->max_sources; i++) {
698 if (mixer->source_ids[i] && fnv1a_hash_string(mixer->source_ids[i]) == exclude_client_id) {
699 exclude_index = (uint8_t)i;
700 break;
701 }
702 }
703 if (exclude_index != MIXER_HASH_INVALID)
704 mixer_distribute_samples(mixer);
705 uint64_t active_mask[MIXER_MASK_WORDS];
706 memcpy(active_mask, mixer->active_sources_mask, sizeof(active_mask));
707 bool valid_exclude = exclude_index < mixer->max_sources && exclude_index != MIXER_HASH_INVALID;
708 if (valid_exclude)
709 active_mask[exclude_index / 64] &= ~(1ULL << (exclude_index % 64));
710 bool any_active = false;
711 for (size_t word = 0; word < MIXER_MASK_WORDS; ++word)
712 any_active |= active_mask[word] != 0;
713
714 // Fast check: any sources to mix?
715 int spectral_slot = valid_exclude ? exclude_index : mixer->max_sources;
716 if (!any_active && !mixer->spectral_left[spectral_slot]) {
717 rwlock_wrunlock(&mixer->source_lock);
718 return 0; // No sources to mix (excluding the specified client), output silence
719 }
720
721 // Process in frames for efficiency
722 for (int frame_start = 0; frame_start < num_samples; frame_start += MIXER_FRAME_SIZE) {
723 int frame_size = (frame_start + MIXER_FRAME_SIZE > num_samples) ? (num_samples - frame_start) : MIXER_FRAME_SIZE;
724
725 uint64_t read_start_ns = time_get_ns();
726
727 // Clear mix buffer
728 SAFE_MEMSET(mixer->mix_buffer, frame_size * sizeof(float), 0, frame_size * sizeof(float));
729
730 // Temporary buffers for source audio
731 float source_samples[MIXER_MAX_SOURCES][MIXER_FRAME_SIZE];
732 int source_count = 0;
733 int source_map[MIXER_MAX_SOURCES]; // Maps source index to slot
734
735 // OPTIMIZATION 1: Iterate only over active sources using bitset
736 size_t word = 0;
737 uint64_t current_mask = active_mask[0];
738 while (source_count < MIXER_MAX_SOURCES) {
739 while (!current_mask && word + 1 < MIXER_MASK_WORDS)
740 current_mask = active_mask[++word];
741 if (!current_mask)
742 break;
743 int i = (int)(word * 64) + find_first_set_bit(current_mask);
744 current_mask &= current_mask - 1;
745
746 // Verify source is valid (defensive programming)
747 if (i < mixer->max_sources && mixer->source_ids[i] != 0 && mixer->source_buffers[i]) {
748 // Read samples from this source's ring buffer
749 size_t samples_read_size = 0;
750 if (valid_exclude) {
751 mixer_listener_buffer_t *queue = mixer->listener_buffers[exclude_index][i];
752 if (queue && queue->count >= (size_t)frame_size) {
753 samples_read_size = (size_t)frame_size;
754 for (int s = 0; s < frame_size; s++)
755 source_samples[source_count][s] = queue->samples[(queue->read + s) % MIXER_LISTENER_SAMPLES];
756 queue->read = (queue->read + samples_read_size) % MIXER_LISTENER_SAMPLES;
757 queue->count -= samples_read_size;
758 }
759 } else {
760 samples_read_size = audio_ring_buffer_read(mixer->source_buffers[i], source_samples[source_count], frame_size);
761 }
762 int samples_read = (int)samples_read_size;
763
764 // Accept partial frames - pad with silence if needed
765 // This prevents audio dropouts when ring buffers are temporarily under-filled
766 if (samples_read > 0) {
767 // Pad remaining samples with silence if we got a partial frame
768 if (samples_read < frame_size) {
769 SAFE_MEMSET(&source_samples[source_count][samples_read], (frame_size - samples_read) * sizeof(float), 0, (frame_size - samples_read) * sizeof(float));
770 }
771
772 // DIAGNOSTIC: Calculate RMS of this source's audio
773 float source_rms = 0.0f;
774 if (frame_start == 0) { // Only log for first frame to reduce spam
775 float sum_squares = 0.0f;
776 for (int s = 0; s < samples_read; s++) {
777 sum_squares += source_samples[source_count][s] * source_samples[source_count][s];
778 }
779 source_rms = sqrtf(sum_squares / (float)samples_read);
780 log_dev_every(NS_PER_MS_INT, "MIXER SOURCE READ: client_id=%u, slot=%d, samples_read=%d, RMS=%.6f", mixer->source_ids[i], i, samples_read, source_rms);
781 }
782
783 source_map[source_count] = i;
784 source_count++;
785 }
786 }
787 }
788
789 uint64_t read_end_ns = time_get_ns();
790 uint64_t read_time_ns = time_elapsed_ns(read_start_ns, read_end_ns);
791
792 if (read_time_ns > 10 * NS_PER_MS_INT) {
793 char read_time_str[TIME_DURATION_BUFFER_SIZE];
794 time_pretty(read_time_ns, -1, read_time_str, sizeof(read_time_str));
795 log_warn_every(LOG_RATE_DEFAULT, "Mixer: Slow source reading took %s for %d sources", read_time_str, source_count);
796 }
797
798 if (GET_OPTION(no_audio_mixer)) {
799 for (int sample = 0; sample < frame_size; sample++) {
800 for (int source = 0; source < source_count; source++)
801 output[frame_start + sample] += source_samples[source][sample];
802 }
803 continue;
804 }
805
806 // OPTIMIZATION: Batch envelope calculation per-frame instead of per-sample
807 // Calculate peak amplitude for each source over the entire frame
808 int speaking_count = 0;
809
810 for (int i = 0; i < source_count; i++) {
811 int slot = source_map[i];
812 float peak = 0.0f;
813
814 // Find peak amplitude in frame (much faster than per-sample envelope)
815 for (int s = 0; s < frame_size; s++) {
816 float abs_sample = fabsf(source_samples[i][s]);
817 if (abs_sample > peak)
818 peak = abs_sample;
819 }
820
821 // Update envelope using frame peak (one update per frame instead of per-sample)
822 if (peak > mixer->ducking.envelope[slot]) {
823 mixer->ducking.envelope[slot] = mixer->ducking.attack_coeff * mixer->ducking.envelope[slot] + (1.0f - mixer->ducking.attack_coeff) * peak;
824 } else {
825 mixer->ducking.envelope[slot] = mixer->ducking.release_coeff * mixer->ducking.envelope[slot] + (1.0f - mixer->ducking.release_coeff) * peak;
826 }
827
828 // Count speaking sources
829 if (mixer->ducking.envelope[slot] > db_to_linear(-60.0f))
830 speaking_count++;
831 }
832
833 // Apply ducking ONCE per frame (not per-sample)
834 ducking_process_frame(&mixer->ducking, mixer->ducking.envelope, mixer->ducking.gain, mixer->max_sources);
835
836 // Calculate crowd scaling ONCE per frame
837 float crowd_gain = (speaking_count > 0) ? (1.0f / powf((float)speaking_count, mixer->crowd_alpha)) : 1.0f;
838 float pre_bus = mixer->base_gain * crowd_gain;
839
840 // Pre-calculate combined gains for each source (ducking * pre_bus)
841 float combined_gains[MIXER_MAX_SOURCES];
842 for (int i = 0; i < source_count; i++) {
843 int slot = source_map[i];
844 combined_gains[i] = mixer->ducking.gain[slot] * pre_bus;
845 }
846
847 // OPTIMIZATION: Fast mixing loop - simple multiply-add with pre-calculated gains
848 // NO per-sample compressor to avoid expensive log/pow calls (480x per iteration)
849 for (int s = 0; s < frame_size; s++) {
850 float mix = 0.0f;
851 for (int i = 0; i < source_count; i++) {
852 mix += source_samples[i][s] * combined_gains[i];
853 }
854
855 // Store in mix buffer for frame-level compression below
856 mixer->mix_buffer[s] = mix;
857 }
858
859 // OPTIMIZATION: Apply compression ONCE per frame instead of per-sample
860 // This reduces expensive log10f/powf calls from 480x to 1x per iteration
861 // Calculate frame peak for compressor sidechain
862 float frame_peak = 0.0f;
863 for (int s = 0; s < frame_size; s++) {
864 float abs_val = fabsf(mixer->mix_buffer[s]);
865 if (abs_val > frame_peak)
866 frame_peak = abs_val;
867 }
868
869 // Process compressor with frame peak (1 call instead of 480)
870 float comp_gain = compressor_process_sample(&mixer->compressor, frame_peak);
871
872 uint64_t clipped = 0;
873 // Apply compression gain and soft clipping to all samples
874 for (int s = 0; s < frame_size; s++) {
875 float mix = mixer->mix_buffer[s] * comp_gain;
876 if (fabsf(mix) > 0.7f)
877 ++clipped;
878
879 // Soft clip at 0.7 to provide 3dB headroom and prevent hard clipping
880 // With +6dB makeup gain, peaks can exceed 1.0, so we need headroom
881 // threshold=0.7 maps asymptotically toward 1.0, preventing harsh distortion
882 output[frame_start + s] = soft_clip(mix, 0.7f, 3.0f);
883 }
884 ascii_otel_metric(ASCII_OTEL_METRIC_AUDIO_CLIP, (double)clipped);
885 }
886
887 if (mixer->spectral_left[spectral_slot])
888 mixer_spectral_process(mixer, spectral_slot, output, (size_t)num_samples);
889 rwlock_wrunlock(&mixer->source_lock);
890
891 return num_samples;
892}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
int atomic_fetch_add_int(atomic_t *a, int delta)
Atomically add to an int and return the previous value.
int atomic_fetch_sub_int(atomic_t *a, int delta)
Atomically subtract from an int and return the previous value.
void atomic_store_int(atomic_t *a, int value)
Atomically store an int value.
int atomic_load_int(atomic_t *a)
Atomically load an int value.
#define OPUS_FRAME_SAMPLES
🔢 Bit Manipulation Utilities
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
struct audio_ring_buffer audio_ring_buffer_t
Named object registry for debugging — log identifiable resource names.
audio_visualization_source_t source
Definition fft.c:25
️⃣ FNV-1a Hash Function Implementation
struct spectral_processor * spectral_left[128+1]
Definition mixer.h:352
uint8_t source_id_to_index[256]
Hash table mapping client_id → mixer source index (uses hash function for strings).
Definition mixer.h:367
float base_gain
Base gain before crowd scaling is applied.
Definition mixer.h:377
float leader_margin_dB
Leader margin in dB (sources within this of loudest are leaders).
Definition mixer.h:289
size_t audio_ring_buffer_read(audio_ring_buffer_t *rb, float *data, size_t samples)
Read audio samples from ring buffer.
float release_coeff
Release coefficient (converted from release_ms).
Definition mixer.h:300
const char ** source_ids
Array of client ID strings (one per source slot).
Definition mixer.h:360
struct spectral_processor * spectral_right[128+1]
Definition mixer.h:353
#define MIXER_MAX_SOURCES
Maximum number of simultaneous audio sources.
Definition mixer.h:105
bool * source_active
Array of active flags (true if source is active).
Definition mixer.h:362
float atten_dB
Attenuation in dB for non-leader sources.
Definition mixer.h:291
rwlock_t source_lock
Reader-writer lock protecting source arrays and bitset.
Definition mixer.h:372
atomic_t active_sources
Definition mixer.h:345
float * gain
Per-source ducking gain (linear, calculated from envelope).
Definition mixer.h:304
float attack_coeff
Attack coefficient (converted from attack_ms).
Definition mixer.h:298
mixer_t * mixer_create_channels(int max_sources, int sample_rate, unsigned channels)
Definition mixer.c:208
unsigned channels
Definition mixer.h:350
uint64_t release_ns
Ducking release time in nanoseconds.
Definition mixer.h:295
float threshold_dB
Speaking threshold in dB (sources below this are not "speaking").
Definition mixer.h:287
size_t audio_ring_buffer_available_read(audio_ring_buffer_t *rb)
Get number of samples available for reading.
void ducking_process_frame(ducking_t *duck, float *envelopes, float *gains, int num_sources)
Process a frame of audio through ducking system.
Definition mixer.c:170
float crowd_alpha
Crowd scaling exponent (typically 0.5 for sqrt scaling).
Definition mixer.h:375
void ducking_destroy(ducking_t *duck)
Free ducking system resources.
Definition mixer.c:153
ducking_t ducking
Ducking system (active speaker detection and attenuation).
Definition mixer.h:380
int num_sources
Current number of active audio sources.
Definition mixer.h:344
float soft_clip(float sample, float threshold, float steepness)
Apply soft clipping to a sample.
Definition effects.c:284
void mixer_remove_source(mixer_t *mixer, const char *client_id)
Remove an audio source from the mixer.
Definition mixer.c:463
uint64_t active_sources_mask[((128+63)/64)]
Bitset of active sources (bit i = source i is active, O(1) iteration).
Definition mixer.h:365
void compressor_init(compressor_t *comp, float sample_rate)
Initialize a compressor.
Definition effects.c:29
int ducking_init(ducking_t *duck, int num_sources, float sample_rate)
Initialize ducking system.
Definition mixer.c:98
#define MIXER_MASK_WORDS
Definition mixer.h:106
#define MIXER_FRAME_SIZE
Number of samples processed per audio frame.
Definition mixer.h:116
uint64_t attack_ns
Ducking attack time in nanoseconds.
Definition mixer.h:293
float * envelope
Per-source envelope follower state (linear, allocated per source).
Definition mixer.h:302
int max_sources
Maximum number of sources (allocated array sizes).
Definition mixer.h:347
float * mix_buffer
Temporary buffer for mixing operations (pre-allocated).
Definition mixer.h:385
compressor_t compressor
Compressor (dynamic range compression).
Definition mixer.h:382
mixer_t * mixer_create(int max_sources, int sample_rate)
Create a new audio mixer.
Definition mixer.c:204
int mixer_process_excluding_source(mixer_t *mixer, float *output, int num_samples, uint32_t exclude_client_id)
Process audio from all sources except one (for per-client output).
Definition mixer.c:683
float db_to_linear(float db)
Convert decibels to linear gain.
Definition effects.c:10
#define MIXER_HASH_INVALID
Invalid index sentinel value for mixer hash table.
Definition mixer.h:489
void ducking_set_params(ducking_t *duck, float threshold_dB, float leader_margin_dB, float atten_dB, uint64_t attack_ns, uint64_t release_ns)
Set ducking parameters.
Definition mixer.c:162
void mixer_set_source_active(mixer_t *mixer, const char *client_id, bool active)
Set whether a source is active (receiving audio).
Definition mixer.c:509
audio_ring_buffer_t ** source_buffers
Array of pointers to client audio ring buffers.
Definition mixer.h:356
struct mixer_listener_buffer * listener_buffers[128][128]
Definition mixer.h:358
int mixer_source_count(const mixer_t *mixer)
Definition mixer.c:505
float compressor_process_sample(compressor_t *comp, float sidechain)
Process a single sample through compressor.
Definition effects.c:73
void mixer_destroy(mixer_t *mixer)
Destroy a mixer and free all resources.
Definition mixer.c:370
int sample_rate
Sample rate in Hz (e.g., 44100).
Definition mixer.h:349
float linear_to_db(float linear)
Convert linear gain to decibels.
Definition effects.c:14
int mixer_add_source(mixer_t *mixer, const char *client_id, audio_ring_buffer_t *buffer)
Add an audio source to the mixer.
Definition mixer.c:402
int mixer_process(mixer_t *mixer, float *output, int num_samples)
Process audio from all active sources.
Definition mixer.c:536
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MEMSET(dest, dest_size, ch, count)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
#define SAFE_STRCPY(dest, dest_size, src)
unsigned long long uint64_t
unsigned char uint8_t
#define NAMED_REGISTER_RWLOCK(lock, name, parent_ptr)
Register an rwlock with automatic format specifier.
#define NAMED_REGISTER_MIXER(mixer, name, parent_ptr)
Register an audio mixer with automatic format specifier.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ ERROR_THREAD
#define LOG_RATE_DEFAULT
Log rate limit: 5 seconds (5,000,000 microseconds) - default for audio/video packets.
Definition log_rates.h:36
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as a compact duration with configurable sub-minute precision.
Definition time.c:368
#define TIME_DURATION_BUFFER_SIZE
Definition time.h:40
#define NS_PER_MS_INT
Definition time.h:172
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)
Calculate elapsed time with wraparound safety.
Definition time.c:117
#define NS_PER_SEC
Definition time.h:164
#define GET_OPTION(field)
Definition options.h:1333
🔊 Audio Capture and Playback Interface for ascii-chat
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define log_warn_every(interval_us, fmt,...)
Rate-limited WARN logging.
Definition log/log.h:749
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
🔢 Mathematical Utility Functions
struct mixer_listener_buffer mixer_listener_buffer_t
Owns the temporary storage used by mixer listener buffer. The data pointers and size or cursor fields...
#define MIXER_LISTENER_SAMPLES
Definition mixer.c:25
Multi-Source Audio Mixing and Processing System.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:159
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
✅ Safe Integer Arithmetic and Overflow Detection
asciichat_error_t spectral_create(const spectral_config_t *c, spectral_processor_t **out)
Definition spectral.c:63
asciichat_error_t spectral_process(spectral_processor_t *p, const float *input, float *output, size_t count)
Definition spectral.c:272
spectral_config_t spectral_default_config(int sample_rate)
Definition spectral.c:36
void spectral_destroy(spectral_processor_t *p)
Definition spectral.c:96
void spectral_reset(spectral_processor_t *p)
Definition spectral.c:41
🔤 String Manipulation and Shell Escaping Utilities
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
unsigned channels
Frame width, configured before the first read/write.
Definition ringbuffer.h:214
Ducking system settings and state.
Definition mixer.h:285
Owns the temporary storage used by mixer listener buffer. The data pointers and size or cursor fields...
Definition mixer.c:31
float samples[8192]
Definition mixer.c:32
For ducking, this is the live record that ties threshold_dB, leader_margin_dB, atten_dB,...
Definition mixer.h:342
For spectral config, this is the live record that ties sample_rate, fft_size, noise_gate,...
Definition spectral.h:21
⏱️ High-precision timing utilities using sokol_time.h and uthash