X-Git-Url: https://git.ao2.it/wav_header.git/blobdiff_plain/c494c8415ef810a1ec1db9ee2d0905096dd77aae..1a954d251ea3f121679d4ccbebbf7804a052024a:/wav_header.c diff --git a/wav_header.c b/wav_header.c index cdd64a2..2a90ea7 100644 --- a/wav_header.c +++ b/wav_header.c @@ -11,14 +11,12 @@ */ #include -#include #include -#include #include /* This is the logical arrangement of the struct but we are splitting it - * because it is more easy to handle the variable length extradata + * because it is easier to handle the variable length extradata that way. * * strongly inspired by http://www.mpg123.de/mpg123/mpg123/wav.c */ @@ -36,7 +34,7 @@ struct _RIFF { uint32_t nAvgBytesPerSec; uint16_t nBlockAlign; uint16_t wBitsPerSample; - uint16_t cbSize; /* cbSize = sizeof(extardata) */ + uint16_t cbSize; /* cbSize = sizeof(extradata) */ uint8_t extradata[]; } fmt; struct _data @@ -58,7 +56,7 @@ struct fmt { uint16_t wBitsPerSample; uint16_t cbSize; uint8_t extradata[]; -} __attribute__((__packed__)); +} __attribute__((__packed__)); struct WAVE { char fmtheader[8]; @@ -76,11 +74,23 @@ struct data { }; struct extradata { - uint16_t len; /* same size as fmt.cbSize */ + uint16_t len; /* same storage size as fmt.cbSize */ uint8_t data[]; }; +static inline void fwrite_le16(uint16_t x, FILE *file) +{ + uint16_t tmp = htole16(x); + fwrite(&tmp, sizeof(tmp), 1, file); +} + +static inline void fwrite_le32(uint32_t x, FILE *file) +{ + uint32_t tmp = htole32(x); + fwrite(&tmp, sizeof(tmp), 1, file); +} + void write_wav_header(FILE *file, struct fmt *format, struct extradata *extradata, unsigned data_len) { @@ -106,27 +116,27 @@ void write_wav_header(FILE *file, struct fmt *format, /* RIFF */ fwrite(&r.riffheader, 1, 4, file); - fwrite(&htole32(r.WAVElen), 4, 1, file); + fwrite_le32(r.WAVElen, file); /* WAVE */ fwrite(&w.fmtheader, 1, 8, file); - fwrite(&htole32(w.fmtlen), 4, 1, file); + fwrite_le32(w.fmtlen, file); /* fmt */ - fwrite(&htole16(format->wFormatTag), 2, 1, file); - fwrite(&htole16(format->nChannels), 2, 1, file); - fwrite(&htole32(format->nSamplesPerSec), 4, 1, file); - fwrite(&htole32(format->nAvgBytesPerSec), 4, 1, file); - fwrite(&htole16(format->nBlockAlign), 2, 1, file); - fwrite(&htole16(format->wBitsPerSample), 2, 1, file); - fwrite(&htole16(format->cbSize), 2, 1, file); + fwrite_le16(format->wFormatTag, file); + fwrite_le16(format->nChannels, file); + fwrite_le32(format->nSamplesPerSec, file); + fwrite_le32(format->nAvgBytesPerSec, file); + fwrite_le16(format->nBlockAlign, file); + fwrite_le16(format->wBitsPerSample, file); + fwrite_le16(format->cbSize, file); /* extradata */ fwrite(extradata->data, 1, extradata->len, file); /* data */ fwrite(&d.dataheader, 1, 4, file); - fwrite(&htole32(d.datalen), 4, 1, file); + fwrite_le32(d.datalen, file); } int main(void)