Phase 6: Add WAV clips, graph playback and bounded recording

This commit is contained in:
Johannes Findeisen 2026-10-27 00:31:03 +01:00
commit 8a37090b81
40 changed files with 1450 additions and 69 deletions

10
src/audio/README.md Normal file
View file

@ -0,0 +1,10 @@
# Audio files and capture
C99/POSIX control code loads bounded WAV files and prepares immutable clip
plans. Callback rendering reads only preloaded samples. The audio source node
passes the mono timeline mix through the ordinary modular graph.
The recorder uses a lock-free SPSC ring: callback push, control-thread drain,
and finish/destroy only after callback quiescence. WAV headers, file writing
and final rename occur on the control thread. See docs/AUDIO_FILES.md and
include/xtc/xtc_audio.h for ownership, limits and recovery behavior.

132
src/audio/arrangement.c Normal file
View file

@ -0,0 +1,132 @@
#define _XOPEN_SOURCE 700
#include "wave_internal.h"
#include "../project/project_internal.h"
#include "../graph/graph_internal.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct { uint64_t start, end, offset; size_t asset; } region;
struct xtc_audio_plan {
xtc_wave waves[64];
char paths[64][XTC_ASSET_PATH_BYTES];
region regions[XTC_PROJECT_MAX_CLIPS];
size_t assets, count, issues;
char first_issue[XTC_ASSET_PATH_BYTES];
unsigned int rate;
};
xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track,
const char *path, uint64_t start, uint64_t *out_clip)
{
xtc_wave wave = {0};
xtc_project *candidate = NULL;
char *absolute;
uint64_t length, id = 0;
size_t i, source = SIZE_MAX, output = SIZE_MAX;
xtc_result result;
if (!project || !path || !out_clip) return XTC_ERR_INVALID_ARGUMENT;
absolute = realpath(path, NULL);
if (!absolute) return XTC_ERR_IO;
if (strlen(absolute) >= XTC_ASSET_PATH_BYTES) { free(absolute); return XTC_ERR_CAPACITY; }
result = xtc_wave_load(absolute, &wave);
if (result != XTC_OK) { free(absolute); return result; }
length = ((uint64_t)wave.frames * project->tempo * XTC_TICKS_PER_QUARTER +
(uint64_t)wave.rate * 60000U - 1U) / ((uint64_t)wave.rate * 60000U);
free(wave.samples);
result = xtc_project_clone(project, &candidate);
if (result == XTC_OK) result = xtc_project_add_clip(candidate, track, "Audio", start, length, &id);
if (result == XTC_OK) result = xtc_project_set_clip_asset(candidate, id, absolute);
free(absolute);
if (result == XTC_OK) {
for (i = 0; i < candidate->graph->node_count; ++i) {
xtc_node *n = candidate->graph->nodes + i;
if (n->type == XTC_NODE_AUDIO) source = n->id;
if (n->type == XTC_NODE_OUTPUT) output = n->id;
}
if (source == SIZE_MAX) {
result = xtc_graph_add_audio(candidate->graph, &source);
if (result == XTC_OK && output == SIZE_MAX) result = xtc_graph_add_output(candidate->graph, &output);
if (result == XTC_OK) result = xtc_graph_connect(candidate->graph, source, output);
}
}
if (result == XTC_OK) {
xtc_project old = *project; *project = *candidate; *candidate = old;
*out_clip = id;
}
xtc_project_destroy(candidate);
return result;
}
void xtc_audio_plan_destroy(xtc_audio_plan *plan)
{
size_t i;
if (!plan) return;
for (i = 0; i < plan->assets; ++i) free(plan->waves[i].samples);
free(plan);
}
xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate, xtc_audio_plan **out)
{
xtc_audio_plan *plan;
size_t i, total = 0;
xtc_result result = XTC_OK;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!project || !rate || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
plan = calloc(1, sizeof(*plan));
if (!plan) return XTC_ERR_OUT_OF_MEMORY;
plan->rate = rate;
for (i = 0; i < project->clip_count; ++i) {
const xtc_clip_info *clip = project->clips + i;
size_t a; region *r;
if (!clip->asset_path[0]) continue;
for (a = 0; a < plan->assets; ++a) if (!strcmp(plan->paths[a], clip->asset_path)) break;
if (a == plan->assets) {
if (a == 64) { result = XTC_ERR_CAPACITY; break; }
strcpy(plan->paths[a], clip->asset_path); ++plan->assets;
result = xtc_wave_load(clip->asset_path, plan->waves + a);
if (result == XTC_ERR_CAPACITY || result == XTC_ERR_OUT_OF_MEMORY) break;
if (result != XTC_OK) {
++plan->issues;
if (!plan->first_issue[0]) strcpy(plan->first_issue, clip->asset_path);
}
result = XTC_OK;
total += plan->waves[a].frames;
if (total > 67108864U) { result = XTC_ERR_CAPACITY; break; }
}
r = plan->regions + plan->count++;
r->asset = a;
result = xtc_project_tick_to_sample(project, clip->start_tick, rate, &r->start);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->start_tick + clip->length_tick, rate, &r->end);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->offset_tick, rate, &r->offset);
if (result != XTC_OK) break;
}
if (result != XTC_OK) { xtc_audio_plan_destroy(plan); return result; }
*out = plan; return XTC_OK;
}
size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
{
if (path) *path = plan ? plan->first_issue : "";
return plan ? plan->issues : 0;
}
xtc_result xtc_audio_plan_render(const xtc_audio_plan *plan, uint64_t position, float *output, size_t frames)
{
size_t i, j;
if (!plan || !output || frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
memset(output, 0, frames * sizeof(*output));
for (i = 0; i < plan->count; ++i) {
const region *r = plan->regions + i;
const xtc_wave *wave = plan->waves + r->asset;
uint64_t begin = position > r->start ? position : r->start;
uint64_t end = position + frames < r->end ? position + frames : r->end;
if (!wave->samples || end <= begin) continue;
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
double source = (double)(position + j - r->start + r->offset) * wave->rate / plan->rate;
size_t index;
double fraction;
if (source >= (double)wave->frames) continue;
index = (size_t)source; fraction = source - (double)index;
output[j] += (float)(wave->samples[index] * (1.0 - fraction) +
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
}
}
for (j = 0; j < frames; ++j) if (!isfinite(output[j])) output[j] = 0;
return XTC_OK;
}

105
src/audio/recorder.c Normal file
View file

@ -0,0 +1,105 @@
#define _POSIX_C_SOURCE 200809L
#include "wave_internal.h"
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <unistd.h>
struct xtc_recorder {
float *ring;
size_t capacity;
uint64_t write_position, read_position, written;
unsigned int error, rate;
FILE *file;
char *path, *temporary;
int finished;
};
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity, xtc_recorder **out)
{
xtc_recorder *r;
size_t length = 0;
int fd;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!path || !rate || rate > 768000 || !capacity || capacity > 2097152U) return XTC_ERR_INVALID_ARGUMENT;
while (length < 4096 && path[length]) ++length;
if (!length || length == 4096) return XTC_ERR_INVALID_ARGUMENT;
if (sizeof(float) != 4 || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) return XTC_ERR_UNSUPPORTED;
if (!__atomic_always_lock_free(sizeof(uint64_t), 0) ||
!__atomic_always_lock_free(sizeof(unsigned int), 0)) return XTC_ERR_UNSUPPORTED;
r = calloc(1, sizeof(*r));
if (!r) return XTC_ERR_OUT_OF_MEMORY;
r->capacity = capacity; r->rate = rate;
r->ring = malloc(capacity * sizeof(float));
r->path = malloc(length + 1); r->temporary = calloc(length + 12, 1);
if (!r->ring || !r->path || !r->temporary) { xtc_recorder_destroy(r); return XTC_ERR_OUT_OF_MEMORY; }
memcpy(r->path, path, length + 1);
memcpy(r->temporary, path, length); memcpy(r->temporary + length, ".tmp.XXXXXX", 12);
fd = mkstemp(r->temporary);
if (fd < 0) { xtc_recorder_destroy(r); return XTC_ERR_IO; }
r->file = fdopen(fd, "w+b");
if (!r->file) { close(fd); xtc_recorder_destroy(r); return XTC_ERR_IO; }
if (!xtc_wave_header(r->file, rate, 0)) { xtc_recorder_destroy(r); return XTC_ERR_IO; }
*out = r; return XTC_OK;
}
void xtc_recorder_push(xtc_recorder *r, const float *input, size_t frames)
{
uint64_t write, read;
size_t i;
if (!r || !frames || __atomic_load_n(&r->error, __ATOMIC_ACQUIRE)) return;
write = __atomic_load_n(&r->write_position, __ATOMIC_RELAXED);
read = __atomic_load_n(&r->read_position, __ATOMIC_ACQUIRE);
if (!input || frames > r->capacity || frames > r->capacity - (size_t)(write - read) ||
write > UINT64_C(1073741814) - frames) {
__atomic_store_n(&r->error, (unsigned int)(input ? XTC_ERR_CAPACITY : XTC_ERR_IO), __ATOMIC_RELEASE);
return;
}
for (i = 0; i < frames; ++i) r->ring[(size_t)((write + i) % r->capacity)] = input[i];
__atomic_store_n(&r->write_position, write + frames, __ATOMIC_RELEASE);
}
xtc_result xtc_recorder_drain(xtc_recorder *r)
{
uint64_t read, write;
if (!r || !r->file || r->finished) return XTC_ERR_INVALID_STATE;
read = __atomic_load_n(&r->read_position, __ATOMIC_RELAXED);
write = __atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE);
while (read < write) {
size_t offset = (size_t)(read % r->capacity);
size_t count = r->capacity - offset;
if (count > write - read) count = (size_t)(write - read);
if (!xtc_wave_write(r->file, r->ring + offset, count)) {
__atomic_store_n(&r->error, (unsigned int)XTC_ERR_IO, __ATOMIC_RELEASE);
return XTC_ERR_IO;
}
read += count; r->written += count;
__atomic_store_n(&r->read_position, read, __ATOMIC_RELEASE);
}
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
xtc_result xtc_recorder_status(const xtc_recorder *r, uint64_t *frames)
{
if (!r || !frames) return XTC_ERR_INVALID_ARGUMENT;
*frames = __atomic_load_n(&r->write_position, __ATOMIC_ACQUIRE);
return (xtc_result)__atomic_load_n(&r->error, __ATOMIC_ACQUIRE);
}
xtc_result xtc_recorder_finish(xtc_recorder *r)
{
xtc_result result;
int failed = 0;
if (!r || !r->file || r->finished) return XTC_ERR_INVALID_STATE;
result = xtc_recorder_drain(r);
if (result == XTC_ERR_IO) failed = 1;
if (fseek(r->file, 0, SEEK_SET) || !xtc_wave_header(r->file, r->rate, (uint32_t)r->written) ||
fflush(r->file) || fsync(fileno(r->file))) failed = 1;
if (fclose(r->file)) failed = 1;
r->file = NULL;
if (!failed && rename(r->temporary, r->path)) failed = 1;
r->finished = 1;
return failed ? XTC_ERR_IO : result;
}
void xtc_recorder_destroy(xtc_recorder *r)
{
if (!r) return;
if (r->file) fclose(r->file);
if (r->temporary) unlink(r->temporary);
free(r->temporary); free(r->path); free(r->ring); free(r);
}

