~/bend-docscommunity

src/wav.bend checks

raw source on the hub · import emerging-ezaudio@0.6.0.0/src/wav.bend as Wav

RIFF WAVE. PCM is little-endian signed 16-bit. Float is IEEE binary32.

The reader walks the chunks in order: fmt first, then any chunks it does not know, then data, whose samples it reads straight from the input. It stops there, as libsndfile, dr_wav and Python's wave do, so what follows the data chunk (a LIST chunk, an ID3 tag, junk) is not read. A data chunk larger than what is left (a truncated file, or 0xFFFFFFFF from a streamed writer) is clamped to the bytes present, and the samples to whole frames.

1 import
import Base

Types

type Wav source · line 12 · raw

Data

a parsed wave. kind 1 is signed 16-bit, kind 3 is binary32. frames are interleaved.

type Fm source · line 16 · raw

Data

fmt fields that passed the checks

type Step source · line 232 · raw

Data

one step of the chunk walk: done, or the chunks left and the fmt seen so far

Definitions

def tag.riff source · line 20 · raw

U32

'RIFF'

def tag.wave source · line 24 · raw

U32

'WAVE'

def tag.fmt source · line 28 · raw

U32

'fmt '

def tag.data source · line 32 · raw

U32

'data'

def tag.fact source · line 36 · raw

U32

'fact'

def be.u32 source · line 39 · raw

@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> @+dd:U32 -> U32

def le.u16 source · line 42 · raw

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

def le.u32 source · line 45 · raw

@+aa:U32 -> @+bb:U32 -> @+cc:U32 -> @+dd:U32 -> U32

def put.u16 source · line 48 · raw

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

def put.u32 source · line 51 · raw

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

def put.be source · line 54 · raw

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

def put.lo source · line 58 · raw

@+vv:U32 -> U32

def put.hi source · line 61 · raw

@+vv:U32 -> U32

def put.b2 source · line 64 · raw

@+vv:U32 -> U32

def put.b3 source · line 67 · raw

@+vv:U32 -> U32

def ls.skip source · line 71 · raw

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

the list after its first hop elements, or [] when it is shorter

def chunk.span source · line 83 · raw

@+sz:U32 -> Nat

a chunk's payload and its pad byte: an odd size carries one

def fmt.rate source · line 86 · raw

@ok:Bool -> @+kind:U32 -> @+ch:U32 -> @+rate:U32 -> Maybe<&2, Fm>

def fmt.ok source · line 96 · raw

@+kind:U32 -> @+ch:U32 -> @+rate:U32 -> @+bits:U32 -> Maybe<&2, Fm>

mono or stereo 16-bit PCM or 32-bit float at a non-zero rate. The block align and byte rate are not checked: they follow from the channels and the bits, and readers such as Python's wave and dr_wav derive them the same way.

def guid.tail source · line 110 · raw

List<&2, U32>

the tail of KSDATAFORMAT_SUBTYPE_PCM and _IEEE_FLOAT after their first word

def guid.is source · line 114 · raw

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

do the bytes of xs begin ys: ok carries the match so far and stops the walk

def guid.kind source · line 129 · raw

@ok:Bool -> @+kind:U32 -> U32

def fmt.sub source · line 138 · raw

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

WAVE_FORMAT_EXTENSIBLE: after cbSize, the valid bits and the channel mask, the first word of the sub-format GUID is the format tag

def fmt.tag.at source · line 147 · raw

@ext:Bool -> @+sz:U32 -> @+tag:U32 -> @rest:List<&2, U32> -> U32

the format tag: 1 and 3 as they are, 0xFFFE through its sub-format when the chunk holds it (40 bytes or more), and 0 for anything else

def fmt.pack source · line 154 · raw

@+sz:U32 -> @+kind:U32 -> @+ch:U32 -> @+rate:U32 -> @+bits:U32 -> @rest:List<&2, U32> -> Maybe<&2, Fm>

def fmt.read source · line 164 · raw

@+sz:U32 -> @ys:List<&2, U32> -> Maybe<&2, Fm>

def fmt.long source · line 172 · raw

@ok:Bool -> @+sz:U32 -> @ys:List<&2, U32> -> Maybe<&2, Fm>

def fmt.fields source · line 180 · raw

@+sz:U32 -> @ys:List<&2, U32> -> Maybe<&2, Fm>

the fmt chunk's fields. A chunk under 16 bytes is none.

