|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
Unified client-side audio processing pipeline. More...
Go to the source code of this file.
Data Structures | |
| struct | client_audio_pipeline_flags_t |
| Component enable/disable flags. More... | |
| struct | client_audio_pipeline_config_t |
| Pipeline configuration parameters. More... | |
| struct | client_audio_pipeline_t |
| Client audio pipeline state. More... | |
Macros | |
| #define | CLIENT_AUDIO_PIPELINE_FLAGS_ALL |
| Default flags with all processing enabled. | |
| #define | CLIENT_AUDIO_PIPELINE_FLAGS_MINIMAL |
| Minimal flags for testing (only codec, no processing) | |
Audio Pipeline Constants | |
| #define | CLIENT_AUDIO_PIPELINE_SAMPLE_RATE 48000 |
| #define | CLIENT_AUDIO_PIPELINE_FRAME_MS 20 |
| #define | CLIENT_AUDIO_PIPELINE_FRAME_SIZE (CLIENT_AUDIO_PIPELINE_SAMPLE_RATE * CLIENT_AUDIO_PIPELINE_FRAME_MS / 1000) |
| #define | CLIENT_AUDIO_PIPELINE_ECHO_REF_SIZE (CLIENT_AUDIO_PIPELINE_SAMPLE_RATE / 2) |
| #define | CLIENT_AUDIO_PIPELINE_MAX_OPUS_PACKET 4000 |
Typedefs | |
| typedef struct SpeexPreprocessState_ | SpeexPreprocessState |
| typedef struct JitterBuffer_ | JitterBuffer |
| typedef struct webrtc_EchoCanceller3 | webrtc_EchoCanceller3 |
| typedef struct OpusEncoder | OpusEncoder |
| typedef struct OpusDecoder | OpusDecoder |
Functions | |
| client_audio_pipeline_config_t | client_audio_pipeline_default_config (void) |
| Get default configuration. | |
Lifecycle Functions | |
| client_audio_pipeline_t * | client_audio_pipeline_create (const client_audio_pipeline_config_t *config) |
| Create a new client audio pipeline. | |
| void | client_audio_pipeline_destroy (client_audio_pipeline_t *pipeline) |
| Destroy a client audio pipeline. | |
Flag Management | |
| void | client_audio_pipeline_set_flags (client_audio_pipeline_t *pipeline, client_audio_pipeline_flags_t flags) |
| Set component enable flags. | |
| client_audio_pipeline_flags_t | client_audio_pipeline_get_flags (client_audio_pipeline_t *pipeline) |
| Get current component enable flags. | |
Capture Path (Microphone → Network) | |
| int | client_audio_pipeline_capture (client_audio_pipeline_t *pipeline, const float *input, int num_samples, uint8_t *opus_out, int opus_out_size) |
| Process captured audio and encode to Opus. | |
Playback Path (Network → Speakers) | |
| int | client_audio_pipeline_playback (client_audio_pipeline_t *pipeline, const uint8_t *opus_in, int opus_len, float *output, int max_samples) |
| Decode Opus packet and process for playback. | |
| int | client_audio_pipeline_get_playback_frame (client_audio_pipeline_t *pipeline, float *output, int num_samples) |
| Get audio frame from jitter buffer for playback callback. | |
Full-Duplex AEC3 Processing | |
| void | client_audio_pipeline_process_duplex (client_audio_pipeline_t *pipeline, const float *render_samples, int render_count, const float *capture_samples, int capture_count, float *processed_output) |
| Process AEC3 inline in full-duplex callback. | |
Status and Diagnostics | |
| int | client_audio_pipeline_jitter_margin (client_audio_pipeline_t *pipeline) |
| Get jitter buffer margin (buffered time in ms) | |
| bool | client_audio_pipeline_voice_detected (client_audio_pipeline_t *pipeline) |
| Check if VAD detected voice activity in last capture. | |
| void | client_audio_pipeline_reset (client_audio_pipeline_t *pipeline) |
| Reset pipeline state. | |
Unified client-side audio processing pipeline.
This header provides a complete audio processing pipeline for ascii-chat clients, integrating WebRTC AEC3 (production-grade echo cancellation), SpeexDSP (noise suppression, AGC, VAD), Speex jitter buffer, Opus codec, and mixer components (compression, noise gate, filters).
CAPTURE PATH (microphone → network): Mic Input (float32, 48kHz) ↓ Echo Cancellation (WebRTC AEC3 - automatic network delay estimation + adaptive filtering) ↓ Preprocessor (Noise/AGC/VAD) ↓ High-Pass Filter (remove rumble) ↓ Low-Pass Filter (remove hiss) ↓ Noise Gate (silence detection) ↓ Compressor (dynamic range control) ↓ Opus Encode → Network
PLAYBACK PATH (network → speakers): Network ↓ Speex Jitter Buffer ↓ Opus Decode ↓ Register with AEC (WebRTC AEC3 reference signal for echo learning) ↓ Soft Clipping → Speakers
Each processing stage can be individually enabled/disabled via flags. This allows testing and debugging specific components.
Definition in file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_ECHO_REF_SIZE (CLIENT_AUDIO_PIPELINE_SAMPLE_RATE / 2) |
Echo reference ring buffer size (500ms at 48kHz)
Definition at line 96 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_FLAGS_ALL |
Default flags with all processing enabled.
Definition at line 133 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_FLAGS_MINIMAL |
Minimal flags for testing (only codec, no processing)
Definition at line 149 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_FRAME_MS 20 |
Default frame size in milliseconds
Definition at line 90 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_FRAME_SIZE (CLIENT_AUDIO_PIPELINE_SAMPLE_RATE * CLIENT_AUDIO_PIPELINE_FRAME_MS / 1000) |
Default frame size in samples at 48kHz
Definition at line 93 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_MAX_OPUS_PACKET 4000 |
Maximum Opus packet size
Definition at line 99 of file client_audio_pipeline.h.
| #define CLIENT_AUDIO_PIPELINE_SAMPLE_RATE 48000 |
Default sample rate (48kHz, native for Opus)
Definition at line 87 of file client_audio_pipeline.h.
| typedef struct JitterBuffer_ JitterBuffer |
Definition at line 68 of file client_audio_pipeline.h.
| typedef struct OpusDecoder OpusDecoder |
Definition at line 75 of file client_audio_pipeline.h.
| typedef struct OpusEncoder OpusEncoder |
Definition at line 74 of file client_audio_pipeline.h.
| typedef struct SpeexPreprocessState_ SpeexPreprocessState |
Definition at line 67 of file client_audio_pipeline.h.
| typedef struct webrtc_EchoCanceller3 webrtc_EchoCanceller3 |
Definition at line 71 of file client_audio_pipeline.h.
| int client_audio_pipeline_capture | ( | client_audio_pipeline_t * | pipeline, |
| const float * | input, | ||
| int | num_samples, | ||
| uint8_t * | opus_out, | ||
| int | max_opus_len | ||
| ) |
Process captured audio and encode to Opus.
| pipeline | Pipeline instance |
| input | Input samples (float32, -1.0 to 1.0) |
| num_samples | Number of input samples (should match frame_size) |
| opus_out | Output buffer for Opus packet |
| opus_out_size | Size of opus_out buffer (should be >= MAX_OPUS_PACKET) |
Processing pipeline (when flags enabled):
Encode already-processed audio to Opus.
In full-duplex mode, AEC3 and DSP processing are done in process_duplex(). This function just does Opus encoding.
Definition at line 431 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::encoder, client_audio_pipeline_t::frame_size, and log_error.
| client_audio_pipeline_t * client_audio_pipeline_create | ( | const client_audio_pipeline_config_t * | config | ) |
Create a new client audio pipeline.
| config | Pipeline configuration (NULL for defaults) |
Allocates and initializes all audio processing components. Must be destroyed with client_audio_pipeline_destroy().
Create a new client audio pipeline.
This function:
Definition at line 153 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::aec3_capture_buffer, client_audio_pipeline_t::aec3_render_buffer, client_audio_pipeline_t::capture_fadein_remaining, client_audio_pipeline_default_config(), client_audio_pipeline_config_t::comp_attack_ms, client_audio_pipeline_config_t::comp_makeup_db, client_audio_pipeline_config_t::comp_ratio, client_audio_pipeline_config_t::comp_release_ms, client_audio_pipeline_config_t::comp_threshold_db, client_audio_pipeline_t::compressor, compressor_init(), compressor_set_params(), client_audio_pipeline_t::config, client_audio_pipeline_t::debug_wav_aec3_in, client_audio_pipeline_t::debug_wav_aec3_out, client_audio_pipeline_t::decoder, client_audio_pipeline_flags_t::echo_cancel, client_audio_pipeline_t::echo_canceller, client_audio_pipeline_t::encoder, client_audio_pipeline_config_t::flags, client_audio_pipeline_t::flags, client_audio_pipeline_t::frame_size, client_audio_pipeline_config_t::frame_size_ms, client_audio_pipeline_config_t::gate_attack_ms, client_audio_pipeline_config_t::gate_hysteresis, client_audio_pipeline_config_t::gate_release_ms, client_audio_pipeline_config_t::gate_threshold, client_audio_pipeline_t::highpass, highpass_filter_init(), client_audio_pipeline_config_t::highpass_hz, client_audio_pipeline_t::initialized, log_error, log_info, log_warn, client_audio_pipeline_t::lowpass, lowpass_filter_init(), client_audio_pipeline_config_t::lowpass_hz, client_audio_pipeline_t::noise_gate, noise_gate_init(), noise_gate_set_params(), OPUS_APPLICATION_VOIP, client_audio_pipeline_config_t::opus_bitrate, client_audio_pipeline_t::playback_noise_gate, SAFE_CALLOC, SAFE_FREE, client_audio_pipeline_config_t::sample_rate, and wav_writer_open().
Referenced by audio_client_init().
| client_audio_pipeline_config_t client_audio_pipeline_default_config | ( | void | ) |
Get default configuration.
Definition at line 100 of file client_audio_pipeline.cpp.
References CLIENT_AUDIO_PIPELINE_FLAGS_ALL, CLIENT_AUDIO_PIPELINE_FRAME_MS, CLIENT_AUDIO_PIPELINE_SAMPLE_RATE, and client_audio_pipeline_config_t::sample_rate.
Referenced by audio_client_init(), and client_audio_pipeline_create().
| void client_audio_pipeline_destroy | ( | client_audio_pipeline_t * | pipeline | ) |
Destroy a client audio pipeline.
| pipeline | Pipeline to destroy (can be NULL) |
Frees all resources including SpeexDSP states, Opus codec, and all work buffers.
Definition at line 360 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::aec3_capture_buffer, client_audio_pipeline_t::aec3_render_buffer, client_audio_pipeline_t::debug_wav_aec3_in, client_audio_pipeline_t::debug_wav_aec3_out, client_audio_pipeline_t::decoder, client_audio_pipeline_t::echo_canceller, client_audio_pipeline_t::encoder, SAFE_FREE, and wav_writer_close().
Referenced by audio_cleanup(), and audio_client_init().
| client_audio_pipeline_flags_t client_audio_pipeline_get_flags | ( | client_audio_pipeline_t * | pipeline | ) |
Get current component enable flags.
| pipeline | Pipeline instance |
Definition at line 414 of file client_audio_pipeline.cpp.
References CLIENT_AUDIO_PIPELINE_FLAGS_MINIMAL, and client_audio_pipeline_t::flags.
| int client_audio_pipeline_get_playback_frame | ( | client_audio_pipeline_t * | pipeline, |
| float * | output, | ||
| int | num_samples | ||
| ) |
Get audio frame from jitter buffer for playback callback.
| pipeline | Pipeline instance |
| output | Output buffer for decoded samples (float32) |
| num_samples | Number of samples to retrieve |
This is called from the audio output callback to get the next frame of audio. It pulls from the jitter buffer and decodes.
Get a processed playback frame (currently just returns decoded frame)
Definition at line 482 of file client_audio_pipeline.cpp.
| int client_audio_pipeline_jitter_margin | ( | client_audio_pipeline_t * | pipeline | ) |
Get jitter buffer margin (buffered time in ms)
| pipeline | Pipeline instance |
Get jitter buffer margin
Definition at line 664 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::config, and client_audio_pipeline_config_t::jitter_margin_ms.
| int client_audio_pipeline_playback | ( | client_audio_pipeline_t * | pipeline, |
| const uint8_t * | opus_in, | ||
| int | opus_len, | ||
| float * | output, | ||
| int | num_samples | ||
| ) |
Decode Opus packet and process for playback.
| pipeline | Pipeline instance |
| opus_in | Opus packet data (can be NULL for packet loss concealment) |
| opus_len | Opus packet length (0 if opus_in is NULL) |
| output | Output buffer for decoded samples (float32) |
| max_samples | Maximum samples to write to output |
Processing pipeline (when flags enabled):
Note: Output should be fed to speakers AND to echo reference via client_audio_pipeline_feed_echo_ref().
Process network playback (decode and register with echo canceller as reference)
Definition at line 452 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::decoder, log_error, noise_gate_process_buffer(), and client_audio_pipeline_t::playback_noise_gate.
Referenced by audio_decode_opus().
| void client_audio_pipeline_process_duplex | ( | client_audio_pipeline_t * | pipeline, |
| const float * | render_samples, | ||
| int | render_count, | ||
| const float * | capture_samples, | ||
| int | capture_count, | ||
| float * | processed_output | ||
| ) |
Process AEC3 inline in full-duplex callback.
| pipeline | Pipeline instance |
| render_samples | Audio samples being played to speakers RIGHT NOW |
| render_count | Number of render samples |
| capture_samples | Audio samples from microphone RIGHT NOW |
| capture_count | Number of capture samples |
| processed_output | Output buffer for processed capture (must be capture_count size) |
Called from PortAudio's single full-duplex callback where render and capture happen at the EXACT same instant. No ring buffers, no timing mismatch.
Processing:
REAL-TIME SAFE: No mutexes, no allocations, no blocking.
Process AEC3 inline in full-duplex callback (REAL-TIME SAFE).
This is the PROFESSIONAL approach to AEC3 timing:
This function does ALL AEC3 processing inline:
Returns processed capture samples in processed_output. Opus encoding is done separately by the encoding thread.
Definition at line 509 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::aec3_capture_buffer, client_audio_pipeline_t::aec3_render_buffer, audio_analysis_set_aec3_metrics(), client_audio_pipeline_t::capture_fadein_remaining, client_audio_pipeline_flags_t::compressor, client_audio_pipeline_t::compressor, compressor_process_sample(), client_audio_pipeline_t::config, copy_buffer_with_gain(), client_audio_pipeline_t::debug_wav_aec3_in, client_audio_pipeline_t::debug_wav_aec3_out, client_audio_pipeline_flags_t::echo_cancel, client_audio_pipeline_t::echo_canceller, client_audio_pipeline_t::flags, client_audio_pipeline_flags_t::highpass, client_audio_pipeline_t::highpass, highpass_filter_process_buffer(), log_info, log_warn, log_warn_every, client_audio_pipeline_flags_t::lowpass, client_audio_pipeline_t::lowpass, lowpass_filter_process_buffer(), client_audio_pipeline_flags_t::noise_gate, client_audio_pipeline_t::noise_gate, noise_gate_process_buffer(), platform_getenv(), client_audio_pipeline_config_t::sample_rate, smoothstep(), soft_clip(), soft_clip_buffer(), and wav_writer_write().
| void client_audio_pipeline_reset | ( | client_audio_pipeline_t * | pipeline | ) |
Reset pipeline state.
| pipeline | Pipeline instance |
Resets echo canceller, jitter buffer, and filter states. Call when starting a new audio session.
Reset pipeline state
Definition at line 673 of file client_audio_pipeline.cpp.
References log_info.
| void client_audio_pipeline_set_flags | ( | client_audio_pipeline_t * | pipeline, |
| client_audio_pipeline_flags_t | flags | ||
| ) |
Set component enable flags.
| pipeline | Pipeline instance |
| flags | New flags to set |
Thread-safe. Changes take effect on next capture/playback call.
Definition at line 407 of file client_audio_pipeline.cpp.
References client_audio_pipeline_t::flags.
| bool client_audio_pipeline_voice_detected | ( | client_audio_pipeline_t * | pipeline | ) |
Check if VAD detected voice activity in last capture.
| pipeline | Pipeline instance |