logo

hare-audio

Audio format library for Hare git clone https://anongit.hacktivis.me/git/hare-audio.git/

au.ha (1657B)


  1. // Copyright 2023 Haelwenn (lanodan) Monnier <contact+hare-audio@hacktivis.me>
  2. // SPDX-License-Identifier: MPL-2.0
  3. use io;
  4. use bufio;
  5. use endian;
  6. use bytes;
  7. export type encoding = enum u32 {
  8. G711_ULAW_8B = 1,
  9. LPCM_8B = 2,
  10. LPCM_16B = 3,
  11. LPCM_24B = 4,
  12. LPCM_32B = 5,
  13. FPCM_32B = 6,
  14. FPCM_64B = 7,
  15. G721_ADPCM_4B = 23,
  16. G723_ADPCM_3B = 25,
  17. G711_ALAW_8B = 27,
  18. };
  19. export type header = struct {
  20. extra_offset: u32,
  21. length: u32,
  22. encoding: u32,
  23. samplerate: u32,
  24. channels: u32,
  25. };
  26. def magic: [4]u8 = ['.', 's', 'n', 'd'];
  27. export fn write_header(h: io::handle, au: *header) (void | io::error) = {
  28. let buf: [4]u8 = [0...];
  29. const offset_base = 24u32;
  30. io::writeall(h, magic)?;
  31. endian::beputu32(buf, offset_base + au.extra_offset);
  32. io::writeall(h, buf)?;
  33. endian::beputu32(buf, au.length);
  34. io::writeall(h, buf)?;
  35. endian::beputu32(buf, au.encoding);
  36. io::writeall(h, buf)?;
  37. endian::beputu32(buf, au.samplerate);
  38. io::writeall(h, buf)?;
  39. endian::beputu32(buf, au.channels);
  40. io::writeall(h, buf)?;
  41. };
  42. @test fn write_header() void = {
  43. let header = header {
  44. extra_offset = 0,
  45. length = 0,
  46. encoding = encoding::LPCM_8B,
  47. samplerate = 8000,
  48. channels = 1,
  49. };
  50. let buf: [24]u8 = [0...];
  51. let iobuf = bufio::fixed(buf, io::mode::RDWR);
  52. write_header(&iobuf.stream, &header)!;
  53. assert(bytes::equal(buf[0..4], magic));
  54. assert(bytes::equal(buf[4..8], [0, 0, 0, 24])); // offset
  55. assert(bytes::equal(buf[8..12], [0, 0, 0, 0])); // length
  56. assert(bytes::equal(buf[12..16], [0, 0, 0, 0x02])); // encoding
  57. assert(bytes::equal(buf[16..20], [0, 0, 0x1F, 0x40])); // samplerate
  58. assert(bytes::equal(buf[20..24], [0, 0, 0, 1])); // channels
  59. };