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. The returned audio_samples value is the number of frames represented at the encoded output sample rate.
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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
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.