def pcm.s16 source · line 183 · raw

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

def pcm.f32 source · line 194 · raw

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

def pcm.of source · line 205 · raw

@+kind:U32 -> @nn:Nat -> @ys:List<&2, U32> -> List<&2, U32>

def width.nat source · line 213 · raw

@+kind:U32 -> Nat

bytes per sample

def data.count source · line 221 · raw

@+kind:U32 -> @+ch:U32 -> @+have:Nat -> Nat

the samples in the whole frames of have bytes

def data.read source · line 225 · raw

@fm:Fm -> @+sz:U32 -> @+rest:List<&2, U32> -> Maybe<&2, Wav>

the data chunk: its size clamped to the bytes left, its samples to whole frames

def step.fmt source · line 236 · raw

@got:Maybe<&2, Fm> -> @rest:List<&2, U32> -> Step

def step.pre.data source · line 244 · raw

@isd:Bool -> @+sz:U32 -> @rest:List<&2, U32> -> Step

a chunk before fmt: fmt is read, data is none, anything else is skipped

def step.pre source · line 251 · raw

@isf:Bool -> @+tag:U32 -> @+sz:U32 -> @+rest:List<&2, U32> -> Step

def step.post source · line 259 · raw

@isd:Bool -> @fm:Fm -> @+sz:U32 -> @rest:List<&2, U32> -> Step

a chunk after fmt: data is read and ends the walk, anything else is skipped

def step.on source · line 266 · raw

@got:Maybe<&2, Fm> -> @+tag:U32 -> @+sz:U32 -> @rest:List<&2, U32> -> Step

def step.of source · line 274 · raw

@got:Maybe<&2, Fm> -> @xs:List<&2, U32> -> Step

read one chunk header. Fewer than eight bytes left, and no data chunk, is none.

def chunk.walk source · line 283 · raw

@fuel:Nat -> @st:Step -> Maybe<&2, Wav>

walk the chunks. Each step reads an eight-byte header, so the fuel, one more than the bytes, is never spent first.

def open.ok source · line 294 · raw

@ok:Bool -> @+rest:List<&2, U32> -> Maybe<&2, Wav>

def wav.parse source · line 303 · raw

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

bytes to a wave, or none when the form is not mono or stereo PCM or float. The RIFF size is not read: the walk stops at the data chunk.

def enc.s16 source · line 312 · raw

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

def enc.f32 source · line 319 · raw

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

def enc.pcm source · line 326 · raw

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

def enc.ch source · line 333 · raw

@+nc:U32 -> Bool

def enc.both source · line 342 · raw

@okc:Bool -> @okr:Bool -> Bool

def enc.ok source · line 349 · raw

@+nc:U32 -> @+fk:U32 -> @+rt:U32 -> Bool

def enc.bytes source · line 358 · raw

@+kind:U32 -> @+ns:U32 -> U32

def enc.cat source · line 365 · raw

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

def align.of source · line 368 · raw

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

def width.of source · line 375 · raw

@+kind:U32 -> U32

def enc.low16 source · line 383 · raw

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

every sample is below 2^16: ok carries the check so far and stops the walk

def enc.fits source · line 395 · raw

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

every s16 sample fits its 16 bits; binary32 words always fit

def head.n source · line 404 · raw

@+kind:U32 -> U32

the header bytes before the samples: 44 for PCM, 58 for float, which also carries cbSize and a fact chunk

def enc.sized source · line 412 · raw

@+rate:U32 -> @+ch:U32 -> @+kind:U32 -> @+ns:U32 -> Bool

the byte rate, the data size and the RIFF size all fit a U32

def enc.fmt.body source · line 416 · raw

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

def enc.fmt source · line 424 · raw

@+rate:U32 -> @+ch:U32 -> @+kind:U32 -> @+ns:U32 -> List<&2, U32>

the fmt chunk and, for float, its cbSize and a fact chunk with the frame count

def enc.head source · line 435 · raw

@+rate:U32 -> @+ch:U32 -> @+kind:U32 -> @+ns:U32 -> @+nb:U32 -> List<&2, U32>

def write.some source · line 442 · raw

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

def write.if source · line 451 · raw

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

def write.n source · line 465 · raw

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

def wav.write source · line 471 · raw

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

a wave file, or none when the clip is not 16-bit PCM or 32-bit float, mono or stereo, an s16 sample does not fit 16 bits, or a size does not fit a U32