audio.au
Reading and writing the Sun and NeXT au format.
import audio.au
The au format is the simplest real sound file there is: a 24 byte big-endian header and then the samples. It came from Sun, it is what /dev/audio spoke, and it is still a pleasant way to hand raw audio between programs because there is almost nothing to get wrong.
It carries more encodings than its size suggests: mu-law and A-law, signed PCM at 8, 16, 24 and 32 bits, and IEEE floating point at 32 and 64. Everything is big-endian.
Things to know
- 8 bit au PCM is signed, where 8 bit WAVE is unsigned. The shared helpers in audio take that as an argument, so neither format has to guess.
- A companded file decodes to a clip marked 16 bit, because that is the depth mu-law and A-law reconstruct to.
data_offsetmay be larger thanHeaderSize; the gap is a text note, and this package steps over it.- A data size of 0xFFFFFFFF means unknown, which this package treats as everything to the end of the file.
Choosing an encoding
An AU file has a 24 byte header followed by the samples, and the header names the encoding. EncodeAUEncoding takes one of EncPcm16, EncMuLaw, EncALaw, EncFloat32 and the others the package lists. For 64 mono frames the three files are 152, 88 and 280 bytes: 16 bit PCM, one byte per sample, and 32 bit floats.
import audio
import audio.au
import compress
import math
func main:
mut c := audio.New(8000, 1, 64)
audio.SetSample(c, 3, 0, 0.5)
mut pcm := au.EncodeAUEncoding(c, au.EncPcm16)
mut mu := au.EncodeAUEncoding(c, au.EncMuLaw)
mut fl := au.EncodeAUEncoding(c, au.EncFloat32)
println compress.BufLen(pcm), compress.BufLen(mu), compress.BufLen(fl)
mut h := au.Header {}
println au.ReadHeader(mu.data, mu.len, h)
println h.encoding == au.EncMuLaw, h.rate, h.channels, h.data_size
mut ok := false
mut back := au.DecodeAU(mu.data, mu.len, ok)
println ok, back.bits, math.Abs(audio.Sample(back, 3, 0) - 0.5) < 0.02
compress.BufFree(pcm)
compress.BufFree(mu)
compress.BufFree(fl)
audio.Free(c)
audio.Free(back)
endReadHeader fills a Header with the encoding, rate, channels and data size. DecodeAU expands the mu-law file to a 16 bit clip, and the sample is within 0.02 of the original.
Saving, probing and opening
SaveAU writes 16 bit PCM and returns true on success. ProbeAU works on bytes, so the program encodes the clip first and probes the buffer: rate, depth and frame count come from the header without decoding.
import audio
import audio.au
import compress
import filepath
import mem
import os
func main:
mut c := audio.New(22050, 1, 64)
audio.SetSample(c, 3, 0, 0.5)
path := filepath.Join(os.TempDir(), "salam_guide.au")
defer mem.Free(path as void*)
println au.SaveAU(c, path)
mut buf := au.EncodeAU(c)
mut ok := false
info := au.ProbeAU(buf.data, buf.len, ok)
println ok, info.rate, info.bits, info.frames
mut open_ok := false
mut back := au.OpenAU(path, open_ok)
println open_ok, back.frames, back.rate, audio.Sample(back, 3, 0)
compress.BufFree(buf)
audio.Free(c)
audio.Free(back)
endOpenAU reads the file written to disk and returns the clip with its rate and frames intact. Free the buffer and every clip when you are done.
Types
Header
struct Header| Name | Type | Description |
|---|---|---|
data_offset | int = 0 | Where the samples start. At least HeaderSize, and anything between is a text note. |
data_size | int = 0 | How many bytes of samples there are, held inside the file. |
encoding | int = 0 | Which Enc number the samples use. |
rate | int = 0 | Frames per second. |
channels | int = 0 | How many channels. |
import audio
import audio.au
import compress
import filepath
import math
import mem
import os
func main:
mut c := audio.New(22050, 1, 64)
audio.SetSample(c, 3, 0, 0.5)
mut buf := au.EncodeAUEncoding(c, au.EncPcm16)
println au.IsAU(buf.data, buf.len), au.IsAU(buf.data, 8)
println audio.FormatName(audio.DetectFormat(buf.data, buf.len))
mut h := au.Header {}
println au.ReadHeader(buf.data, buf.len, h)
println h.data_offset == au.HeaderSize, h.encoding == au.EncPcm16
println h.rate, h.channels, h.data_size
mut ok := false
info := au.ProbeAU(buf.data, buf.len, ok)
println ok, info.rate, info.channels, info.bits, info.frames, info.lossless
mut dok := false
mut back := au.DecodeAU(buf.data, buf.len, dok)
println dok, back.frames, back.bits, audio.Sample(back, 3, 0)
mut fbuf := au.EncodeAUEncoding(c, au.EncFloat32)
mut fok := false
mut fback := au.DecodeAU(fbuf.data, fbuf.len, fok)
println fok, fback.bits, audio.Sample(fback, 3, 0) == audio.Sample(c, 3, 0)
mut mbuf := au.EncodeAUEncoding(c, au.EncMuLaw)
mut mok := false
mut mback := au.DecodeAU(mbuf.data, mbuf.len, mok)
println mok, mback.bits, math.Abs(audio.Sample(mback, 3, 0) - 0.5) < 0.02
mut minfo := false
println au.ProbeAU(mbuf.data, mbuf.len, minfo).lossless
println compress.BufLen(mbuf) == au.HeaderSize + 64
println compress.BufLen(au.EncodeAUEncoding(c, 99))
println compress.BufLen(au.EncodeAU(c)) == au.HeaderSize + 128
path := filepath.Join(os.TempDir(), "salam_au_example.au")
defer mem.Free(path as void*)
println au.SaveAU(c, path)
mut diskok := false
mut disk := au.OpenAU(path, diskok)
println diskok, disk.frames, disk.rate
mut gone := false
missing := filepath.Join(os.TempDir(), "salam_au_none.au")
defer mem.Free(missing as void*)
au.OpenAU(missing, gone)
println gone
compress.BufFree(buf)
compress.BufFree(fbuf)
compress.BufFree(mbuf)
audio.Free(c)
audio.Free(back)
audio.Free(fback)
audio.Free(mback)
audio.Free(disk)
endConstants and variables
HeaderSize
const HeaderSize: i32 = 24The example for this item is shown with the ones above: au-Read.salam
Encoding numbers Enc…
| Name | Value |
|---|---|
EncMuLaw | 1 |
EncPcm8 | 2 |
EncPcm16 | 3 |
EncPcm24 | 4 |
EncPcm32 | 5 |
EncFloat32 | 6 |
EncFloat64 | 7 |
EncALaw | 27 |
The example for this item is shown with the ones above: au-Read.salam
Functions
IsAU
func IsAU(p: void*, n: int): boolThe example for this item is shown with the ones above: au-Read.salam
ReadHeader
func ReadHeader(p: void*, n: int, out &: Header): boolThe example for this item is shown with the ones above: au-Read.salam
ProbeAU
func ProbeAU(p: void*, n: int, ok &: bool): audio.InfoThe example for this item is shown with the ones above: au-Read.salam
DecodeAU
func DecodeAU(p: void*, n: int, ok &: bool): audio.ClipThe example for this item is shown with the ones above: au-Read.salam
EncodeAUEncoding
func EncodeAUEncoding(c: audio.Clip, encoding: int): compress.ByteBufThe example for this item is shown with the ones above: au-Read.salam
EncodeAU
func EncodeAU(c: audio.Clip): compress.ByteBufThe example for this item is shown with the ones above: au-Read.salam
OpenAU
func OpenAU(path: str, ok &: bool): audio.ClipThe example for this item is shown with the ones above: au-Read.salam
SaveAU
func SaveAU(c: audio.Clip, path: str): boolThe example for this item is shown with the ones above: au-Read.salam