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
impl MtmdResampler
Sourcepub fn new(rate_in: i32, rate_out: i32) -> Result<Self>
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.
Sourcepub fn process<'a>(&'a mut self, input: &'a [f32]) -> Result<&'a [f32]>
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.