Skip to main content

AudioEncoder

Struct AudioEncoder 

Source
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

Source

pub fn new() -> Self

Create a new encoder pre-allocated for ~250 ms at 16 kHz.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for AudioEncoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more