~/bend-docscommunity

src/mp3.bend checks

raw source on the hub · import emerging-ezaudio@0.7.0.0/src/mp3.bend as Mp3

MPEG-1 Layer III (ISO/IEC 11172-3). Mono and stereo. Layer I and II are rejected.

3 imports
import Base
import ./mp3_enc.bend as Enc
import ./mp3_dec.bend as Dec

Types

type Pcm source · line 322 · raw

Data

a decoded stream: hertz, channels, interleaved binary32 words

type Cut source · line 326 · raw

Data

the first n bytes, the rest, and whether the bytes ran out first

type Fq source · line 354 · raw

Data

the frame walk: done with the frames found, newest first, or the bytes left, whether no frame has been kept yet, and the frames so far

Definitions

def hdr.layer source · line 7 · raw

@xs:List<&2, U32> -> U32

'MPEG-1 Layer III' is layer code 1 in the header

def hdr.mpeg1 source · line 15 · raw

@xs:List<&2, U32> -> U32

1 when the ID bit selects MPEG-1

def hdr.index source · line 23 · raw

@xs:List<&2, U32> -> U32

bitrate index, 0 and 15 are not a frame

def hdr.sr source · line 31 · raw

@xs:List<&2, U32> -> U32

sampling-frequency index. 3 is reserved.

def hdr.mode source · line 39 · raw

@xs:List<&2, U32> -> U32

channel mode. 3 is mono.

def hdr.pad source · line 47 · raw

@xs:List<&2, U32> -> U32

padding bit

def hdr.crc source · line 55 · raw

@xs:List<&2, U32> -> U32

protection bit 0 means a 16-bit CRC follows the header

def hdr.sync source · line 63 · raw

@xs:List<&2, U32> -> Bool

1 when the first two bytes carry the 11-bit sync 0xFFE

def kbps.of source · line 70 · raw

@+ix:U32 -> U32

def hdr.kbps source · line 104 · raw

@xs:List<&2, U32> -> U32

ISO/IEC 11172-3 MPEG-1 Layer III bitrate, kilobits per second

def hz.of source · line 107 · raw

@+ix:U32 -> U32

def hdr.hz source · line 119 · raw

@xs:List<&2, U32> -> U32

ISO/IEC 11172-3 sampling frequency, hertz

def ch.of source · line 123 · raw

@+mode:U32 -> U32

1 or 2 channels. mode 3 is single channel.

def hdr.ch source · line 130 · raw

@xs:List<&2, U32> -> U32

def l3.and source · line 133 · raw

@a:Bool -> @b:Bool -> Bool

def l3.ok source · line 140 · raw

@+xs:List<&2, U32> -> Bool

def frame.n source · line 146 · raw

@+kb:U32 -> @+hz:U32 -> @+pad:U32 -> U32

frame length in bytes: 1152 * kbps * 125 / hz, plus the pad bit

def frame.of source · line 149 · raw

@+xs:List<&2, U32> -> U32

def side.n source · line 153 · raw

@+ch:U32 -> U32

side-info length in bytes. MPEG-1 mono is 17, stereo is 32.

def sr.row source · line 160 · raw

@+hz:U32 -> U32

def enc.srbits source · line 169 · raw

@+hz:U32 -> U32

def enc.b3 source · line 178 · raw

@+ch:U32 -> U32

def enc.hdr source · line 186 · raw

@+hz:U32 -> @+ch:U32 -> @+pad:U32 -> List<&2, U32>

encoder header: MPEG-1 Layer III at 320 kbit/s, no CRC, original bit set, pad 0 or 1

def enc.rate source · line 190 · raw

@+hz:U32 -> Bool

the encoder's rates are 32000, 44100 and 48000

def enc.ch source · line 194 · raw

@+ch:U32 -> Bool

mono or stereo

def enc.fit source · line 197 · raw

@+hz:U32 -> @+ch:U32 -> Bool

