Skip to main content

AudioResampler

Trait AudioResampler 

Source
pub trait AudioResampler {
    type Adapter: AudioAdapter;
    type Buffer: AsRef<[u8]>;
    type Error;

    // Required methods
    fn send_frame(
        &mut self,
        frame: &AdapterAudioFrame<Self::Adapter, Self::Buffer>,
    ) -> Result<Sent, Self::Error>;
    fn receive_frame(
        &mut self,
        dst: &mut AdapterAudioFrame<Self::Adapter, Self::Buffer>,
    ) -> Result<Received, Self::Error>;
    fn send_eof(&mut self) -> Result<Sent, Self::Error>;
    fn flush(&mut self) -> Result<(), Self::Error>;
}
Expand description

Push-style audio resampler: rate, sample format, and channel layout conversion.

§Both specs are explicit at construction

An implementation is built knowing both ends: the source spec — the rate, sample format and channel layout the frames coming in will carry, which the caller reads off the track’s TrackInfo — and the target spec, which is the caller’s own. The target is never a constant baked into the seam: one consumer wants 16 kHz mono and the next wants 48 kHz stereo, from the same file, at the same time. It is options.

Construction is therefore not on this trait, for the same reason it is not on the decoder traits: each backend takes its own spelling of those two specs.

§A mid-stream format change is a named refusal

If a frame arrives whose rate, sample format or channel layout is not the source spec the implementation was built with, send_frame fails with a named error. It does not silently reconfigure. Silent reconfiguration would resample the two halves of a stream on different terms and hand the caller a single unbroken timeline built out of them, with nothing in the output saying so. Refusing hands the decision back up: the caller rebuilds the resampler for the new source spec, and the seam in the data has a seam in the code to match.

§EOF drains the conversion tail

A rate converter holds samples back: its filter needs future input to produce present output, so at any moment some tens of milliseconds are inside it and not yet out. send_eof followed by receive_frame drains them. Skipping the drain loses the end of every file.

The tail has an end and receive_frame names it: Received::Ended once the delay line is empty, never Received::NeedsInput. The distinction is the whole reason a drain loop can be written without the caller remembering whether it called send_eof — one answer means feed me, the other means stop, and a resampler that says the first when it means the second is an infinite loop nothing in the caller can detect.

Output timestamps are kept by delay-compensated accounting: the output timeline is anchored on the first input timestamp and then advanced by the number of samples actually produced, so the frames drained after EOF continue the same timeline rather than restarting or repeating it.

Required Associated Types§

Source

type Adapter: AudioAdapter

Backend vocabulary. The source and target specs are both expressed in it.

Source

type Buffer: AsRef<[u8]>

Buffer type held by the frames this resampler accepts and produces.

Source

type Error

Resampler-specific error type — faults only. The rhythm’s own states leave through Sent and Received; what travels here is the mid-stream-change refusal out of send_frame and whatever else genuinely went wrong.

Required Methods§

Source

fn send_frame( &mut self, frame: &AdapterAudioFrame<Self::Adapter, Self::Buffer>, ) -> Result<Sent, Self::Error>

Submits one decoded frame.

Fails with a named error if the frame’s rate, sample format or channel layout is not the source spec this resampler was built with — that is a fault, not back pressure: the same frame will be refused however much is drained first, so it travels in Err.

Sent::MustDrain is reserved for an implementation whose converted-frame queue is bounded. The swresample backend’s is not, so it answers Sent::Accepted whenever it accepts at all; the arm is on the face because a bounded implementation is the obvious next one and a caller written against the face must already handle it.

Source

fn receive_frame( &mut self, dst: &mut AdapterAudioFrame<Self::Adapter, Self::Buffer>, ) -> Result<Received, Self::Error>

Drains one converted frame into dst, answering the same Received that AudioStreamDecoder::receive_frame answers.

Source

fn send_eof(&mut self) -> Result<Sent, Self::Error>

Signals end of stream. The conversion tail becomes drainable through receive_frame.

Source

fn flush(&mut self) -> Result<(), Self::Error>

Flushes internal state — buffered input, the undrained tail, and the output timestamp anchor — leaving the resampler ready for a fresh stream on the same two specs.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§