refactor: replace writer thread with synchronous save and fix ring buffer memory ordering

This commit is contained in:
Loic Coenen
2026-05-18 17:35:31 +00:00
committed by Loic Coenen (aider)
parent 10e47e6c0c
commit f38797fe0a
22 changed files with 291 additions and 1369 deletions

View File

@@ -1,41 +1,49 @@
#include "wav.h"
#include "channel.h"
#include <sndfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int wav_read(const char *path, float **buffer, unsigned *frames) {
SF_INFO info;
info.format = 0;
SNDFILE *sf = sf_open(path, SFM_READ, &info);
if (!sf) return -1;
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;
}
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);
/* We need mono 16-bit PCM; refuse anything else */
if (info.channels != 1 || info.samplerate <= 0) {
sf_close(sf);
*buffer = buf;
*frames = (unsigned)nread;
return 0;
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));
if (!buf) {
sf_close(sf);
return -1;
}
sf_count_t nread = sf_readf_float(sf, buf, total);
sf_close(sf);
*buffer = buf;
*frames = (unsigned)nread;
return 0;
}
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) {
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;
int wav_write(const char *path, const float *data, unsigned frames,
unsigned sample_rate) {
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;
sf_writef_float(sf, data, frames);
sf_close(sf);
return 0;
}