au.ha (1657B)
- // Copyright 2023 Haelwenn (lanodan) Monnier <contact+hare-audio@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- use io;
- use bufio;
- use endian;
- use bytes;
- export type encoding = enum u32 {
- G711_ULAW_8B = 1,
- LPCM_8B = 2,
- LPCM_16B = 3,
- LPCM_24B = 4,
- LPCM_32B = 5,
- FPCM_32B = 6,
- FPCM_64B = 7,
- G721_ADPCM_4B = 23,
- G723_ADPCM_3B = 25,
- G711_ALAW_8B = 27,
- };
- export type header = struct {
- extra_offset: u32,
- length: u32,
- encoding: u32,
- samplerate: u32,
- channels: u32,
- };
- def magic: [4]u8 = ['.', 's', 'n', 'd'];
- export fn write_header(h: io::handle, au: *header) (void | io::error) = {
- let buf: [4]u8 = [0...];
- const offset_base = 24u32;
- io::writeall(h, magic)?;
- endian::beputu32(buf, offset_base + au.extra_offset);
- io::writeall(h, buf)?;
- endian::beputu32(buf, au.length);
- io::writeall(h, buf)?;
- endian::beputu32(buf, au.encoding);
- io::writeall(h, buf)?;
- endian::beputu32(buf, au.samplerate);
- io::writeall(h, buf)?;
- endian::beputu32(buf, au.channels);
- io::writeall(h, buf)?;
- };
- @test fn write_header() void = {
- let header = header {
- extra_offset = 0,
- length = 0,
- encoding = encoding::LPCM_8B,
- samplerate = 8000,
- channels = 1,
- };
- let buf: [24]u8 = [0...];
- let iobuf = bufio::fixed(buf, io::mode::RDWR);
- write_header(&iobuf.stream, &header)!;
- assert(bytes::equal(buf[0..4], magic));
- assert(bytes::equal(buf[4..8], [0, 0, 0, 24])); // offset
- assert(bytes::equal(buf[8..12], [0, 0, 0, 0])); // length
- assert(bytes::equal(buf[12..16], [0, 0, 0, 0x02])); // encoding
- assert(bytes::equal(buf[16..20], [0, 0, 0x1F, 0x40])); // samplerate
- assert(bytes::equal(buf[20..24], [0, 0, 0, 1])); // channels
- };