Skip to main content

AsyncAudioDecoder

Struct AsyncAudioDecoder 

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

Async wrapper around AudioDecoder.

open and decode_frame both execute on a spawn_blocking thread so the Tokio executor is never blocked by FFmpeg I/O or decoding work. Multiple concurrent callers share the inner decoder through Arc<Mutex<...>>.

§Examples

use ff_decode::AsyncAudioDecoder;
use futures::StreamExt;

let mut decoder = AsyncAudioDecoder::open("audio.mp3").await?;
while let Some(frame) = decoder.decode_frame().await? {
    println!("audio frame with {} samples", frame.samples());
}

Implementations§

Source§

impl AsyncAudioDecoder

Source

pub fn builder(path: impl AsRef<Path>) -> AsyncAudioDecoderBuilder

Returns a builder for configuring the async audio decoder.

Use this when you need to control the output sample format, sample rate, or channel count. For zero-configuration decoding, prefer AsyncAudioDecoder::open.

§Examples
use ff_decode::AsyncAudioDecoder;
use ff_format::SampleFormat;

let decoder = AsyncAudioDecoder::builder("audio.mp3")
    .output_format(SampleFormat::F32)
    .output_sample_rate(48_000)
    .build()
    .await?;
Source

pub async fn open( path: impl AsRef<Path> + Send + 'static, ) -> Result<Self, DecodeError>

Opens the audio file asynchronously.

File I/O and codec initialisation are performed on a spawn_blocking thread so the async executor is not blocked.

§Errors

Returns DecodeError if the file is missing, contains no audio stream, or uses an unsupported codec.

Source

pub async fn decode_frame(&mut self) -> Result<Option<AudioFrame>, DecodeError>

Decodes the next audio frame.

The blocking FFmpeg call is offloaded to a spawn_blocking thread so the Tokio executor is never blocked.

Returns Ok(None) at end of stream.

§Errors

Returns DecodeError on codec or I/O errors.

Source

pub fn into_stream( self, ) -> impl Stream<Item = Result<AudioFrame, DecodeError>> + Send

Converts this decoder into a Stream of audio frames.

Decoding is offloaded to a spawn_blocking thread on each poll via Self::decode_frame. The stream is Send and can be used with tokio::spawn.

The stream ends when the file is exhausted (Ok(None) from decode_frame). Errors are yielded as Err items; the stream terminates after the first error.

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, 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 = 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.