# ezaudio: audio for Bend 2, reading and writing WAV (RIFF PCM) and MP3 (MPEG-1 Layer III). # # A Clip is a sample rate, a channel count, a sample format, and # interleaved PCM frames. WAV reads and writes RIFF PCM. MP3 reads and writes # MPEG-1 Layer III. import Base import ./src/wav.bend as Wav import ./src/mp3.bend as Mp3 # signed 16-bit PCM, or IEEE 754 binary32 type Format is Data: S16{} Flt{} # one clip. pcm is interleaved. s16 lives in the low 16 bits. type Clip is Data: Clip{hz: U32, nch: U32, fmt: Format, pcm: List<&2, U32>} # signed 16-bit little-endian PCM def s16() -> Format: S16{} # IEEE 754 binary32, stored as its bits def f32() -> Format: Flt{} def kind.of(fmt: Format) -> U32: match fmt: case S16{}: 1 case Flt{}: 3 def fmt.of(+kind: U32) -> Format: match kind: case 1: S16{} case _: Flt{} def wav.clip(w: Wav.Wav) -> Clip: match w: case Wav.Wav{+rate, +channels, +kind, frames}: Clip{rate, channels, fmt.of(kind), frames} def fmt.same(aa: Format, bb: Format) -> Bool: match aa bb: case S16{} S16{}: True{} case Flt{} Flt{}: True{} case S16{} Flt{}: False{} case Flt{} S16{}: False{} # a clip from its rate, channel count, format, and interleaved frames def clip(+rate: U32, +channels: U32, format: Format, frames: List<&2, U32>) -> Clip: Clip{rate, channels, format, frames} # the sample rate, in hertz def rate(c: Clip) -> U32: match c: case Clip{+hz, _nch, _fmt, _pcm}: hz # how many channels are interleaved def channels(c: Clip) -> U32: match c: case Clip{_hz, +nch, _fmt, _pcm}: nch # s16 or f32 def format(c: Clip) -> Format: match c: case Clip{_hz, _nch, +fmt, _pcm}: fmt # the interleaved frames def frames(c: Clip) -> List<&2, U32>: match c: case Clip{_hz, _nch, _fmt, +pcm}: pcm def count.at(zero: Bool, +channels: U32, +nsamples: U32) -> U32: match zero: case True{}: 0 case False{}: U32.div(nsamples, channels) def count.of(+channels: U32, +nsamples: U32) -> U32: count.at(U32.is_eq(channels, 0), channels, nsamples) # how many frames. zero channels count as none. def count(c: Clip) -> U32: match c: case Clip{_hz, +nch, _fmt, +pcm}: count.of(nch, U32.from_nat(List.length(&2, U32, pcm))) # one sample, repeated across nframes frames: nframes * channels samples, # counted as natural numbers so the product does not wrap a U32 def fill(+rate: U32, +channels: U32, format: Format, +sample: U32, +nframes: U32) -> Clip: Clip{rate, channels, format, List.replicate(U32, Nat.mul(U32.to_nat(nframes), U32.to_nat(channels)), sample)} def ls.drop(hop: Nat, xs: List<&2, U32>) -> List<&2, U32>: match hop: case 0n: xs case 1n+p: match xs: case Nil{}: [] case _hd <> tl: ls.drop(p, tl) def ls.take(hop: Nat, xs: List<&2, U32>) -> List<&2, U32>: match hop: case 0n: [] case 1n+p: match xs: case Nil{}: [] case +hd <> tl: hd <> ls.take(p, tl) def trim.go( ok: Bool, +rate: U32, +channels: U32, format: Format, frames: List<&2, U32>, +skip: Nat, +keep: Nat ) -> Maybe<&2, Clip>: match ok: case False{}: None{} case True{}: Some{Clip{rate, channels, format, ls.take(keep, ls.drop(skip, frames))}} # the span fits: (start + nframes) * channels samples are there, and a clip # with no channels has only the empty span. Counted as natural numbers. def trim.fits(+chn: Nat, +span: Nat, +nsamples: Nat) -> Bool: Nat.is_le(Nat.mul(span, chn), nsamples) && Bool.or(Nat.is_ne(chn, 0n), Nat.is_eq(span, 0n)) # frames [start, start + nframes). none when that span passes the end. def trim(c: Clip, +start: U32, +nframes: U32) -> Maybe<&2, Clip>: match c: case Clip{+hz, +nch, fmt, +pcm}: trim.go( trim.fits(U32.to_nat(nch), Nat.add(U32.to_nat(start), U32.to_nat(nframes)), List.length(&2, U32, pcm)), hz, nch, fmt, pcm, Nat.mul(U32.to_nat(start), U32.to_nat(nch)), Nat.mul(U32.to_nat(nframes), U32.to_nat(nch))) def eq.all(ar: Bool, ac: Bool, af: Bool) -> Bool: match ar: case False{}: False{} case True{}: match ac: case False{}: False{} case True{}: af def concat.go( ok: Bool, +rate: U32, +channels: U32, format: Format, left: List<&2, U32>, right: List<&2, U32> ) -> Maybe<&2, Clip>: match ok: case False{}: None{} case True{}: Some{Clip{rate, channels, format, List.append(&2, U32, left, right)}} def concat.pair(+rate: U32, +channels: U32, +format: Format, left: List<&2, U32>, other: Clip) -> Maybe<&2, Clip>: match other: case Clip{+rb, +cb, fb, right}: concat.go(eq.all(U32.is_eq(rate, rb), U32.is_eq(channels, cb), fmt.same(format, fb)), rate, channels, format, left, right) # the frames of both clips, or none when rate, channels, or format differ def concat(left: Clip, right: Clip) -> Maybe<&2, Clip>: match left: case Clip{+hz, +nch, +fmt, pcm}: concat.pair(hz, nch, fmt, pcm, right) def wav.bytes(whole: Bool, +rate: U32, +channels: U32, +kind: U32, frames: List<&2, U32>) -> Maybe<&2, List<&2, U32>>: match whole: case False{}: None{} case True{}: Wav.wav.write(rate, channels, kind, frames) # keep a decoded clip only when it is well formed: one or two channels and # whole frames, the sample count a multiple of the channels def wav.keep(ok: Bool, c: Clip) -> Maybe<&2, Clip>: match ok: case False{}: None{} case True{}: Some{c} def wav.kept(c: Clip) -> Maybe<&2, Clip>: match c: case Clip{+hz, +nch, fmt, +pcm}: wav.keep( Bool.and(Bool.or(U32.is_eq(nch, 1), U32.is_eq(nch, 2)), Nat.is_eq(Nat.mod(List.length(&2, U32, pcm), U32.to_nat(nch)), 0n)), Clip{hz, nch, fmt, pcm}) def wav.parsed(got: Maybe<&2, Wav.Wav>) -> Maybe<&2, Clip>: match got: case None{}: None{} case Some{w}: wav.kept(wav.clip(w)) # a RIFF WAVE clip, or none when the bytes are not mono or stereo PCM or float def decode_wav(xs: List<&2, U32>) -> Maybe<&2, Clip>: wav.parsed(Wav.wav.parse(xs)) # a RIFF WAVE file. none when the samples are not whole frames, the rate or # channels are unfit, an s16 sample does not fit 16 bits, or a size does not fit. def encode_wav(c: Clip) -> Maybe<&2, List<&2, U32>>: match c: case Clip{+hz, +nch, fmt, +pcm}: wav.bytes(Nat.is_eq(Nat.mod(List.length(&2, U32, pcm), U32.to_nat(nch)), 0n), hz, nch, kind.of(fmt), pcm) def mp3.clip(p: Mp3.Pcm) -> Clip: match p: case Mp3.Pcm{+hz, +ch, pcm}: Clip{hz, ch, Flt{}, pcm} # keep a decoded MP3 clip only when it is whole Layer III frames: one or two # channels, and a sample count that is a multiple of 1152 samples a channel def mp3.keep(ok: Bool, c: Clip) -> Maybe<&2, Clip>: match ok: case False{}: None{} case True{}: Some{c} def mp3.kept(c: Clip) -> Maybe<&2, Clip>: match c: case Clip{+hz, +nch, fmt, +pcm}: mp3.keep( Bool.and(Bool.or(U32.is_eq(nch, 1), U32.is_eq(nch, 2)), Nat.is_eq(Nat.mod(List.length(&2, U32, pcm), U32.to_nat((1152 * nch : U32))), 0n)), Clip{hz, nch, fmt, pcm}) def mp3.parsed(got: Maybe<&2, Mp3.Pcm>) -> Maybe<&2, Clip>: match got: case None{}: None{} case Some{p}: mp3.kept(mp3.clip(p)) # MPEG-1 Layer III (ISO/IEC 11172-3) to f32 frames, mono or stereo, at 32000, # 44100 or 48000 Hz. A leading ID3v2 tag is skipped by its size, bytes that are # not an MPEG-1 Layer III header are stepped over, a first Xing or Info frame is # skipped, a CRC is skipped (not checked), and a frame the bytes end inside is # dropped. Big values and count1 quadruples are decoded. None when there is no # frame, when the rate or channels change, and when any frame uses a bit # reservoir (main_data_begin not 0), window switching (block types 1, 2, 3), # scale-factor bits (scalefac_compress not 0), preflag, scalefac_scale, or # mid/side or intensity stereo: this reads streams like shine's and its own, # not typical LAME files. def decode_mp3(xs: List<&2, U32>) -> Maybe<&2, Clip>: mp3.parsed(Mp3.mp3.decode(xs)) # MPEG-1 Layer III at 320 kbit/s, samples / channels / 1152 + 2 frames. none unless the # rate is 32000, 44100 or 48000, there are one or two channels, and there is a sample. def encode_mp3(c: Clip) -> Maybe<&2, List<&2, U32>>: match c: case Clip{+hz, +nch, fmt, pcm}: Mp3.mp3.encode(hz, nch, kind.of(fmt), pcm)