Skip to main content

Module encoder

Module encoder 

Source
Expand description

MPEG-1 Layer III encoding: planar f32 PCM to an .mp3 file at 32, 44.1 or 48 kHz, mono or stereo, at a constant bitrate.

The chain, per granule (576 samples) and channel:

  1. The 32-band polyphase analysis filterbank (ISO 11172-3 annex C), whose prototype is the decoder’s synthesis window scaled by 1/32.
  2. An 18-point MDCT per subband with the sine window, after the sign flip that undoes the filterbank’s frequency inversion, and the alias-reduction butterflies between neighbouring subbands.
  3. A low-pass that follows the bitrate.
  4. For stereo, a per-frame choice of left/right or mid/side.
  5. Power-law quantization (|x|^(3/4), rounding offset 0.4054) with one global step per granule and channel, found by bisection so that the granule fits its share of the frame.
  6. Huffman coding with the best of the 32 big-value tables for each of three regions (the region split is searched too), and the better of the two count-1 tables.

What it leaves out: block switching (short blocks, so sharp attacks pre-echo), scalefactors (the quantization noise is flat across the spectrum), the bit reservoir (every frame is self-contained), intensity stereo, a psychoacoustic model, and MPEG-2 sample rates. The output decodes with any MP3 decoder. encode_mp3 puts a Xing/LAME Info frame first, which carries the encoder delay and padding, so gapless players trim the file to exactly the input’s length.

use threers_codec::mp3::encoder::{encode_mp3, Mp3Config};
let tone: Vec<f32> = (0..44_100).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
let mp3 = encode_mp3(&[tone], &Mp3Config::new(44_100, 1, 64)).unwrap();
assert!(mp3.len() > 1_000);

Structs§

Mp3Config
What to encode.
Mp3Encoder
Encodes a stream frame by frame.

Enums§

Mp3Error
Why encoding was refused.

Constants§

BITRATES
MPEG-1 Layer III bitrates, kbit/s, by header index 1 to 14.
DELAY
Samples a decoder’s output is late by, counting its own 529: where the input’s first sample lands when the stream is decoded without the Info frame’s help. The filterbank takes 481 and the transform’s overlap 576.
FRAME
Samples per frame and channel.
SAMPLE_RATES
The MPEG-1 sample rates, in header order.

Functions§

encode_mp3
Encodes whole channels to an .mp3 file, Info frame first.