115
src/audio/wave.c Normal file
View file

@ -0,0 +1,115 @@
#include "wave_internal.h"
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
static uint32_t u32(const unsigned char *p)
{ return (uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U | (uint32_t)p[3] << 24U; }
static unsigned int u16(const unsigned char *p)
{ return (unsigned int)p[0] | (unsigned int)p[1] << 8U; }
static void put32(unsigned char *p, uint32_t v)
{ size_t i; for (i = 0; i < 4; ++i) { p[i] = (unsigned char)(v & 255U); v >>= 8U; } }
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames)
{
unsigned char h[44] = {0};
memcpy(h, "RIFF", 4); put32(h + 4, 36U + frames * 4U);
memcpy(h + 8, "WAVEfmt ", 8); put32(h + 16, 16);
h[20] = 3; h[22] = 1; put32(h + 24, rate); put32(h + 28, rate * 4U);
h[32] = 4; h[34] = 32; memcpy(h + 36, "data", 4); put32(h + 40, frames * 4U);
return fwrite(h, 1, sizeof(h), file) == sizeof(h);
}
int xtc_wave_write(FILE *file, const float *input, size_t frames)
{
unsigned char bytes[4096];
while (frames) {
size_t i, count = frames > 1024 ? 1024 : frames;
for (i = 0; i < count; ++i) {
uint32_t bits; float value = isfinite(input[i]) ? input[i] : 0.0f;
memcpy(&bits, &value, 4); put32(bytes + i * 4, bits);
}
if (fwrite(bytes, 4, count, file) != count) return 0;
input += count; frames -= count;
}
return 1;
}
xtc_result xtc_wave_load(const char *path, xtc_wave *out)
{
FILE *f;
unsigned char h[40], bytes[8];
uint64_t remaining;
uint32_t data_size = 0, rate = 0;
unsigned int format = 0, channels = 0, bits = 0, align = 0;
long data_offset = 0, file_size;
size_t i, frames, chunks = 0;
float *samples = NULL;
xtc_result result = XTC_ERR_FORMAT;
if (!path || !out) return XTC_ERR_INVALID_ARGUMENT;
memset(out, 0, sizeof(*out));
if (sizeof(float) != 4 || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128)
return XTC_ERR_UNSUPPORTED;
f = fopen(path, "rb");
if (!f) return XTC_ERR_IO;
if (fseek(f, 0, SEEK_END) || (file_size = ftell(f)) < 12 || fseek(f, 0, SEEK_SET)) goto done;
if (file_size > 268435456L) { result = XTC_ERR_CAPACITY; goto done; }
if (fread(h, 1, 12, f) != 12 || memcmp(h, "RIFF", 4) || memcmp(h + 8, "WAVE", 4)) goto done;
remaining = u32(h + 4);
if (remaining < 4 || remaining + 8U > (uint64_t)file_size) goto done;
remaining -= 4;
while (remaining) {
uint32_t size; uint64_t padded;
if (++chunks > 4096 || remaining < 8 || fread(h, 1, 8, f) != 8) goto done;
size = u32(h + 4); padded = (uint64_t)size + (size & 1U);
remaining -= 8;
if (padded > 0x7fffffffU || padded > remaining) goto done;
if (!memcmp(h, "fmt ", 4)) {
if (format || size < 16 || fread(h + 8, 1, 16, f) != 16) goto done;
format = u16(h + 8); channels = u16(h + 10); rate = u32(h + 12);
align = u16(h + 20); bits = u16(h + 22);
if (fseek(f, (long)(padded - 16), SEEK_CUR)) goto done;
} else {
if (!memcmp(h, "data", 4)) {
if (data_offset) goto done;
data_offset = ftell(f); data_size = size;
}
if (padded > 0x7fffffffU || fseek(f, (long)padded, SEEK_CUR)) goto done;
}
remaining -= padded;
}
if ((format != 1 && format != 3) || (channels != 1 && channels != 2) ||
(bits != 16 && bits != 24 && bits != 32) || (format == 3 && bits != 32)) {
result = XTC_ERR_UNSUPPORTED; goto done;
}
if (!rate || rate > 768000 || !data_offset || !data_size ||
align != channels * (bits / 8U) || data_size % align) goto done;
frames = data_size / align;
if (frames > 16777216U) { result = XTC_ERR_CAPACITY; goto done; }
samples = malloc(frames * sizeof(*samples));
if (!samples) { result = XTC_ERR_OUT_OF_MEMORY; goto done; }
if (fseek(f, data_offset, SEEK_SET)) goto done;
for (i = 0; i < frames; ++i) {
unsigned int c; double sum = 0;
if (fread(bytes, 1, align, f) != align) goto done;
for (c = 0; c < channels; ++c) {
const unsigned char *p = bytes + c * (bits / 8U);
if (format == 3) {
uint32_t value = u32(p); float sample; memcpy(&sample, &value, 4);
if (!isfinite(sample)) goto done;
sum += sample;
} else {
uint32_t value = bits == 16 ? u16(p) : bits == 24 ?
(uint32_t)p[0] | (uint32_t)p[1] << 8U | (uint32_t)p[2] << 16U : u32(p);
int64_t signed_value = value;
if (value & (UINT32_C(1) << (bits - 1U))) signed_value -= INT64_C(1) << bits;
sum += (double)signed_value / (double)(INT64_C(1) << (bits - 1U));
}
}
samples[i] = (float)(sum / channels);
}
if (ferror(f)) goto done;
result = XTC_OK;
done:
if (fclose(f) && result == XTC_OK) result = XTC_ERR_IO;
if (result == XTC_OK) { out->samples = samples; out->frames = frames; out->rate = rate; }
else free(samples);
return result;
}

View file

@ -0,0 +1,9 @@
#ifndef XTC_WAVE_INTERNAL_H
#define XTC_WAVE_INTERNAL_H
#include "xtc/xtc_audio.h"
#include <stdio.h>
typedef struct { float *samples; size_t frames; unsigned int rate; } xtc_wave;
xtc_result xtc_wave_load(const char *path, xtc_wave *out_wave);
int xtc_wave_header(FILE *file, unsigned int rate, uint32_t frames);
int xtc_wave_write(FILE *file, const float *input, size_t frames);
#endif