def ls.cat source · line 200 · raw

@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>

def ls.fix source · line 204 · raw

@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>

exactly hop bytes: the list, then zeros

def enc.rem source · line 216 · raw

@+hz:U32 -> U32

a frame is 1152 * 320000 / 8 / hz bytes; this is the remainder of that division, times hz

def enc.padb source · line 220 · raw

@+hz:U32 -> @+slot:U32 -> Bool

a frame pads once the remainders since the last padded frame make a whole byte

def enc.next source · line 223 · raw

@pad:Bool -> @+hz:U32 -> @+slot:U32 -> U32

def enc.pads source · line 231 · raw

@hop:Nat -> @+hz:U32 -> @+slot:U32 -> List<&2, Bool>

the pad bit of each of hop frames, starting from a slot remainder

def enc.pad source · line 238 · raw

@pad:Bool -> U32

def enc.frame source · line 246 · raw

@+hz:U32 -> @+ch:U32 -> @+pad:U32 -> @gs:List<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_enc.Gr> -> List<&2, U32>

a frame: the header, side information and main data, as many bytes as the header says

def fr.go source · line 250 · raw

@pads:List<&2, Bool> -> @+gs:List<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_enc.Gr> -> @+hz:U32 -> @+ch:U32 -> List<&2, U32>

one frame per pad bit, each taking two granules per channel

def enc.nf source · line 260 · raw

@+ch:U32 -> @+pcm:List<&2, U32> -> U32

frames for samples / channels per-channel samples: whole frames, a granule of MDCT delay and the filterbank's, so 1152 * frames is at least samples + 1056

def side.bits source · line 263 · raw

@+ch:U32 -> U32

def bud.cap source · line 270 · raw

@fit:Bool -> @+nn:U32 -> U32

def enc.room source · line 277 · raw

@+hz:U32 -> @+ch:U32 -> U32

def enc.bud source · line 282 · raw

@+hz:U32 -> @+ch:U32 -> U32

bits per granule and channel: an unpadded frame less header and side information, shared by the granules, less 8; at most 4095, the largest 12-bit part2_3_length

def enc.out source · line 285 · raw

@+nf:U32 -> @+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @+pcm:List<&2, U32> -> List<&2, U32>

def enc.bytes source · line 289 · raw

@+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @+pcm:List<&2, U32> -> List<&2, U32>

every input sample is in a frame: nf frames, the first padded from slot 0

def mp3.body source · line 292 · raw

@+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @pcm:List<&2, U32> -> Maybe<&2, List<&2, U32>>

def mp3.pick source · line 299 · raw

@ok:Bool -> @+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @pcm:List<&2, U32> -> Maybe<&2, List<&2, U32>>

def mp3.encode source · line 313 · raw

@+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @pcm:List<&2, U32> -> Maybe<&2, List<&2, U32>>

samples are interleaved U32 bits. kind 1 is s16, any other binary32.

def cut.go source · line 329 · raw

@hop:Nat -> @xs:List<&2, U32> -> @acc:List<&2, U32> -> Cut

def xing.tag source · line 339 · raw

@xs:List<&2, U32> -> Bool

"Xing" or "Info", a LAME/Xing header frame

def xing.is source · line 349 · raw

@+fr:List<&2, U32> -> Bool

the tag sits where the main data would start, after the header, CRC and side info

def fq.keep source · line 358 · raw

@info:Bool -> @got:List<&2, U32> -> @rest:List<&2, U32> -> @acc:List<&2, List<&2, U32>> -> Fq

def fq.first source · line 366 · raw

@first:Bool -> @+got:List<&2, U32> -> @rest:List<&2, U32> -> @acc:List<&2, List<&2, U32>> -> Fq

a Xing or Info frame is skipped when it comes first

def fq.cut source · line 374 · raw

@cc:Cut -> @first:Bool -> @acc:List<&2, List<&2, U32>> -> Fq

