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
impl Mp3Encoder
pub fn new(config: Mp3EncoderConfig) -> Self
Sourcepub fn push_pcm_f32(
&mut self,
interleaved: &[f32],
channels: u16,
sample_rate: u32,
) -> Result<()>
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).
Sourcepub fn push_pcm_s16(
&mut self,
interleaved: &[i16],
channels: u16,
sample_rate: u32,
) -> Result<()>
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).
Sourcepub fn push_pcm_s16le(
&mut self,
bytes: &[u8],
channels: u16,
sample_rate: u32,
) -> Result<()>
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).
Sourcepub fn push_pcm_f32le(
&mut self,
bytes: &[u8],
channels: u16,
sample_rate: u32,
) -> Result<()>
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).
Sourcepub fn next_packet(&mut self) -> Result<Vec<u8>>
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.
Sourcepub fn finish(&mut self)
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).