Skip to main content

MtmdResampler

Struct MtmdResampler 

Source
pub struct MtmdResampler { /* private fields */ }
Expand description

Streaming sample-rate converter for mono f32 PCM.

Audio models want their own rate — MtmdContext::audio_sample_rate says which — and a microphone or file rarely matches it. This converts between the two a block at a time, keeping the filter state across calls, so a realtime stream can be fed in as it arrives rather than buffered whole.

One instance per stream: the state is what makes consecutive blocks join without a seam, so sharing one across two streams would interleave them.

Wraps mtmd_helper_resampler_*, new in b12388.

§Examples

let mut resampler = MtmdResampler::new(48_000, mtmd.audio_sample_rate())?;
let mut pcm = Vec::new();
for chunk in mic_chunks {
    pcm.extend_from_slice(resampler.process(&chunk)?);
}

Implementations§

Source§

impl MtmdResampler

Source

pub fn new(rate_in: i32, rate_out: i32) -> Result<Self>

Open a resampler converting rate_in Hz to rate_out Hz.

Equal rates are allowed and pass samples through.

§Errors

Returns MtmdError::ResamplerInitFailed when llama.cpp cannot build a converter for that pair — a non-positive rate, say.

Source

pub fn process<'a>(&'a mut self, input: &'a [f32]) -> Result<&'a [f32]>

Convert one block, returning the samples produced so far.

The result is often shorter or longer than input and may be empty: the converter holds back whatever it needs to join the next block cleanly. Feeding an empty slice is allowed and simply drains nothing.

The returned slice borrows the resampler’s own buffer, which the next call overwrites. &mut self here and &[f32] out is what makes the borrow checker enforce that rather than leaving it to a doc comment — copy out (as the example on this type does) to keep the samples.

input is borrowed for the same region as the result, which is not over-tight: when the two rates are equal upstream passes the input buffer straight back out, so the result can alias input rather than the resampler’s buffer. Tying both to one lifetime is what keeps that case from handing back a dangling slice.

§Errors

Returns MtmdError::ResampleError if llama.cpp reports a negative code.

Trait Implementations§

Source§

impl Debug for MtmdResampler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for MtmdResampler

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for MtmdResampler

Source§

impl Sync for MtmdResampler

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<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