Skip to main content

Mp3Encoder

Struct Mp3Encoder 

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

Stream-level MP3 encoder: accumulates per-channel PCM, emits one MP3 frame per 1152 samples per channel (576 for MPEG-2/2.5). MPEG-1/2/2.5, CBR or VBR, psychoacoustic noise shaping; mono, stereo, or per-frame mid/side joint stereo. Configured via Mp3EncoderConfig.

Push PCM with push_pcm_f32 / push_pcm_s16 (the first push fixes the header from the sample rate + channel count), drain with next_packet, and call finish at end of input (tail padding, reservoir assembly, Xing/Info header).

Implementations§

Source§

impl Mp3Encoder

Source

pub fn new(config: Mp3EncoderConfig) -> Self

Source

pub fn push_pcm_f32( &mut self, interleaved: &[f32], channels: u16, sample_rate: u32, ) -> Result<()>

Push interleaved f32 PCM in [-1, 1]. The first push fixes the frame header (MPEG version from sample_rate, mono/stereo from channels); if a later push carries fewer channels than the header, the last input channel is replicated. interleaved.len() should be a multiple of channels (a trailing partial sample is ignored).

§Errors

Error::Unsupported when the FIRST push carries a sample rate outside the nine MPEG Layer III rates (it fixes the stream header).

Source

pub fn push_pcm_s16( &mut self, interleaved: &[i16], channels: u16, sample_rate: u32, ) -> Result<()>

Push interleaved signed-16-bit PCM. Converts i16 / 32768.0 — the same convention the decoder uses — then follows push_pcm_f32.

§Errors

Error::Unsupported when the FIRST push carries a sample rate outside the nine MPEG Layer III rates (it fixes the stream header).

Source

pub fn push_pcm_s16le( &mut self, bytes: &[u8], channels: u16, sample_rate: u32, ) -> Result<()>

Push interleaved little-endian s16 bytes — the layout a WAV data chunk or an FFmpeg-style AudioFrame plane already has. Same samples as push_pcm_s16 on the decoded values, without the caller materialising an i16 (and then an f32) copy of the input first. A trailing partial sample is ignored.

§Errors

Error::Unsupported when the FIRST push carries a sample rate outside the nine MPEG Layer III rates (it fixes the stream header).

Source

pub fn push_pcm_f32le( &mut self, bytes: &[u8], channels: u16, sample_rate: u32, ) -> Result<()>

Push interleaved little-endian f32 bytes; the byte-level twin of push_pcm_f32, as push_pcm_s16le is of push_pcm_s16.

§Errors

Error::Unsupported when the FIRST push carries a sample rate outside the nine MPEG Layer III rates (it fixes the stream header).

Source

pub fn next_packet(&mut self) -> Result<Vec<u8>>

Pull the next encoded MP3 packet (one frame, or the prepended Xing/Info frame after finish). Err(Again) = feed more PCM; Err(Eof) = finished and fully drained.

§Errors

Error::Again when more PCM is needed; Error::Eof once finish was called and every packet has been drained.

Source

pub fn finish(&mut self)

End of input: pad the PCM tail to a whole frame, assemble the bit reservoir stream (CBR V1), and prepend the Xing/Info header. After this, next_packet drains to Err(Eof).

Trait Implementations§

Source§

impl Default for Mp3Encoder

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