### SpeexDSP Audio Resampling C Example Source: https://context7.com/xiph/speexdsp/llms.txt Demonstrates how to resample audio from one sampling rate to another using the SpeexDSP library. It initializes the resampler, processes an audio buffer, and cleans up resources. The quality can be configured from 0 (poor) to 10 (very high). ```c #include #include #include int main() { // Resample 8kHz mono audio to 48kHz int err; spx_uint32_t in_rate = 8000; spx_uint32_t out_rate = 48000; int quality = SPEEX_RESAMPLER_QUALITY_DESKTOP; SpeexResamplerState *resampler = speex_resampler_init( 1, // mono channel in_rate, out_rate, quality, &err ); if (err != RESAMPLER_ERR_SUCCESS) { fprintf(stderr, "Error: %s\n", speex_resampler_strerror(err)); return 1; } // Process audio buffer spx_int16_t input[160]; // 20ms at 8kHz spx_int16_t output[960]; // 20ms at 48kHz spx_uint32_t in_len = 160; spx_uint32_t out_len = 960; // Fill input buffer with audio data... int result = speex_resampler_process_int( resampler, 0, // channel index input, &in_len, // updated with samples consumed output, &out_len // updated with samples written ); printf("Processed %u input samples, generated %u output samples\n", in_len, out_len); speex_resampler_destroy(resampler); return 0; } ``` -------------------------------- ### Speex Speech Decoding Initialization and Processing (C) Source: https://github.com/xiph/speexdsp/blob/master/doc/programming.html Illustrates the C code for initializing a Speex decoder, configuring options like perceptual filtering, decoding speech frames from a bitstream, and handling lost frames. Resources are freed upon completion. ```c #include SpeexBits bits; void *dec_state; int frame_size; int pf = 1; // Enable perceptual filter speex_bits_init(&bits); dec_state = speex_decoder_init(&speex_nb_mode); speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size); speex_decoder_ctl(dec_state, SPEEX_SET_PF, &pf); // For each input frame: speex_bits_read_from(&bits, input_bytes, nbBytes); speex_decode(st, &bits, output_frame, 0); // 0 for normal, 1 for lost frame speex_bits_destroy(&bits); speex_decoder_destroy(&dec_state); ``` -------------------------------- ### Speex Speech Encoding Initialization and Processing (C) Source: https://github.com/xiph/speexdsp/blob/master/doc/programming.html Demonstrates the C code required to initialize a Speex encoder, set parameters like frame size, encode speech frames, and manage the encoded bitstream. It includes freeing resources after encoding. ```c #include SpeexBits bits; void *enc_state; int frame_size; speex_bits_init(&bits); enc_state = speex_encoder_init(&speex_nb_mode); speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size); // For each input frame: speex_bits_reset(&bits); speex_encode(enc_state, input_frame, &bits); nbBytes = speex_bits_write(&bits, byte_ptr, MAX_NB_BYTES); speex_bits_destroy(&bits); speex_encoder_destroy(&enc_state); ``` -------------------------------- ### SpeexDSP Echo Cancellation C Example Source: https://context7.com/xiph/speexdsp/llms.txt Illustrates the implementation of an acoustic echo canceller using SpeexDSP. This function removes speaker output from microphone input, crucial for full-duplex communication. It supports adaptive filtering for single and multi-channel configurations. ```c #include #include int main() { int sample_rate = 16000; int frame_size = 320; // 20ms at 16kHz int filter_length = 3200; // 200ms echo tail SpeexEchoState *echo = speex_echo_state_init(frame_size, filter_length); // Set sampling rate speex_echo_ctl(echo, SPEEX_ECHO_SET_SAMPLING_RATE, &sample_rate); spx_int16_t mic_input[320]; // microphone (near + far echo) spx_int16_t speaker_output[320]; // speaker playback (far end) spx_int16_t clean_output[320]; // echo-cancelled result // Continuous processing loop while (1) { // Get microphone frame and speaker frame... // Perform echo cancellation speex_echo_cancellation( echo, mic_input, speaker_output, clean_output ); // clean_output now contains near-end signal with echo removed // Process or transmit clean_output... } speex_echo_state_destroy(echo); return 0; } ``` -------------------------------- ### Ring Buffer for Audio Sample Storage in C Source: https://context7.com/xiph/speexdsp/llms.txt Provides a simple, thread-unsafe ring buffer for audio sample storage, suitable for single-threaded applications. It requires the Speex buffer library. Functions allow writing, reading, checking available space, and resizing the buffer. ```c #include #include int main() { int buffer_size = 4096; // bytes SpeexBuffer *buffer = speex_buffer_init(buffer_size); // Write audio data short audio_samples[320]; // Fill audio_samples... int written = speex_buffer_write(buffer, audio_samples, 320 * sizeof(short)); if (written < 320 * sizeof(short)) { // Buffer full, handle overflow } // Write silence speex_buffer_writezeros(buffer, 160 * sizeof(short)); // Check available data int available = speex_buffer_get_available(buffer); // Read data short output[320]; int read = speex_buffer_read(buffer, output, 320 * sizeof(short)); if (read < 320 * sizeof(short)) { // Not enough data available } // Resize buffer if needed speex_buffer_resize(buffer, 8192); speex_buffer_destroy(buffer); return 0; } ``` -------------------------------- ### Advanced Resampler Configuration using SpeexDSP Source: https://context7.com/xiph/speexdsp/llms.txt Shows how to configure and use the SpeexDSP resampler for fractional resampling with custom quality and stride settings. It handles interleaved stereo audio buffers and allows runtime quality adjustments and latency retrieval. Requires the speex_resampler library. ```c #include int main() { int err; // Fractional resampling: 44100 Hz to 48000 Hz // Ratio = 48000/44100 = 160/147 SpeexResamplerState *resampler = speex_resampler_init_frac( 2, // stereo 160, 147, // rational ratio 44100, // approximate input rate 48000, // approximate output rate SPEEX_RESAMPLER_QUALITY_DEFAULT, &err ); // Change quality at runtime int quality = SPEEX_RESAMPLER_QUALITY_VOIP; speex_resampler_set_quality(resampler, quality); // Set stride for interleaved stereo processing spx_uint32_t stride = 2; speex_resampler_set_input_stride(resampler, stride); speex_resampler_set_output_stride(resampler, stride); // Process interleaved stereo float input[882 * 2]; // 20ms stereo at 44.1kHz float output[960 * 2]; // 20ms stereo at 48kHz spx_uint32_t in_len = 882; spx_uint32_t out_len = 960; speex_resampler_process_interleaved_float( resampler, input, &in_len, output, &out_len ); // Get latency information int input_latency = speex_resampler_get_input_latency(resampler); int output_latency = speex_resampler_get_output_latency(resampler); // Reset internal state for new stream speex_resampler_reset_mem(resampler); speex_resampler_destroy(resampler); return 0; } ``` -------------------------------- ### Ring Buffer API Source: https://context7.com/xiph/speexdsp/llms.txt A simple, thread-unsafe ring buffer implementation for audio sample storage, suitable for single-threaded applications requiring custom buffering mechanisms. ```APIDOC ## Ring Buffer ### Description Simple thread-unsafe ring buffer for audio sample storage, useful for implementing custom buffering schemes in single-threaded applications. ### Initialization ```c SpeexBuffer *speex_buffer_init(int buffer_size); ``` Initializes a ring buffer with the specified `buffer_size` in bytes. ### Writing Data ```c int speex_buffer_write(SpeexBuffer *buffer, const void *data, int len); void speex_buffer_writezeros(SpeexBuffer *buffer, int len); ``` - `speex_buffer_write`: Writes `len` bytes from `data` into the buffer. Returns the number of bytes actually written. - `speex_buffer_writezeros`: Writes `len` zero bytes into the buffer. ### Reading Data ```c int speex_buffer_read(SpeexBuffer *buffer, void *data, int len); ``` Reads up to `len` bytes from the buffer into `data`. Returns the number of bytes actually read. ### Buffer Information ```c int speex_buffer_get_available(SpeexBuffer *buffer); ``` Returns the number of bytes currently available in the buffer. ### Resizing ```c int speex_buffer_resize(SpeexBuffer *buffer, int new_size); ``` Resizes the buffer to `new_size` bytes. Returns 0 on success, -1 on failure. ### Destruction ```c void speex_buffer_destroy(SpeexBuffer *buffer); ``` ### Example ```c #include #include int main() { int buffer_size = 4096; SpeexBuffer *buffer = speex_buffer_init(buffer_size); short audio_samples[320]; // Fill audio_samples... int written = speex_buffer_write(buffer, audio_samples, 320 * sizeof(short)); speex_buffer_writezeros(buffer, 160 * sizeof(short)); int available = speex_buffer_get_available(buffer); short output[320]; int read = speex_buffer_read(buffer, output, 320 * sizeof(short)); speex_buffer_resize(buffer, 8192); speex_buffer_destroy(buffer); return 0; } ``` ``` -------------------------------- ### Audio Preprocessing API Source: https://context7.com/xiph/speexdsp/llms.txt This API provides a multi-function audio preprocessor with capabilities for noise suppression, residual echo suppression, automatic gain control (AGC), and voice activity detection (VAD). ```APIDOC ## Audio Preprocessing ### Description Multi-function audio preprocessor providing noise suppression, residual echo suppression, automatic gain control (AGC), and voice activity detection (VAD) in a single efficient module. ### Initialization ```c SpeexPreprocessState *speex_preprocess_state_init(int frame_size, int sample_rate); ``` ### Control Functions ```c int speex_preprocess_ctl(SpeexPreprocessState *state, int request, void *data); ``` **Control Requests:** - `SPEEX_PREPROCESS_SET_DENOISE`: Enable/disable noise suppression (int). - `SPEEX_PREPROCESS_SET_NOISE_SUPPRESS`: Set noise suppression level (int, e.g., -30). - `SPEEX_PREPROCESS_SET_AGC`: Enable/disable AGC (int). - `SPEEX_PREPROCESS_SET_AGC_LEVEL`: Set AGC target level (int). - `SPEEX_PREPROCESS_SET_VAD`: Enable/disable VAD (int). - `SPEEX_PREPROCESS_SET_ECHO_STATE`: Link with an echo canceller state (SpeexEchoState *). ### Processing ```c int speex_preprocess_run(SpeexPreprocessState *state, spx_int16_t *audio_frame); ``` Returns 1 if the frame contains speech, 0 otherwise. ### Destruction ```c void speex_preprocess_state_destroy(SpeexPreprocessState *state); ``` ### Example ```c #include #include int main() { int frame_size = 320; int sample_rate = 16000; SpeexPreprocessState *preprocess = speex_preprocess_state_init(frame_size, sample_rate); int denoise = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_DENOISE, &denoise); int noise_suppress = -30; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noise_suppress); int agc = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC, &agc); int agc_level = 24000; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC_LEVEL, &agc_level); int vad = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_VAD, &vad); SpeexEchoState *echo = speex_echo_state_init(frame_size, 3200); speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo); spx_int16_t audio_frame[320]; while (1) { // Get audio frame... int is_speech = speex_preprocess_run(preprocess, audio_frame); if (is_speech) { // Process speech } } speex_preprocess_state_destroy(preprocess); speex_echo_state_destroy(echo); return 0; } ``` ``` -------------------------------- ### Jitter Buffer for VoIP Packet Reordering and Loss Handling in C Source: https://context7.com/xiph/speexdsp/llms.txt Implements an adaptive jitter buffer for VoIP applications that reorders packets, handles packet loss, and dynamically adjusts buffering. It requires the Speex jitter library. The buffer takes incoming packets and provides them for playback, handling missing packets with concealment or silence insertion. ```c #include int main() { int step_size = 160; // 20ms at 8kHz JitterBuffer *jitter = jitter_buffer_init(step_size); // Configure jitter buffer int margin = 5; // extra buffering margin jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MARGIN, &margin); int max_late_rate = 5; // 5% max late packet rate jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MAX_LATE_RATE, &max_late_rate); // Receiving packets (in network thread) JitterBufferPacket incoming_packet; incoming_packet.data = malloc(200); incoming_packet.len = 160; incoming_packet.timestamp = 1000; incoming_packet.span = 160; incoming_packet.sequence = 42; // Copy received RTP payload to incoming_packet.data... jitter_buffer_put(jitter, &incoming_packet); // Playback thread JitterBufferPacket output_packet; output_packet.data = malloc(200); output_packet.len = 200; spx_int32_t start_offset; int result = jitter_buffer_get(jitter, &output_packet, 160, &start_offset); if (result == JITTER_BUFFER_OK) { // Packet retrieved successfully, decode and play } else if (result == JITTER_BUFFER_MISSING) { // Packet lost, perform concealment } else if (result == JITTER_BUFFER_INSERTION) { // Insert silence to increase buffering } jitter_buffer_tick(jitter); // Advance time jitter_buffer_destroy(jitter); return 0; } ``` -------------------------------- ### Multi-Channel Echo Cancellation using SpeexDSP Source: https://context7.com/xiph/speexdsp/llms.txt Demonstrates initializing and using the SpeexDSP multi-channel echo canceller (AEC) with speaker decorrelation. It takes interleaved microphone and decorrelated speaker audio as input and outputs cleaned audio. Requires the speex_echo and speex_decorrelate libraries. ```c #include #include int main() { int frame_size = 320; int filter_length = 3200; int nb_microphones = 2; int nb_speakers = 2; // Create multi-channel echo canceller SpeexEchoState *echo = speex_echo_state_init_mc( frame_size, filter_length, nb_microphones, nb_speakers ); // Create decorrelator for speakers int sample_rate = 16000; SpeexDecorrState *decorr = speex_decorrelate_new( sample_rate, nb_speakers, frame_size ); // Interleaved stereo buffers spx_int16_t mic_input[640]; // 2 mics, 320 samples each spx_int16_t speaker_output[640]; // 2 speakers, 320 samples each spx_int16_t decorrelated[640]; spx_int16_t clean_output[640]; // Decorrelate speaker channels to improve AEC convergence int strength = 50; // 0-100, amount of decorrelation speex_decorrelate(decorr, speaker_output, decorrelated, strength); // Perform echo cancellation speex_echo_cancellation(echo, mic_input, decorrelated, clean_output); speex_decorrelate_destroy(decorr); speex_echo_state_destroy(echo); return 0; } ``` -------------------------------- ### Audio Preprocessing with Noise Suppression, AGC, and VAD in C Source: https://context7.com/xiph/speexdsp/llms.txt Implements a multi-function audio preprocessor for noise suppression, residual echo suppression, automatic gain control (AGC), and voice activity detection (VAD). It requires the Speex preprocess and echo libraries. The function takes an audio frame and returns whether speech is detected. ```c #include #include int main() { int frame_size = 320; // 20ms int sample_rate = 16000; SpeexPreprocessState *preprocess = speex_preprocess_state_init(frame_size, sample_rate); // Enable noise suppression int denoise = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_DENOISE, &denoise); // Set noise suppression level (-30 dB maximum attenuation) int noise_suppress = -30; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noise_suppress); // Enable AGC int agc = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC, &agc); // Set AGC target level int agc_level = 24000; // target level speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC_LEVEL, &agc_level); // Enable VAD int vad = 1; speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_VAD, &vad); // Optional: Link with echo canceller for residual echo suppression SpeexEchoState *echo = speex_echo_state_init(frame_size, 3200); speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_ECHO_STATE, echo); spx_int16_t audio_frame[320]; while (1) { // Get audio frame... int is_speech = speex_preprocess_run(preprocess, audio_frame); if (is_speech) { // Frame contains speech, process/transmit } else { // Frame is silence/noise } } speex_preprocess_state_destroy(preprocess); speex_echo_state_destroy(echo); return 0; } ``` -------------------------------- ### Jitter Buffer API Source: https://context7.com/xiph/speexdsp/llms.txt This API provides an adaptive jitter buffer designed for VoIP applications. It handles packet reordering, loss, and dynamically adjusts buffering to optimize latency and quality. ```APIDOC ## Jitter Buffer ### Description Adaptive jitter buffer for VoIP applications that reorders packets, handles packet loss, and dynamically adjusts buffering to balance latency and quality. ### Initialization ```c JitterBuffer *jitter_buffer_init(int step_size); ``` `step_size` is the number of samples per interval (e.g., 160 for 20ms at 8kHz). ### Control Functions ```c int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *data); ``` **Control Requests:** - `JITTER_BUFFER_SET_MARGIN`: Set extra buffering margin (int). - `JITTER_BUFFER_SET_MAX_LATE_RATE`: Set maximum acceptable late packet rate (int, e.g., 5 for 5%). ### Packet Handling ```c int jitter_buffer_put(JitterBuffer *jitter, JitterBufferPacket *packet); int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, int desired_span, spx_int32_t *start_offset); ``` - `jitter_buffer_put`: Adds a received packet to the buffer. - `jitter_buffer_get`: Retrieves a packet for playback. - `desired_span`: The number of samples expected. - `start_offset`: Output parameter indicating the offset from the ideal playback time. **Return values for `jitter_buffer_get`:** - `JITTER_BUFFER_OK`: Packet retrieved successfully. - `JITTER_BUFFER_MISSING`: Packet was lost. - `JITTER_BUFFER_INSERTION`: Silence inserted to increase buffering. ### Time Advancement ```c void jitter_buffer_tick(JitterBuffer *jitter); ``` Advances the jitter buffer's internal time. ### Destruction ```c void jitter_buffer_destroy(JitterBuffer *jitter); ``` ### Example ```c #include int main() { int step_size = 160; JitterBuffer *jitter = jitter_buffer_init(step_size); int margin = 5; jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MARGIN, &margin); int max_late_rate = 5; jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MAX_LATE_RATE, &max_late_rate); JitterBufferPacket incoming_packet; incoming_packet.data = malloc(200); incoming_packet.len = 160; incoming_packet.timestamp = 1000; incoming_packet.span = 160; incoming_packet.sequence = 42; jitter_buffer_put(jitter, &incoming_packet); JitterBufferPacket output_packet; output_packet.data = malloc(200); output_packet.len = 200; spx_int32_t start_offset; int result = jitter_buffer_get(jitter, &output_packet, 160, &start_offset); if (result == JITTER_BUFFER_OK) { // Playback } else if (result == JITTER_BUFFER_MISSING) { // Concealment } else if (result == JITTER_BUFFER_INSERTION) { // Insert silence } jitter_buffer_tick(jitter); jitter_buffer_destroy(jitter); return 0; } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.