main.bend checks
raw source on the hub · import emerging-ezaudio@0.7.0.0/main.bend as Main
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.
3 imports
import Base import ./src/wav.bend as Wav import ./src/mp3.bend as Mp3
Types
type Format source · line 11 · raw
Data
signed 16-bit PCM, or IEEE 754 binary32
S16Format
FltFormat
type Clip source · line 16 · raw
Data
one clip. pcm is interleaved. s16 lives in the low 16 bits.
Clip@hz:U32 -> @nch:U32 -> @fmt:Format -> @pcm:List<&2, U32> -> Clip
Definitions
def s16 source · line 20 · raw
Format
signed 16-bit little-endian PCM
def f32 source · line 24 · raw
Format
IEEE 754 binary32, stored as its bits
def kind.of source · line 27 · raw
@fmt:Format -> U32
def fmt.of source · line 34 · raw
@+kind:U32 -> Format
def wav.clip source · line 41 · raw
@w:0x17b1d81789c1d8452301453a7acd4c81/src/wav.Wav -> Clip
def fmt.same source · line 46 · raw
@aa:Format -> @bb:Format -> Bool
def clip source · line 58 · raw
@+rate:U32 -> @+channels:U32 -> @format:Format -> @frames:List<&2, U32> -> Clip
a clip from its rate, channel count, format, and interleaved frames
def rate source · line 62 · raw
@c:Clip -> U32
the sample rate, in hertz
def channels source · line 68 · raw
@c:Clip -> U32
how many channels are interleaved
def format source · line 74 · raw
@c:Clip -> Format
s16 or f32
def frames source · line 80 · raw
@c:Clip -> List<&2, U32>
the interleaved frames
def count.at source · line 85 · raw
@zero:Bool -> @+channels:U32 -> @+nsamples:U32 -> U32
def count.of source · line 92 · raw
@+channels:U32 -> @+nsamples:U32 -> U32
def count source · line 96 · raw
@c:Clip -> U32
how many frames. zero channels count as none.
def fill source · line 103 · raw
@+rate:U32 -> @+channels:U32 -> @format:Format -> @+sample:U32 -> @+nframes:U32 -> Clip
one sample, repeated across nframes frames: nframes * channels samples, counted as natural numbers so the product does not wrap a U32
def ls.drop source · line 106 · raw
@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>
def ls.take source · line 117 · raw
@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>
def trim.go source · line 128 · raw
@ok:Bool -> @+rate:U32 -> @+channels:U32 -> @format:Format -> @frames:List<&2, U32> -> @+skip:Nat -> @+keep:Nat -> Maybe<&2, Clip>
def trim.fits source · line 145 · raw
@+chn:Nat -> @+span:Nat -> @+nsamples:Nat -> Bool
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 source · line 149 · raw
@c:Clip -> @+start:U32 -> @+nframes:U32 -> Maybe<&2, Clip>
frames [start, start + nframes). none when that span passes the end.
def eq.all source · line 156 · raw
@ar:Bool -> @ac:Bool -> @af:Bool -> Bool
def concat.go source · line 167 · raw
@ok:Bool -> @+rate:U32 -> @+channels:U32 -> @format:Format -> @left:List<&2, U32> -> @right:List<&2, U32> -> Maybe<&2, Clip>
def concat.pair source · line 181 · raw
@+rate:U32 -> @+channels:U32 -> @+format:Format -> @left:List<&2, U32> -> @other:Clip -> Maybe<&2, Clip>
def concat source · line 188 · raw
@left:Clip -> @right:Clip -> Maybe<&2, Clip>
the frames of both clips, or none when rate, channels, or format differ
def wav.bytes source · line 193 · raw
@whole:Bool -> @+rate:U32 -> @+channels:U32 -> @+kind:U32 -> @frames:List<&2, U32> -> Maybe<&2, List<&2, U32>>
def wav.keep source · line 202 · raw
@ok:Bool -> @c:Clip -> Maybe<&2, Clip>
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.kept source · line 209 · raw
@c:Clip -> Maybe<&2, Clip>
def wav.parsed source · line 217 · raw
@got:Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/wav.Wav> -> Maybe<&2, Clip>
def decode_wav source · line 225 · raw
@xs:List<&2, U32> -> Maybe<&2, Clip>
a RIFF WAVE clip, or none when the bytes are not mono or stereo PCM or float
def encode_wav source · line 230 · raw
@c:Clip -> Maybe<&2, List<&2, U32>>
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 mp3.clip source · line 235 · raw
@p:0x17b1d81789c1d8452301453a7acd4c81/src/mp3.Pcm -> Clip
def mp3.keep source · line 242 · raw
@ok:Bool -> @c:Clip -> Maybe<&2, Clip>
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.kept source · line 249 · raw
@c:Clip -> Maybe<&2, Clip>
def mp3.parsed source · line 257 · raw
@got:Maybe<&2, 0x17b1d81789c1d8452301453a7acd4c81/src/mp3.Pcm> -> Maybe<&2, Clip>
def decode_mp3 source · line 274 · raw
@xs:List<&2, U32> -> Maybe<&2, Clip>
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 encode_mp3 source · line 279 · raw
@c:Clip -> Maybe<&2, List<&2, U32>>
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.