src/mp3.bend checks
raw source on the hub · import emerging-ezaudio@0.6.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 Bit source · line 7 · raw
Data
one pulled bit, or a field, beside the reader
Bit@xs:List<&2, U32> -> @buf:U32 -> @n:U32 -> @v:U32 -> Bit
type Br source · line 11 · raw
Data
bitstream: leftover bytes, the current byte, how many low bits remain
Br@xs:List<&2, U32> -> @buf:U32 -> @n:U32 -> Br
type Acc source · line 15 · raw
Data
a field just read
Acc@br:Br -> @v:U32 -> Acc
type Pcm source · line 397 · raw
Data
a decoded stream: hertz, channels, interleaved binary32 words
Pcm@hz:U32 -> @ch:U32 -> @pcm:List<&2, U32> -> Pcm
type Cut source · line 401 · raw
Data
the first n bytes, the rest, and whether the bytes ran out first
Cut@got:List<&2, U32> -> @rest:List<&2, U32> -> @short:Bool -> Cut
type Fq source · line 429 · 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
FqEnd@acc:List<&2, List<&2, U32>> -> Fq
FqGo@xs:List<&2, U32> -> @first:Bool -> @acc:List<&2, List<&2, U32>> -> Fq
Definitions
def hdr.layer source · line 19 · raw
@xs:List<&2, U32> -> U32
'MPEG-1 Layer III' is layer code 1 in the header
def hdr.mpeg1 source · line 27 · raw
@xs:List<&2, U32> -> U32
1 when the ID bit selects MPEG-1
def hdr.index source · line 35 · raw
@xs:List<&2, U32> -> U32
bitrate index, 0 and 15 are not a frame
def hdr.sr source · line 43 · raw
@xs:List<&2, U32> -> U32
sampling-frequency index. 3 is reserved.
def hdr.mode source · line 51 · raw
@xs:List<&2, U32> -> U32
channel mode. 3 is mono.
def hdr.pad source · line 59 · raw
@xs:List<&2, U32> -> U32
padding bit
def hdr.crc source · line 67 · raw
@xs:List<&2, U32> -> U32
protection bit 0 means a 16-bit CRC follows the header
def hdr.sync source · line 75 · raw
@xs:List<&2, U32> -> Bool
1 when the first two bytes carry the 11-bit sync 0xFFE
def kbps.of source · line 82 · raw
@+ix:U32 -> U32
def hdr.kbps source · line 116 · raw
@xs:List<&2, U32> -> U32
ISO/IEC 11172-3 MPEG-1 Layer III bitrate, kilobits per second
def hz.of source · line 119 · raw
@+ix:U32 -> U32
def hdr.hz source · line 131 · raw
@xs:List<&2, U32> -> U32
ISO/IEC 11172-3 sampling frequency, hertz
def ch.of source · line 135 · raw
@+mode:U32 -> U32
1 or 2 channels. mode 3 is single channel.
def hdr.ch source · line 142 · raw
@xs:List<&2, U32> -> U32
def l3.and source · line 145 · raw
@a:Bool -> @b:Bool -> Bool
def l3.ok source · line 152 · raw
@+xs:List<&2, U32> -> Bool
def frame.n source · line 158 · 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 161 · raw
@+xs:List<&2, U32> -> U32
def br.zero source · line 164 · raw
@xs:List<&2, U32> -> Br
def br.fill source · line 167 · raw
@xs:List<&2, U32> -> Br
def br.norm source · line 174 · raw
@br:Br -> Br
def bit.at source · line 181 · raw
@z:Bool -> @xs:List<&2, U32> -> @+buf:U32 -> @+n:U32 -> Bit
def br.one.at source · line 188 · raw
@br:Br -> Bit
def br.one source · line 193 · raw
@br:Br -> Bit
def br.shift source · line 196 · raw
@b:Bit -> @+acc:U32 -> Acc
def br.next source · line 201 · raw
@ac:Acc -> Acc
def br.get source · line 206 · raw
@hop:Nat -> @ac:Acc -> Acc
def br.take source · line 213 · raw
@br:Br -> @+k:U32 -> Acc
def acc.br source · line 216 · raw
@ac:Acc -> Br
def acc.v source · line 221 · raw
@ac:Acc -> U32
def side.n source · line 227 · raw
@+ch:U32 -> U32
side-info length in bytes. MPEG-1 mono is 17, stereo is 32.
def sr.row source · line 234 · raw
@+hz:U32 -> U32
def enc.srbits source · line 243 · raw
@+hz:U32 -> U32
def enc.b3 source · line 252 · raw
@+ch:U32 -> U32
def enc.hdr source · line 260 · 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 264 · raw
@+hz:U32 -> Bool
the encoder's rates are 32000, 44100 and 48000
def enc.ch source · line 268 · raw
@+ch:U32 -> Bool
mono or stereo
def enc.fit source · line 271 · raw
@+hz:U32 -> @+ch:U32 -> Bool
def ls.cat source · line 274 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
def ls.fix source · line 278 · raw
@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>
exactly hop bytes: the list, then zeros
def enc.rem source · line 290 · 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 294 · 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 297 · raw
@pad:Bool -> @+hz:U32 -> @+slot:U32 -> U32
def enc.pads source · line 305 · 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 312 · raw
@pad:Bool -> U32
def enc.frame source · line 320 · raw
@+hz:U32 -> @+ch:U32 -> @+pad:U32 -> @gs:List<&2, 0xd7eee36bef55d6453644b418e1a0daff/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 324 · raw
@pads:List<&2, Bool> -> @+gs:List<&2, 0xd7eee36bef55d6453644b418e1a0daff/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 334 · 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 337 · raw
@+ch:U32 -> U32
def bud.cap source · line 344 · raw
@fit:Bool -> @+nn:U32 -> U32
def enc.room source · line 351 · raw
@+hz:U32 -> @+ch:U32 -> U32
def enc.bud source · line 356 · 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 359 · raw
@+nf:U32 -> @+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @+pcm:List<&2, U32> -> List<&2, U32>
def enc.bytes source · line 363 · 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 366 · raw
@+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @pcm:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def mp3.pick source · line 373 · raw
@ok:Bool -> @+hz:U32 -> @+ch:U32 -> @+kind:U32 -> @pcm:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def mp3.encode source · line 387 · 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 404 · raw
@hop:Nat -> @xs:List<&2, U32> -> @acc:List<&2, U32> -> Cut
def xing.tag source · line 414 · raw
@xs:List<&2, U32> -> Bool
"Xing" or "Info", a LAME/Xing header frame
def xing.is source · line 424 · 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 433 · raw
@info:Bool -> @got:List<&2, U32> -> @rest:List<&2, U32> -> @acc:List<&2, List<&2, U32>> -> Fq
def fq.first source · line 441 · 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 449 · 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 459 · 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 466 · raw
@ok:Bool -> @+xs:List<&2, U32> -> @first:Bool -> @acc:List<&2, List<&2, U32>> -> Fq
def fq.done source · line 473 · raw
@st:Fq -> List<&2, List<&2, U32>>
def fq.walk source · line 481 · 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 493 · raw
@+s0:U32 -> @+s1:U32 -> @+s2:U32 -> @+s3:U32 -> U32
ID3v2 size: four syncsafe bytes, 7 bits each
def id3.ok source · line 496 · raw
@+v0:U32 -> @+s0:U32 -> @+s1:U32 -> @+s2:U32 -> @+s3:U32 -> Bool
def id3.foot source · line 500 · raw
@on:Bool -> U32
flag bit 4: a 10-byte footer follows the tag
def id3.cut source · line 507 · raw
@ok:Bool -> @+size:U32 -> @rest:List<&2, U32> -> @whole:List<&2, U32> -> List<&2, U32>
def id3.at source · line 514 · raw
@xs:List<&2, U32> -> @whole:List<&2, U32> -> List<&2, U32>
def id3.skip source · line 523 · 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 526 · raw
@+xs:List<&2, U32> -> List<&2, List<&2, U32>>
def mp3.frames source · line 533 · 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 537 · 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 544 · raw
@+mode:U32 -> @+ext:U32 -> Bool
def play.ok source · line 552 · 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 555 · raw
@+fr:List<&2, U32> -> @+st:0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St -> Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St>
def play.step source · line 560 · raw
@+fr:List<&2, U32> -> @got:Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St> -> Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St>
def play.go source · line 567 · raw
@fs:List<&2, List<&2, U32>> -> @got:Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St> -> Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St>
def play.end source · line 574 · raw
@got:Maybe<&2, 0xd7eee36bef55d6453644b418e1a0daff/src/mp3_dec.St> -> Maybe<&2, Pcm>
def mp3.play source · line 584 · 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 593 · raw
@+fr:List<&2, U32> -> Maybe<&2, List<&2, 0xd7eee36bef55d6453644b418e1a0daff/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 599 · 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.