a frame the bytes end inside is dropped, as minimp3 and dr_mp3 drop it

def fq.tail source · line 384 · raw

@xs:List<&2, U32> -> @first:Bool -> @acc:List<&2, List<&2, U32>> -> Fq

a byte that does not start an MPEG-1 Layer III header is junk: step over it

def fq.at source · line 391 · raw

@ok:Bool -> @+xs:List<&2, U32> -> @first:Bool -> @acc:List<&2, List<&2, U32>> -> Fq

def fq.done source · line 398 · raw

@st:Fq -> List<&2, List<&2, U32>>

def fq.walk source · line 406 · raw

@fuel:Nat -> @st:Fq -> List<&2, List<&2, U32>>

every step takes at least one byte, so one more step than there are bytes finishes

def id3.size source · line 418 · raw

@+s0:U32 -> @+s1:U32 -> @+s2:U32 -> @+s3:U32 -> U32

ID3v2 size: four syncsafe bytes, 7 bits each

def id3.ok source · line 421 · raw

@+v0:U32 -> @+s0:U32 -> @+s1:U32 -> @+s2:U32 -> @+s3:U32 -> Bool

def id3.foot source · line 425 · raw

@on:Bool -> U32

flag bit 4: a 10-byte footer follows the tag

def id3.cut source · line 432 · raw

@ok:Bool -> @+size:U32 -> @rest:List<&2, U32> -> @whole:List<&2, U32> -> List<&2, U32>

def id3.at source · line 439 · raw

@xs:List<&2, U32> -> @whole:List<&2, U32> -> List<&2, U32>

def id3.skip source · line 448 · raw

@+xs:List<&2, U32> -> List<&2, U32>

a leading ID3v2 tag ("ID3", version, flags, syncsafe size) is skipped by its size

def fq.start source · line 451 · raw

@+xs:List<&2, U32> -> List<&2, List<&2, U32>>

def mp3.frames source · line 458 · raw

@xs:List<&2, U32> -> List<&2, List<&2, U32>>

the whole frames of a stream, each starting with its header: a leading ID3v2 tag is skipped by its size, bytes that are not an MPEG-1 Layer III header are stepped over, a Xing or Info frame is skipped when it is the first frame, and a frame the bytes end inside is dropped

def hdr.ext source · line 462 · raw

@xs:List<&2, U32> -> U32

mode extension: in joint stereo, bit 1 is mid/side and bit 0 is intensity stereo

def mode.ok source · line 469 · raw

@+mode:U32 -> @+ext:U32 -> Bool

def play.ok source · line 477 · raw

@+fr:List<&2, U32> -> @+hz:U32 -> @+ch:U32 -> Bool

the stream's rate and channels hold, and joint stereo uses neither mid/side nor intensity

def play.frame source · line 480 · raw

@+fr:List<&2, U32> -> @+st:0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St -> Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St>

def play.step source · line 485 · raw

@+fr:List<&2, U32> -> @got:Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St> -> Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St>

def play.go source · line 492 · raw

@fs:List<&2, List<&2, U32>> -> @got:Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St> -> Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St>

def play.end source · line 499 · raw

@got:Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.St> -> Maybe<&2, Pcm>

def mp3.play source · line 509 · raw

@fs:List<&2, List<&2, U32>> -> Maybe<&2, Pcm>

decode frames in order. The first frame sets the rate and channels; no frames is none.

def mp3.lines source · line 518 · raw

@+fr:List<&2, U32> -> Maybe<&2, List<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3_dec.Gl>>

the quantized lines and global_gain of each granule channel of one frame, in bitstream order, before requantization

def mp3.decode source · line 524 · raw

@xs:List<&2, U32> -> Maybe<&2, Pcm>

bytes in, PCM out: the frames mp3.frames finds, decoded. none when a frame holds what the decoder does not read (see Dec.dec.gls), uses mid/side or intensity stereo, or changes the rate or channels, and when there is no frame at all.