pub struct AudioEncoder { /* private fields */ }Expand description
Zero-allocation audio encoder for the streaming hot path.
Both encode_f32 and encode_i16_le
write into pre-allocated internal buffers and return a borrowed &str.
After the first call that establishes capacity, subsequent calls produce
no heap allocations — critical for real-time audio where this runs
every 100–250 ms.
§Performance
For maximum throughput, keep a single AudioEncoder instance alive across
the streaming loop and pair it with Session::send_raw
to avoid the extra allocation in Session::send_audio.
let mut enc = AudioEncoder::new();
// In a streaming loop:
let b64 = enc.encode_i16_le(pcm_bytes);
// Build the message — only the to_owned() here allocates:
let msg = ClientMessage::RealtimeInput(RealtimeInput {
audio: Some(Blob { data: b64.to_owned(), mime_type: INPUT_AUDIO_MIME.into() }),
video: None, text: None, activity_start: None, activity_end: None,
audio_stream_end: None,
});§Example
use gemini_live::audio::AudioEncoder;
let mut enc = AudioEncoder::new();
let samples: Vec<f32> = vec![0.0, 0.5, -0.5, 1.0, -1.0];
let base64 = enc.encode_f32(&samples);
assert!(!base64.is_empty());Implementations§
Source§impl AudioEncoder
impl AudioEncoder
Sourcepub fn encode_f32(&mut self, samples: &[f32]) -> &str
pub fn encode_f32(&mut self, samples: &[f32]) -> &str
Encode f32 samples (range [-1.0, 1.0]) to base64 i16-LE PCM.
Values outside [-1.0, 1.0] are clamped. The returned &str borrows
the encoder’s internal buffer and is valid until the next encode_*
call.
Sourcepub fn encode_i16_le(&mut self, pcm: &[u8]) -> &str
pub fn encode_i16_le(&mut self, pcm: &[u8]) -> &str
Encode raw i16 little-endian PCM bytes to base64 (zero-conversion path).
Use this when the audio source already provides i16-LE samples.