logo

bytemedia

Home to byte-level sounds, images, videos, …
commit: 6962a6a68324dab4f0b8d2c400e8f08f7a4b3f3e
parent 21c8c3e4cbd5fa58439f1bbea09c338448c36fa8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 26 Oct 2020 12:08:10 +0100

C/au.h: New header

Diffstat:

MC/AU 8-bit 8kHz.c26+++++++++++++++++---------
AC/au.h49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/C/AU 8-bit 8kHz.c b/C/AU 8-bit 8kHz.c @@ -1,15 +1,23 @@ #define _POSIX_C_SOURCE 200809L -// 16k Because 8kHz × 8-bit #include <stdio.h> -#include <unistd.h> #include <inttypes.h> // uint32_t -int main(void){ -write(1, "\x2e\x73\x6e\x64", 4); // AU magic header (big-endian ".snd") -write(1, "\x00\x00\x00\x18", 4); // 24 = byte offset, no annotation -write(1, "\xFF\xFF\xFF\xFF", 4); // 0xffffffff = unknown data size -write(1, "\x00\x00\x00\x02", 4); // 10 = (unsigned) 8-bit fixed-point -write(1, "\x00\x00\x1F\x40", 4); // 8000 = Hz -write(1, "\x00\x00\x00\x01", 4); // mono + +#include "au.h" + +int +main(void) +{ + struct au_header header = { + .offset = 24, // no-annotation, in octets + .lenght = 0xFFFFFFFF, // unknown data size + .encoding = 02, // unsigned 8-bit linear PCM + .samplerate = 8000, // Hz + .channels = 1 + }; + + // fd 1 is stdout + write_au_header(1, &header); + for(int t = 0;;t++)putchar( // 8kHz //t*(((t>>12)|(t>>8))&(63&(t>>4))) diff --git a/C/au.h b/C/au.h @@ -0,0 +1,49 @@ +#include <unistd.h> // write() +#include <inttypes.h> // uint32_t +#include <stdio.h> // perror() + +struct au_header { + uint32_t offset; + uint32_t lenght; + uint32_t encoding; + uint32_t samplerate; + uint32_t channels; +}; + +static void +be32enc(void *buf, uint32_t in) +{ + char *out = (char *)buf; + + out[0] = (in >> 24) & 0xff; + out[1] = (in >> 16) & 0xff; + out[2] = (in >> 8) & 0xff; + out[3] = (in >> 0) & 0xff; +} + +static int +write_au_header(int fd, struct au_header *header) +{ + char buf[4]; + uint32_t *value = (uint32_t*)header; + + if(4 != write(fd, "\x2E\x73\x6E\x64", 4)) + { + perror("write(fd, signature, 4)"); + return -1; + } + + // 5: number of params in struct au_header + for(int i = 0;i<5; i++) + { + // 32 bits = 4 octets + be32enc(buf, *(value+i)); + if(4 != write(fd, buf, 4)) + { + perror("write(fd, au_header, 4)"); + return -1; + } + } + + return 0; +}