From 525516fe03e4cb42fe6327c22e164f3118ce6ffb Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Tue, 12 May 2026 19:15:12 +0000 Subject: [PATCH] refactor: replace manual WAV I/O with libsndfile Co-authored-by: aider (deepseek/deepseek-reasoner) --- src/wav.c | 127 ++++++++++++------------------------------------------ 1 file changed, 27 insertions(+), 100 deletions(-) diff --git a/src/wav.c b/src/wav.c index 29cf849..4abfcaa 100644 --- a/src/wav.c +++ b/src/wav.c @@ -2,113 +2,40 @@ #include "channel.h" #include #include -#include -#include -#include -#include - -static inline int read_uint16(int fd, uint16_t *v) { - return read(fd, v, sizeof(uint16_t)) == sizeof(uint16_t) ? 0 : -1; -} -static inline int read_uint32(int fd, uint32_t *v) { - return read(fd, v, sizeof(uint32_t)) == sizeof(uint32_t) ? 0 : -1; -} +#include int wav_read(const char *path, float **buffer, unsigned *frames) { - int fd = open(path, O_RDONLY); - if (fd < 0) return -1; - posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); - char riff[4]; - if (read(fd, riff, 4) != 4 || memcmp(riff, "RIFF", 4) != 0) { close(fd); return -1; } - uint32_t chunk_size; - if (read_uint32(fd, &chunk_size) != 0) { close(fd); return -1; } - char wave[4]; - if (read(fd, wave, 4) != 4 || memcmp(wave, "WAVE", 4) != 0) { close(fd); return -1; } - uint32_t fmt_size = 0; - uint16_t audio_format = 0; - uint16_t num_channels = 0; - uint32_t sample_rate = 0; - uint16_t bits_per_sample = 0; - while (1) { - char sub_id[4]; - if (read(fd, sub_id, 4) != 4) { close(fd); return -1; } - if (read_uint32(fd, &fmt_size) != 0) { close(fd); return -1; } - if (memcmp(sub_id, "fmt ", 4) == 0) { - if (read_uint16(fd, &audio_format) != 0) { close(fd); return -1; } - if (read_uint16(fd, &num_channels) != 0) { close(fd); return -1; } - if (read_uint32(fd, &sample_rate) != 0) { close(fd); return -1; } - if (read_uint16(fd, &bits_per_sample) != 0){ close(fd); return -1; } - unsigned consumed = 2 + 2 + 4 + 2; /* format, channels, sample_rate, bits_per_sample */ - if (fmt_size > consumed) lseek(fd, fmt_size - consumed, SEEK_CUR); - continue; - } - if (memcmp(sub_id, "data", 4) == 0) break; - lseek(fd, fmt_size, SEEK_CUR); + SF_INFO info; + info.format = 0; + SNDFILE *sf = sf_open(path, SFM_READ, &info); + if (!sf) return -1; + + /* We need mono 16-bit PCM; refuse anything else */ + if (info.channels != 1 || info.samplerate <= 0) { + sf_close(sf); + return -1; } - if (audio_format != 1 || num_channels != 1 || bits_per_sample != 16) { - close(fd); return -1; - } - unsigned max_frames = LOOP_BUF_SIZE; - unsigned total_frames = 0; - float *buf = (float*)malloc(max_frames * sizeof(float)); - if (!buf) { close(fd); return -1; } - while (total_frames < max_frames) { - int16_t sample; - ssize_t n = read(fd, &sample, 2); - if (n < 2) break; - buf[total_frames++] = sample / 32768.0f; - } - close(fd); + + unsigned total = (info.frames > (sf_count_t)LOOP_BUF_SIZE) ? LOOP_BUF_SIZE : (unsigned)info.frames; + float *buf = (float*)malloc(total * sizeof(float)); + if (!buf) { sf_close(sf); return -1; } + + sf_count_t nread = sf_readf_float(sf, buf, total); + sf_close(sf); *buffer = buf; - *frames = total_frames; + *frames = (unsigned)nread; return 0; } int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) { - int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) return -1; - posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); - unsigned data_bytes = frames * 2; - unsigned file_size = 44 + data_bytes; - unsigned char header[44]; - memset(header, 0, 44); - memcpy(header, "RIFF", 4); - header[4] = (unsigned char)( file_size & 0xff); - header[5] = (unsigned char)((file_size>>8) & 0xff); - header[6] = (unsigned char)((file_size>>16) & 0xff); - header[7] = (unsigned char)((file_size>>24) & 0xff); - memcpy(header+8, "WAVE", 4); - memcpy(header+12, "fmt ", 4); - header[16]=16; header[17]=0; header[18]=0; header[19]=0; - header[20]=1; header[21]=0; - header[22]=1; header[23]=0; - unsigned sr = sample_rate; - header[24] = (unsigned char)( sr & 0xff); - header[25] = (unsigned char)((sr>>8) & 0xff); - header[26] = (unsigned char)((sr>>16)& 0xff); - header[27] = (unsigned char)((sr>>24)& 0xff); - unsigned br = sr * 2; - header[28] = (unsigned char)( br & 0xff); - header[29] = (unsigned char)((br>>8) & 0xff); - header[30] = (unsigned char)((br>>16)& 0xff); - header[31] = (unsigned char)((br>>24)& 0xff); - header[32]=2; header[33]=0; - header[34]=16; header[35]=0; - memcpy(header+36, "data", 4); - header[40] = (unsigned char)( data_bytes & 0xff); - header[41] = (unsigned char)((data_bytes>>8) & 0xff); - header[42] = (unsigned char)((data_bytes>>16)& 0xff); - header[43] = (unsigned char)((data_bytes>>24)& 0xff); - ssize_t written = write(fd, header, 44); - if (written != 44) { close(fd); return -1; } - for (unsigned i = 0; i < frames; ++i) { - float s = data[i]; - if (s < -1.0f) s = -1.0f; - if (s > 1.0f) s = 1.0f; - int16_t sample = (int16_t)(s * 32767); - written = write(fd, &sample, 2); - if (written != 2) { close(fd); return -1; } - } - close(fd); + SF_INFO info; + info.samplerate = sample_rate; + info.channels = 1; + info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; + SNDFILE *sf = sf_open(path, SFM_WRITE, &info); + if (!sf) return -1; + + sf_writef_float(sf, data, frames); + sf_close(sf); return 0; }