feat: add WAV load/save and ring buffer implementation

Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-11 21:15:12 +00:00
parent 6b490ed739
commit 5a2414b4c3
9 changed files with 319 additions and 3 deletions

113
src/wav.c Normal file
View File

@@ -0,0 +1,113 @@
#include "wav.h"
#include "channel.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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 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; }
if (fmt_size > 16) lseek(fd, fmt_size - 16, 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 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);
*buffer = buf;
*frames = total_frames;
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);
return 0;
}