guidellm.utils.audio
encode_audio(audio, sample_rate=None, file_name=None, encode_sample_rate=16000, max_duration=None, mono=True, audio_format=None, bitrate='64k')
Decode audio (if necessary) and re-encode to specified format.
If audio_format is not provided, the format is detected from the source codec (via torchcodec metadata). When detection is not possible (e.g. raw float tensors), falls back to WAV with a one-time warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio | AudioDecoder | bytes | str | Path | ndarray | Tensor | dict[str, Any] | Audio input in any supported form. | required |
sample_rate | int | None | Sample rate hint for raw decoded audio. | None |
file_name | str | None | Override file name in output metadata. Defaults to a name derived from the resolved format. | None |
encode_sample_rate | int | Target sample rate for the encoded output. | 16000 |
max_duration | float | None | Truncate audio to this duration in seconds. | None |
mono | bool | Convert to mono if True. | True |
audio_format | str | None | Target encoding format. If None, detected from source. | None |
bitrate | str | Bitrate for lossy formats like mp3. | '64k' |
Returns:
| Type | Description |
|---|---|
dict[Literal['type', 'audio', 'format', 'mimetype', 'audio_samples', 'audio_seconds', 'audio_bytes', 'file_name'], str | int | float | bytes | None] | Dict containing encoded audio bytes and metadata. |
Source code in src/guidellm/utils/audio.py
get_file_name(path)
pcm16_append_b64_chunks(audio_item, *, target_sample_rate=16000, chunk_samples=3200)
Decode audio to base64-encoded PCM16 mono chunks for realtime append events.
Matches vLLM input_audio_buffer.append (PCM16 mono at target_sample_rate Hz), split into chunk_samples-frame segments. Equivalent conversion flow to vLLM's realtime microphone client example, but generalized for dataset/file inputs used by GuideLLM benchmarks.