refactor: replace manual WAV I/O with libsndfile

Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-12 19:15:12 +00:00
parent 3e52142f62
commit 525516fe03

127
src/wav.c
View File

@@ -2,113 +2,40 @@
#include "channel.h" #include "channel.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <sndfile.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
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;
}
int wav_read(const char *path, float **buffer, unsigned *frames) { int wav_read(const char *path, float **buffer, unsigned *frames) {
int fd = open(path, O_RDONLY); SF_INFO info;
if (fd < 0) return -1; info.format = 0;
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); SNDFILE *sf = sf_open(path, SFM_READ, &info);
char riff[4]; if (!sf) return -1;
if (read(fd, riff, 4) != 4 || memcmp(riff, "RIFF", 4) != 0) { close(fd); return -1; }
uint32_t chunk_size; /* We need mono 16-bit PCM; refuse anything else */
if (read_uint32(fd, &chunk_size) != 0) { close(fd); return -1; } if (info.channels != 1 || info.samplerate <= 0) {
char wave[4]; sf_close(sf);
if (read(fd, wave, 4) != 4 || memcmp(wave, "WAVE", 4) != 0) { close(fd); return -1; } 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);
} }
if (audio_format != 1 || num_channels != 1 || bits_per_sample != 16) {
close(fd); return -1; unsigned total = (info.frames > (sf_count_t)LOOP_BUF_SIZE) ? LOOP_BUF_SIZE : (unsigned)info.frames;
} float *buf = (float*)malloc(total * sizeof(float));
unsigned max_frames = LOOP_BUF_SIZE; if (!buf) { sf_close(sf); return -1; }
unsigned total_frames = 0;
float *buf = (float*)malloc(max_frames * sizeof(float)); sf_count_t nread = sf_readf_float(sf, buf, total);
if (!buf) { close(fd); return -1; } sf_close(sf);
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);
*buffer = buf; *buffer = buf;
*frames = total_frames; *frames = (unsigned)nread;
return 0; return 0;
} }
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) { 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); SF_INFO info;
if (fd < 0) return -1; info.samplerate = sample_rate;
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); info.channels = 1;
unsigned data_bytes = frames * 2; info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
unsigned file_size = 44 + data_bytes; SNDFILE *sf = sf_open(path, SFM_WRITE, &info);
unsigned char header[44]; if (!sf) return -1;
memset(header, 0, 44);
memcpy(header, "RIFF", 4); sf_writef_float(sf, data, frames);
header[4] = (unsigned char)( file_size & 0xff); sf_close(sf);
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);
return 0; return 0;
} }