logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git

au.h (1217B)


  1. #include <unistd.h> // write()
  2. #include <inttypes.h> // uint32_t
  3. #include <stdio.h> // perror()
  4. struct au_header {
  5. uint32_t offset;
  6. uint32_t length;
  7. uint32_t encoding;
  8. uint32_t samplerate;
  9. uint32_t channels;
  10. };
  11. enum au_header_encoding {
  12. AU_ENCODING_8B_G711_ULAW = 01,
  13. AU_ENCODING_8B_LPCM = 02,
  14. AU_ENCODING_16B_LPCM = 03,
  15. AU_ENCODING_24B_LPCM = 04,
  16. AU_ENCODING_32B_LPCM = 05,
  17. AU_ENCODING_32B_FPCM = 06,
  18. AU_ENCODING_64B_FPCM = 07,
  19. AU_ENCODING_4B_G721_ADPCM = 23,
  20. AU_ENCODING_3B_G723_ADPCM = 25,
  21. AU_ENCODING_8B_G711_ALAW = 27,
  22. };
  23. static void
  24. be32enc(void *buf, uint32_t in)
  25. {
  26. char *out = (char *)buf;
  27. out[0] = (in >> 24) & 0xff;
  28. out[1] = (in >> 16) & 0xff;
  29. out[2] = (in >> 8) & 0xff;
  30. out[3] = (in >> 0) & 0xff;
  31. }
  32. static int
  33. write_au_header(int fd, struct au_header *header)
  34. {
  35. char buf[4];
  36. uint32_t *value = (uint32_t*)header;
  37. if(4 != write(fd, "\x2E\x73\x6E\x64", 4))
  38. {
  39. perror("write(fd, signature, 4)");
  40. return -1;
  41. }
  42. // 5: number of params in struct au_header
  43. for(int i = 0;i<5; i++)
  44. {
  45. // 32 bits = 4 octets
  46. be32enc(buf, *(value+i));
  47. if(4 != write(fd, buf, 4))
  48. {
  49. perror("write(fd, au_header, 4)");
  50. return -1;
  51. }
  52. }
  53. return 0;
  54. }