diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-10-08 09:02:54 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-10-08 09:02:54 +0200 |
| commit | 978aa89541181c7e5b114589fac686a96702d371 (patch) | |
| tree | 5dd902164af651c8029be43332c2fe6e932e674e /tools/wav.c | |
| parent | a5af9c3d351085f4005a949f8c382a3aad4e0b29 (diff) | |
| download | svad-master.tar.gz svad-master.zip | |
Diffstat (limited to '')
| -rw-r--r-- | tools/wav.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tools/wav.c b/tools/wav.c new file mode 100644 index 0000000..b2a5330 --- /dev/null +++ b/tools/wav.c @@ -0,0 +1,138 @@ +#include "wav.h" + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static int rd_u32(FILE *f, uint32_t *v) { + unsigned char b[4]; + if (fread(b, 1, 4, f) != 4) + return -1; + *v = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24; + return 0; +} + +static int rd_u16(FILE *f, uint16_t *v) { + unsigned char b[2]; + if (fread(b, 1, 2, f) != 2) + return -1; + *v = (uint16_t)((uint32_t)b[0] | (uint32_t)b[1] << 8); + return 0; +} + +#define FAIL(msg) \ + do { \ + if (err) \ + *err = (msg); \ + goto fail; \ + } while (0) + +int wav_read_mono_s16(const char *path, wav_t *out, const char **err) { + memset(out, 0, sizeof *out); + FILE *f = fopen(path, "rb"); + if (!f) { + if (err) + *err = "cannot open file"; + return -1; + } + + char id[4]; + uint32_t riff_size = 0; + if (fread(id, 1, 4, f) != 4 || memcmp(id, "RIFF", 4) != 0) + FAIL("not a RIFF file"); + if (rd_u32(f, &riff_size) != 0) + FAIL("truncated header"); + if (fread(id, 1, 4, f) != 4 || memcmp(id, "WAVE", 4) != 0) + FAIL("not a WAVE file"); + + uint16_t fmt = 0, channels = 0, bits = 0; + uint32_t rate = 0; + int have_fmt = 0; + + for (;;) { + uint32_t size; + if (fread(id, 1, 4, f) != 4 || rd_u32(f, &size) != 0) + break; /* end of file, or a truncated trailing chunk */ + + if (memcmp(id, "fmt ", 4) == 0) { + uint16_t block_align; + uint32_t byte_rate; + if (size < 16) + FAIL("short fmt chunk"); + if (rd_u16(f, &fmt) || rd_u16(f, &channels) || rd_u32(f, &rate) || + rd_u32(f, &byte_rate) || rd_u16(f, &block_align) || rd_u16(f, &bits)) + FAIL("truncated fmt chunk"); + if (fseek(f, (long)size - 16, SEEK_CUR) != 0) + FAIL("bad fmt chunk size"); + have_fmt = 1; + continue; + } + + if (memcmp(id, "data", 4) == 0) { + if (!have_fmt) + FAIL("data chunk before fmt chunk"); + /* Format 1 is PCM; 0xFFFE is extensible, whose subformat this + * reader does not inspect, so it is accepted only at 16 bits. */ + if ((fmt != 1 && fmt != 0xFFFE) || bits != 16) + FAIL("only 16-bit PCM is supported"); + if (channels == 0) + FAIL("zero channels"); + + const size_t frames = size / (2u * channels); + if (frames == 0) + FAIL("empty data chunk"); + int16_t *mono = malloc(frames * sizeof(int16_t)); + if (!mono) + FAIL("out of memory"); + + const size_t step = channels; + int16_t buf[1024]; + size_t got = 0; + const size_t batch_frames = sizeof buf / sizeof buf[0] / step; + if (batch_frames == 0) { + free(mono); + FAIL("too many channels"); + } + while (got < frames) { + const size_t take = (frames - got) < batch_frames ? frames - got : batch_frames; + const size_t want = take * step; + const size_t n = fread(buf, sizeof(int16_t), want, f); + if (n < step) + break; + for (size_t i = 0; i + step <= n; i += step) { + int64_t sum = 0; + for (size_t c = 0; c < step; ++c) + sum += buf[i + c]; + mono[got++] = (int16_t)lrint((double)sum / channels); + } + if (n < want) + break; + } + if (got == 0) { + free(mono); + FAIL("no samples read"); + } + out->samples = mono; + out->count = got; + out->sample_rate = (int)rate; + out->channels = (int)channels; + fclose(f); + return 0; + } + + /* Skip unknown chunks; RIFF chunks are word aligned. */ + if (fseek(f, (long)size + (size & 1u), SEEK_CUR) != 0) + break; + } + FAIL("no data chunk"); + +fail: + fclose(f); + return -1; +} + +void wav_free(wav_t *w) { + free(w->samples); + memset(w, 0, sizeof *w); +} |
