ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
mixer.h
Go to the documentation of this file.
1#include <ascii-chat/common.h>
2#pragma once
3
78
79#include <stdbool.h>
80#include <stdint.h>
81
82// Include ringbuffer.h to get the audio_ring_buffer_t type
83#include "../asciichat_errno.h"
84#include "../ringbuffer.h"
85#include <libplatform/rwlock.h>
86
87#ifdef __cplusplus
88extern "C" {
89#endif
90
96
105#define MIXER_MAX_SOURCES MAX_SESSION_PARTICIPANTS
106#define MIXER_MASK_WORDS ((MIXER_MAX_SOURCES + 63) / 64)
107
116#define MIXER_FRAME_SIZE 256
117
119
164
208
229typedef struct {
234
236 float alpha;
242
260typedef struct {
265
267 float alpha;
271
312
400
406
420mixer_t *mixer_create(int max_sources, int sample_rate);
421mixer_t *mixer_create_channels(int max_sources, int sample_rate,
422 unsigned channels);
425int mixer_source_count(const mixer_t *mixer);
426
438void mixer_destroy(mixer_t *mixer);
439
441
447
464int mixer_add_source(mixer_t *mixer, const char *client_id,
465 audio_ring_buffer_t *buffer);
466
481void mixer_remove_source(mixer_t *mixer, const char *client_id);
482
498void mixer_set_source_active(mixer_t *mixer, const char *client_id,
499 bool active);
500
501/* ============================================================================
502 * Mixer Hash Table Helpers (Internal API)
503 * ============================================================================
504 */
505
507#define MIXER_HASH_INVALID 0xFF
508
515static inline uint8_t mixer_hash_client_id(uint32_t client_id) {
516 return (uint8_t)(client_id & 0xFF);
517}
518
525static inline void mixer_hash_mark_invalid(mixer_t *mixer, uint32_t client_id) {
526 if (mixer) {
527 mixer->source_id_to_index[mixer_hash_client_id(client_id)] =
529 }
530}
531
539static inline void mixer_hash_set_slot(mixer_t *mixer, uint32_t client_id,
540 uint8_t slot) {
541 if (mixer) {
542 mixer->source_id_to_index[mixer_hash_client_id(client_id)] = slot;
543 }
544}
545
553static inline bool mixer_hash_is_invalid(mixer_t *mixer, uint32_t client_id) {
554 if (!mixer)
555 return true;
556 return mixer->source_id_to_index[mixer_hash_client_id(client_id)] ==
558}
559
567static inline uint8_t mixer_hash_get_slot(mixer_t *mixer, uint32_t client_id) {
568 if (!mixer)
569 return MIXER_HASH_INVALID;
570 return mixer->source_id_to_index[mixer_hash_client_id(client_id)];
571}
572
574
580
601int mixer_process(mixer_t *mixer, float *output, int num_samples);
602
621int mixer_process_excluding_source(mixer_t *mixer, float *output,
622 int num_samples, uint32_t exclude_client_id);
623
625
631
641float db_to_linear(float db);
642
652float linear_to_db(float linear);
653
666float clamp_float(float value, float min, float max);
667
669
675
686void compressor_init(compressor_t *comp, float sample_rate);
687
702void compressor_set_params(compressor_t *comp, float threshold_dB, float ratio,
703 uint64_t attack_ns, uint64_t release_ns,
704 float makeup_dB);
705
721float compressor_process_sample(compressor_t *comp, float sidechain);
722
724
730
745int ducking_init(ducking_t *duck, int num_sources, float sample_rate);
746
755void ducking_destroy(ducking_t *duck);
756
771void ducking_set_params(ducking_t *duck, float threshold_dB,
772 float leader_margin_dB, float atten_dB,
773 uint64_t attack_ns, uint64_t release_ns);
774
790void ducking_process_frame(ducking_t *duck, float *envelopes, float *gains,
791 int num_sources);
792
794
800
811void noise_gate_init(noise_gate_t *gate, float sample_rate);
812
826void noise_gate_set_params(noise_gate_t *gate, float threshold,
827 uint64_t attack_ns, uint64_t release_ns,
828 float hysteresis);
829
844float noise_gate_process_sample(noise_gate_t *gate, float input,
845 float peak_amplitude);
846
859void noise_gate_process_buffer(noise_gate_t *gate, float *buffer,
860 int num_samples);
861
871bool noise_gate_is_open(const noise_gate_t *gate);
872
874
880
892void highpass_filter_init(highpass_filter_t *filter, float cutoff_hz,
893 float sample_rate);
894
905
917float highpass_filter_process_sample(highpass_filter_t *filter, float input);
918
930void highpass_filter_process_buffer(highpass_filter_t *filter, float *buffer,
931 int num_samples);
932
934
940
952void lowpass_filter_init(lowpass_filter_t *filter, float cutoff_hz,
953 float sample_rate);
954
964
976float lowpass_filter_process_sample(lowpass_filter_t *filter, float input);
977
988void lowpass_filter_process_buffer(lowpass_filter_t *filter, float *buffer,
989 int num_samples);
990
992
998
1009float smoothstep(float t);
1010
1021int16_t float_to_int16(float sample);
1022
1032float int16_to_float(int16_t sample);
1033
1044void buffer_float_to_int16(const float *src, int16_t *dst, int count);
1045
1056void buffer_int16_to_float(const int16_t *src, float *dst, int count);
1057
1069float buffer_peak(const float *buffer, int count);
1070
1081void apply_gain_buffer(float *buffer, int count, float gain);
1082
1096void fade_buffer(float *buffer, int count, float start_gain, float end_gain);
1097
1108void fade_buffer_smooth(float *buffer, int count, bool fade_in);
1109
1122void copy_buffer_with_gain(const float *src, float *dst, int count, float gain);
1123
1125
1131
1154float soft_clip(float sample, float threshold, float steepness);
1155
1167void soft_clip_buffer(float *buffer, int num_samples, float threshold,
1168 float steepness);
1169
1171
1172#ifdef __cplusplus
1173}
1174#endif
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
float hysteresis
Hysteresis factor (0-1, prevents gate chatter).
Definition mixer.h:195
float release_coeff
Release coefficient (converted from release_ms).
Definition mixer.h:204
struct spectral_processor * spectral_left[128+1]
Definition mixer.h:360
bool noise_gate_is_open(const noise_gate_t *gate)
Check if noise gate is currently open.
Definition effects.c:193
uint8_t source_id_to_index[256]
Hash table mapping client_id → mixer source index (uses hash function for strings).
Definition mixer.h:379
float base_gain
Base gain before crowd scaling is applied.
Definition mixer.h:390
void copy_buffer_with_gain(const float *src, float *dst, int count, float gain)
Copy buffer with gain scaling.
Definition effects.c:417
float leader_margin_dB
Leader margin in dB (sources within this of loudest are leaders).
Definition mixer.h:294
void noise_gate_process_buffer(noise_gate_t *gate, float *buffer, int num_samples)
Process a buffer of samples through noise gate.
Definition effects.c:173
void buffer_float_to_int16(const float *src, int16_t *dst, int count)
Convert float buffer to int16 buffer.
Definition effects.c:356
float alpha
Filter coefficient alpha (calculated from cutoff_hz).
Definition mixer.h:236
float release_coeff
Release coefficient (converted from release_ms).
Definition mixer.h:305
float clamp_float(float value, float min, float max)
Clamp a float value to a range.
Definition effects.c:16
const char ** source_ids
Array of client ID strings (one per source slot).
Definition mixer.h:369
float ratio
Compression ratio (e.g., 4.0 for 4:1 compression).
Definition mixer.h:145
void highpass_filter_reset(highpass_filter_t *filter)
Reset high-pass filter state.
Definition effects.c:217
float sample_rate
Sample rate in Hz (set during initialization).
Definition mixer.h:233
void soft_clip_buffer(float *buffer, int num_samples, float threshold, float steepness)
Apply soft clipping to a buffer.
Definition effects.c:322
float prev_input
Previous input sample (filter state).
Definition mixer.h:238
float prev_output
Previous output sample (filter state).
Definition mixer.h:269
struct spectral_processor * spectral_right[128+1]
Definition mixer.h:361
#define MIXER_MAX_SOURCES
Maximum number of simultaneous audio sources.
Definition mixer.h:105
float highpass_filter_process_sample(highpass_filter_t *filter, float input)
Process a single sample through high-pass filter.
Definition effects.c:225
float envelope
Current envelope follower state (linear, 0-1).
Definition mixer.h:156
bool * source_active
Array of active flags (true if source is active).
Definition mixer.h:371
float cutoff_hz
Cutoff frequency in Hz (frequencies above this are attenuated).
Definition mixer.h:262
void apply_gain_buffer(float *buffer, int count, float gain)
Apply gain to buffer in-place.
Definition effects.c:385
float atten_dB
Attenuation in dB for non-leader sources.
Definition mixer.h:296
rwlock_t source_lock
Reader-writer lock protecting source arrays and bitset.
Definition mixer.h:385
atomic_t active_sources
Definition mixer.h:353
float * gain
Per-source ducking gain (linear, calculated from envelope).
Definition mixer.h:310
float attack_coeff
Attack coefficient (converted from attack_ms).
Definition mixer.h:303
mixer_t * mixer_create_channels(int max_sources, int sample_rate, unsigned channels)
Definition mixer.c:229
unsigned channels
Definition mixer.h:358
uint64_t release_ns
Ducking release time in nanoseconds.
Definition mixer.h:300
float threshold_dB
Speaking threshold in dB (sources below this are not "speaking").
Definition mixer.h:292
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:187
void noise_gate_init(noise_gate_t *gate, float sample_rate)
Initialize a noise gate.
Definition effects.c:108
float crowd_alpha
Crowd scaling exponent (typically 0.5 for sqrt scaling).
Definition mixer.h:388
void ducking_destroy(ducking_t *duck)
Free ducking system resources.
Definition mixer.c:168
float buffer_peak(const float *buffer, int count)
Find peak absolute value in buffer.
Definition effects.c:372
ducking_t ducking
Ducking system (active speaker detection and attenuation).
Definition mixer.h:393
float cutoff_hz
Cutoff frequency in Hz (frequencies below this are attenuated).
Definition mixer.h:231
int num_sources
Current number of active audio sources.
Definition mixer.h:352
float soft_clip(float sample, float threshold, float steepness)
Apply soft clipping to a sample.
Definition effects.c:307
float sample_rate
Sample rate in Hz (set during initialization).
Definition mixer.h:198
float knee_dB
Knee width in dB for soft knee (e.g., 2.0).
Definition mixer.h:143
void mixer_remove_source(mixer_t *mixer, const char *client_id)
Remove an audio source from the mixer.
Definition mixer.c:511
uint64_t active_sources_mask[((128+63)/64)]
Bitset of active sources (bit i = source i is active, O(1) iteration).
Definition mixer.h:375
void fade_buffer(float *buffer, int count, float start_gain, float end_gain)
Apply linear fade to buffer in-place.
Definition effects.c:394
void compressor_init(compressor_t *comp, float sample_rate)
Initialize a compressor.
Definition effects.c:27
float alpha
Filter coefficient alpha (calculated from cutoff_hz).
Definition mixer.h:267
void compressor_set_params(compressor_t *comp, float threshold_dB, float ratio, uint64_t attack_ns, uint64_t release_ns, float makeup_dB)
Set compressor parameters.
Definition effects.c:39
uint64_t attack_ns
Attack time in nanoseconds (how fast gate opens).
Definition mixer.h:191
int ducking_init(ducking_t *duck, int num_sources, float sample_rate)
Initialize ducking system.
Definition mixer.c:105
#define MIXER_MASK_WORDS
Definition mixer.h:106
void highpass_filter_init(highpass_filter_t *filter, float cutoff_hz, float sample_rate)
Initialize a high-pass filter.
Definition effects.c:202
float int16_to_float(int16_t sample)
Convert int16 sample to float.
Definition effects.c:354
uint64_t attack_ns
Ducking attack time in nanoseconds.
Definition mixer.h:298
float * envelope
Per-source envelope follower state (linear, allocated per source).
Definition mixer.h:308
float threshold_dB
Compression threshold in dB (e.g., -10.0).
Definition mixer.h:141
int max_sources
Maximum number of sources (allocated array sizes).
Definition mixer.h:355
float * mix_buffer
Temporary buffer for mixing operations (pre-allocated).
Definition mixer.h:398
float envelope
Current envelope follower state (linear, 0-1).
Definition mixer.h:200
float sample_rate
Sample rate in Hz (set during initialization).
Definition mixer.h:154
bool gate_open
True if gate is currently open (allowing audio through).
Definition mixer.h:206
float smoothstep(float t)
Compute smoothstep interpolation.
Definition effects.c:337
void fade_buffer_smooth(float *buffer, int count, bool fade_in)
Apply smoothstep fade to buffer in-place.
Definition effects.c:406
int16_t float_to_int16(float sample)
Convert float sample to int16 (WebRTC format).
Definition effects.c:345
compressor_t compressor
Compressor (dynamic range compression).
Definition mixer.h:395
float sample_rate
Sample rate in Hz (set during initialization).
Definition mixer.h:264
void lowpass_filter_init(lowpass_filter_t *filter, float cutoff_hz, float sample_rate)
Initialize a low-pass filter.
Definition effects.c:255
mixer_t * mixer_create(int max_sources, int sample_rate)
Create a new audio mixer.
Definition mixer.c:225
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:759
float db_to_linear(float db)
Convert decibels to linear gain.
Definition effects.c:10
const char * source_id_at_hash[256]
Client ID strings stored at each hash index for collision detection.
Definition mixer.h:382
void lowpass_filter_process_buffer(lowpass_filter_t *filter, float *buffer, int num_samples)
Process a buffer of samples through low-pass filter.
Definition effects.c:292
uint64_t release_ns
Release time in nanoseconds (how fast gate closes).
Definition mixer.h:193
#define MIXER_HASH_INVALID
Invalid index sentinel value for mixer hash table.
Definition mixer.h:507
void highpass_filter_process_buffer(highpass_filter_t *filter, float *buffer, int num_samples)
Process a buffer of samples through high-pass filter.
Definition effects.c:240
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:177
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:560
audio_ring_buffer_t ** source_buffers
Array of pointers to client audio ring buffers.
Definition mixer.h:364
struct mixer_listener_buffer * listener_buffers[128][128]
Definition mixer.h:366
int mixer_source_count(const mixer_t *mixer)
Definition mixer.c:556
float makeup_dB
Makeup gain in dB (compensates for gain reduction).
Definition mixer.h:151
float compressor_process_sample(compressor_t *comp, float sidechain)
Process a single sample through compressor.
Definition effects.c:76
void mixer_destroy(mixer_t *mixer)
Destroy a mixer and free all resources.
Definition mixer.c:411
int sample_rate
Sample rate in Hz (e.g., 44100).
Definition mixer.h:357
uint64_t attack_ns
Attack time in nanoseconds (how fast compression kicks in).
Definition mixer.h:147
float lowpass_filter_process_sample(lowpass_filter_t *filter, float input)
Process a single sample through low-pass filter.
Definition effects.c:279
void noise_gate_set_params(noise_gate_t *gate, float threshold, uint64_t attack_ns, uint64_t release_ns, float hysteresis)
Set noise gate parameters.
Definition effects.c:123
float linear_to_db(float linear)
Convert linear gain to decibels.
Definition effects.c:12
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:443
uint64_t release_ns
Release time in nanoseconds (how fast compression releases).
Definition mixer.h:149
void lowpass_filter_reset(lowpass_filter_t *filter)
Reset low-pass filter state.
Definition effects.c:272
float attack_coeff
Attack coefficient (converted from attack_ms).
Definition mixer.h:160
int mixer_process(mixer_t *mixer, float *output, int num_samples)
Process audio from all active sources.
Definition mixer.c:589
void buffer_int16_to_float(const int16_t *src, float *dst, int count)
Convert int16 buffer to float buffer.
Definition effects.c:364
float threshold
Gate threshold in linear units (e.g., 0.01f for -40dB).
Definition mixer.h:189
float prev_output
Previous output sample (filter state).
Definition mixer.h:240
float release_coeff
Release coefficient (converted from release_ms).
Definition mixer.h:162
float gain_lin
Current gain multiplier (linear, calculated from envelope).
Definition mixer.h:158
float attack_coeff
Attack coefficient (converted from attack_ms).
Definition mixer.h:202
float noise_gate_process_sample(noise_gate_t *gate, float input, float peak_amplitude)
Process a single sample through noise gate.
Definition effects.c:145
unsigned int uint32_t
unsigned long long uint64_t
unsigned char uint8_t
struct audio_ring_buffer audio_ring_buffer_t
Audio ring buffer for real-time audio streaming.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Lock-Free Ring Buffer and Frame Buffer Management.
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80
Dynamic range compressor settings and state.
Definition mixer.h:139
Ducking system settings and state.
Definition mixer.h:290
noise gate keeps the values that move through the subsystem: threshold, attack_ns,...
Definition mixer.h:229
This is the handoff point for highpass filter. Callers fill cutoff_hz, sample_rate,...
Definition mixer.h:260
Owns the temporary storage used by mixer listener buffer. The data pointers and size or cursor fields...
Definition mixer.c:33
For ducking, this is the live record that ties threshold_dB, leader_margin_dB, atten_dB,...
Definition mixer.h:350
This is the handoff point for compressor. Callers fill threshold_dB, knee_dB, ratio,...
Definition mixer.h:187
spectral processor keeps the values that move through the subsystem: config, forward,...
Definition spectral.c:18