~/bend-docscommunity

src/mp3.bend checks

raw source on the hub · import 0xd2dab0fbc9db2e9ba3da65dae724eb41/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

type Br source · line 11 · raw

Data

bitstream: leftover bytes, the current byte, how many low bits remain

type Acc source · line 15 · raw

Data

a field just read

type Pcm source · line 431 · raw

Data

a decoded frame: hertz, channels, interleaved binary32 words

type Wk source · line 435 · raw

Data

decoder state. phase 0 seeks a header, 1 collects a frame, 2 has failed.

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.b2 source · line 244 · raw

@+hz:U32 -> U32

encoder header at 320 kbit/s, original bit set, no CRC, no pad

def enc.srbits source · line 247 · raw

@+hz:U32 -> U32

def enc.b3 source · line 256 · raw

@+ch:U32 -> U32

def enc.hdr source · line 263 · raw

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

def enc.rate source · line 266 · raw

@+hz:U32 -> Bool

def enc.ch source · line 277 · raw

@+ch:U32 -> Bool

def kind.ok source · line 286 · raw

@+k:U32 -> Bool

def zeros.rest source · line 295 · raw

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

def frames.zero source · line 305 · raw

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

an empty sample list is not silence. Silence is one or more zero samples.

def frames.empty source · line 314 · raw

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

def ls.cat source · line 321 · raw

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

def silence.of source · line 325 · raw

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

one silent 320 kbit/s frame. big_values and part2_3_length are zero.

def silence.ok source · line 328 · raw

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

def mp3.live source · line 336 · raw

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

non-zero PCM goes through the analysis filterbank, MDCT, and Huffman encoder.

def one.tail source · line 341 · raw

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

def one.only source · line 348 · raw

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

def samp.hd source · line 355 · raw

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

def pulse.ch source · line 362 · raw

@one:Bool -> @mono:Bool -> @pcm:Bool -> Bool

def pulse.yes source · line 374 · raw

@one:Bool -> @+ch:U32 -> @+kind:U32 -> Bool

one signed sample is a single Huffman pair, not the filterbank

def pulse.fit source · line 378 · raw

@yes:Bool -> @+w:U32 -> Bool

a magnitude above 15 stays on the filterbank. 0 is not a pair.

def pulse.small source · line 385 · raw

@one:Bool -> @+ch:U32 -> @+kind:U32 -> @+w:U32 -> Bool

def mp3.arm source · line 388 · raw

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

def mp3.nz source · line 397 · raw

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

def mp3.body source · line 406 · raw

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

def mp3.pick source · line 415 · raw

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

def mp3.encode source · line 425 · raw

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

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

def le.pick source · line 441 · raw

@eq:Bool -> @+aa:U32 -> @+bb:U32 -> Bool

def le.u source · line 448 · raw

@+aa:U32 -> @+bb:U32 -> Bool

def frm.fit source · line 451 · raw

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

def frm.take source · line 454 · raw

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

def frm.rest source · line 457 · raw

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

def frm.and source · line 460 · raw

@aa:Bool -> @bb:Bool -> Bool

def frm.yes source · line 468 · raw

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

MPEG-1 Layer III, no CRC, not joint stereo

def wk.make source · line 471 · raw

@+hz:U32 -> @+ch:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> Wk

def buf.add source · line 475 · raw

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

def win.tail source · line 478 · raw

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

def wk.bad source · line 485 · raw

@st:Wk -> Wk

def wk.seek source · line 490 · raw

@st:Wk -> @buf:List<&2, U32> -> Wk

def wk.read source · line 495 · raw

@st:Wk -> @+left:U32 -> @buf:List<&2, U32> -> Wk

def wk.fail source · line 500 · raw

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

def walk.keep source · line 503 · raw

@oo:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Out -> @+hz:U32 -> @+ch:U32 -> @+pcm0:List<&2, U32> -> Wk

def walk.apply source · line 508 · raw

@got:Maybe<&2, 0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Out> -> @+hz:U32 -> @+ch:U32 -> @pcm0:List<&2, U32> -> Wk

def wk.eq source · line 515 · raw

@eqh:Bool -> @eqc:Bool -> Bool

def walk.fr2 source · line 522 · raw

@ok:Bool -> @+buf:List<&2, U32> -> @+hz:U32 -> @+ch:U32 -> @pcm0:List<&2, U32> -> @ov:List<&2, F32> -> @qmf:List<&2, F32> -> @+fresh:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> Wk

def walk.fr1 source · line 532 · raw

@+buf:List<&2, U32> -> @+hz:U32 -> @+ch:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> Wk

def walk.fr0 source · line 535 · raw

@cold:Bool -> @+buf:List<&2, U32> -> @+hz0:U32 -> @+ch0:U32 -> @pcm0:List<&2, U32> -> @ov:List<&2, F32> -> @qmf:List<&2, F32> -> @+fresh:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> Wk

def walk.frame source · line 546 · raw

@+buf:List<&2, U32> -> @+hz:U32 -> @+ch:U32 -> @pcm:List<&2, U32> -> @ov:List<&2, F32> -> @qmf:List<&2, F32> -> @+fresh:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> @+on:U32 -> Wk

def walk.rd2 source · line 552 · raw

@done:Bool -> @+hz:U32 -> @+ch:U32 -> @pcm:List<&2, U32> -> @ov:List<&2, F32> -> @qmf:List<&2, F32> -> @+fresh:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> @+on:U32 -> @+left:U32 -> @buf:List<&2, U32> -> Wk

def walk.arm source · line 562 · raw

@big:Bool -> @st:Wk -> @+buf:List<&2, U32> -> Wk

def walk.good source · line 569 · raw

@ok:Bool -> @st:Wk -> @+buf:List<&2, U32> -> Wk

def walk.hdr source · line 576 · raw

@sync:Bool -> @st:Wk -> @+buf:List<&2, U32> -> Wk

def walk.seen2 source · line 583 · raw

@full:Bool -> @+buf:List<&2, U32> -> @st:Wk -> Wk

def walk.seen source · line 590 · raw

@full:Bool -> @st:Wk -> Wk

def walk.ph source · line 595 · raw

@+phase:U32 -> @+hz:U32 -> @+ch:U32 -> @pcm:List<&2, U32> -> @ov:List<&2, F32> -> @qmf:List<&2, F32> -> @+fresh:U32 -> @lib:0xd2dab0fbc9db2e9ba3da65dae724eb41/src/mp3_dec.Lib -> @+on:U32 -> @+left:U32 -> @+buf:List<&2, U32> -> @+hd:U32 -> Wk

def walk.byte source · line 608 · raw

@st:Wk -> @+hd:U32 -> Wk

def walk.done source · line 613 · raw

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

def walk.fin source · line 620 · raw

@seek:Bool -> @have:Bool -> @+hz:U32 -> @+ch:U32 -> @pcm:List<&2, U32> -> Maybe<&2, Pcm>

def walk.finish source · line 627 · raw

@st:Wk -> Maybe<&2, Pcm>

def walk.go source · line 632 · raw

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

def wk.zero source · line 639 · raw

Wk

def mp3.decode source · line 643 · raw

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

bytes in, PCM out. Layer I, Layer II, joint stereo, a CRC, and a bit reservoir are none.