nemo_voice_agent.utils.audio
nemo_voice_agent.utils.audio
Module Contents
Classes
Functions
Data
API
A class that simulates a realtime audio stream. It caches the input audio chunks and resamples them to the output sample rate. Each time its get() function is called, it returns the next chunk of audio at the output sample rate. If the audio cache doesn’t have enough audio to fill the output chunk, it will append silence to the output chunk.
The class will be used in an asyncio context, where one thread is putting audio chunks into the cache and another thread is getting audio chunks from the cache.
Get the current size of the buffer.
Augment audio with noise based on random SNR sampling.
This method mixes audio with noise according to a gain_db.
Parameters:
Original audio bytes (16-bit signed integers)
Noise audio bytes (16-bit signed integers)
Returns: bytes
Mixed audio with noise as bytes
Check if the buffer is full.
Simulate audio device timing by sleeping between audio chunks.
Get the next output chunk of audio, immediately padding with silence if no audio is available.
Pad audio chunk with silence/noise if shorter than expected output chunk size.
If noise_chunk is provided and noise_generator is available, the audio will be augmented with noise based on SNR. Otherwise, it pads with silence (zeros).
Parameters:
Audio bytes (16-bit signed integers)
Optional noise bytes for augmentation
Returns: bytes
Audio chunk padded to output_chunk_bytes
Get the next output chunk of audio, WAITING for audio to be available.
Unlike get(), this method will block and wait for audio to arrive rather than immediately padding with silence. This prevents gaps in audio when packets arrive in bursts (common in WebSocket/network scenarios).
Use this for continuous audio streaming where you want smooth audio without artificial gaps.
Returns: Tuple[audio_chunk, has_speech]: Tuple containing the audio chunk bytes and a boolean indicating if there’s speech in the chunk
Parameters:
Maximum time to wait in seconds (None = no wait)
If True, only tries to read the audio cache once, and returns silence immediately if no audio is available.
Put an audio chunk into the audio cache after resampling.
Parameters:
Input audio chunk at input_sample_rate
Resample an audio chunk from input sample rate to output sample rate.
Parameters:
Raw audio bytes (16-bit signed integers)
Returns: bytes
Resampled audio bytes (16-bit signed integers)
A class that configures the noise for the audio stream.
Convert the noise configuration to a dictionary.
A class that generates noise audio by reading provided noise audio files.
Generate random white noise with the given duration and sample rate.
Returns: np.ndarray
np.ndarray: Float32 white noise in [-1.0, 1.0], length max_duration * sample_rate. Scaled by white_noise_db when set (amplitude = 10^(white_noise_db/20)).
Get the next noise audio segment of chunk size chunk_size_in_seconds, and return the chunk. If the noise audio data is less than the chunk size, restart from the beginning.
Parameters:
Duration of the noise chunk to return in seconds.
Returns: np.ndarray
np.ndarray: Noise audio chunk of the requested duration.
Get the next noise audio segment of chunk size chunk_size_in_seconds, and return the chunk as Int16 bytes.
Load the noise audio files.
An audio resampler that uses the SoX resampler library. It’s stateless and will return the result immediately.
Resample audio data using SoX resampler library.
Parameters:
Input audio data as raw bytes (16-bit signed integers).
Returns: bytes
Resampled audio data as raw bytes (16-bit signed integers).
A class that resamples an audio stream using the SoX resampler library.
Check if the resampler should be flushed.
Resample an audio chunk using the SoX resampler library. Args: audio: The audio chunk to resample. Returns: The resampled audio chunk.
Reset the resampler.
Convert PCM-16 audio bytes to float32 numpy array, clamped to -1.0 to 1.0.
Convert float32 numpy array to PCM-16 audio bytes.