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

46
include/xtc/xtc_audio.h Normal file
View file

@ -0,0 +1,46 @@
#ifndef XTC_AUDIO_H
#define XTC_AUDIO_H
#include "xtc_project.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct xtc_audio_plan xtc_audio_plan;
typedef struct xtc_recorder xtc_recorder;
/* Control-thread WAV import: PCM16/24/32 or float32, mono/stereo downmixed to
* mono, 1..768000 Hz. Absolute canonical references, <=511 bytes.
* Adds a timeline source -> output route if no timeline source exists.
* Transactional; track must exist. Duration rounds up to a musical tick. */
xtc_result xtc_project_import_audio(xtc_project *project, uint64_t track,
const char *path, uint64_t start_tick, uint64_t *out_clip);
/* Empty path makes a region silent; nonempty path must be absolute. Does not
* open the file. Missing references survive save/load for later relinking. */
xtc_result xtc_project_set_clip_asset(xtc_project *project, uint64_t clip, const char *path);
/* Immutable/preloaded plan. At most 64 distinct assets, 16M mono frames/asset,
* 64M total frames, 256 MiB file and 4096 chunk input limits. Missing/invalid assets become silence with explicit issues.
* OOM/capacity fails creation. No file access during render. */
xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
xtc_audio_plan **out_plan);
void xtc_audio_plan_destroy(xtc_audio_plan *plan);
size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **out_first_path);
/* Single consumer; output replaced with overlap-summed mono at unity gain.
* Linear rate conversion, no time stretching. Output must have frames floats. */
xtc_result xtc_audio_plan_render(const xtc_audio_plan *plan, uint64_t position,
float *output, size_t frames);
/* Control-thread creation opens a same-directory temporary float32 WAV.
* Ring capacity 1..2097152 frames. One callback producer, one control consumer.
* push accepts whole blocks; overflow is sticky and all later input is dropped.
* drain performs file I/O; call regularly. finish requires producer quiescence,
* drains, finalizes and atomically renames even on overflow (partial recovery).
* No source/recorder is freed until destroy, which also removes unfinished temp.
* Counts and sticky error can be observed while producing. */
xtc_result xtc_recorder_create(const char *path, unsigned int rate, size_t capacity,
xtc_recorder **out_recorder);
void xtc_recorder_push(xtc_recorder *recorder, const float *input, size_t frames);
xtc_result xtc_recorder_drain(xtc_recorder *recorder);
xtc_result xtc_recorder_finish(xtc_recorder *recorder);
xtc_result xtc_recorder_status(const xtc_recorder *recorder, uint64_t *out_frames);
void xtc_recorder_destroy(xtc_recorder *recorder);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,6 +1,7 @@
#ifndef XTC_BACKEND_H
#define XTC_BACKEND_H
#include "xtc_engine.h"
#include "xtc_audio.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -18,6 +19,7 @@ typedef struct {
unsigned int buffer_frames;
unsigned int xruns; /* Wraps modulo UINT_MAX + 1. */
unsigned int processed_blocks;
unsigned int asset_issues; /* Unique missing/unreadable/unsupported assets. */
uint64_t sample_position; /* Successful callback frames; retained across stop/submit. */
unsigned int submitted_revision, active_revision;
int running;
@ -44,6 +46,13 @@ xtc_result xtc_backend_seek(xtc_backend *backend, uint64_t sample);
* While stopped, transactionally replaces all retained plans (counters/revisions
* restart); failed compilation leaves the previous engine intact. */
xtc_result xtc_backend_submit(xtc_backend *backend, const xtc_graph *graph);
/* Stopped only. Preloads immutable audio and submits graph before committing.
* Asset issues are silent and reported in status/asset_issue. Other failures
* retain the previous plans. Arrangement edits are adopted on the next prepare. */
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project);
const char *xtc_backend_asset_issue(const xtc_backend *backend);
/* Borrowed recorder, set/detach only while stopped. Stop before finish/destroy. */
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder);
void xtc_backend_collect(xtc_backend *backend);
/* Snapshot fields are individually coherent, not a transactional snapshot. */
xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status *out_status);

View file

@ -32,7 +32,8 @@ typedef enum {
XTC_NODE_GAIN = 1,
XTC_NODE_OSCILLATOR,
XTC_NODE_METER,
XTC_NODE_OUTPUT
XTC_NODE_OUTPUT,
XTC_NODE_AUDIO /* Timeline mix source, output only. */
} xtc_node_type;
typedef struct {
@ -80,7 +81,7 @@ void xtc_graph_destroy(xtc_graph *graph);
xtc_result xtc_graph_add_gain(xtc_graph *graph, float gain, size_t *out_node_id);
xtc_result xtc_graph_connect(xtc_graph *graph, size_t source_node,
size_t destination_node);
/* Port 0 is input; port 1 is output. Oscillators have only output 1;
/* Port 0 is input; port 1 is output. Oscillators and audio sources have only output 1;
* output nodes have only input 0. All ports carry mono audio. */
xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
size_t port_id, xtc_port_info *out_port);
@ -90,6 +91,7 @@ xtc_result xtc_graph_connect_ports(xtc_graph *graph, size_t source_node,
xtc_result xtc_graph_add_oscillator(xtc_graph *graph, float frequency,
size_t *out_node_id);
xtc_result xtc_graph_add_meter(xtc_graph *graph, size_t *out_node_id);
xtc_result xtc_graph_add_audio(xtc_graph *graph, size_t *out_node_id);
xtc_result xtc_graph_add_output(xtc_graph *graph, size_t *out_node_id);
/* Compile copies graph state; caller owns the plan. Sample rate must be
* finite and positive, oscillator frequencies <= Nyquist. max_frames: 1..65536.
@ -104,6 +106,9 @@ void xtc_plan_destroy(xtc_plan *plan);
* Sums explicit output nodes; if absent, sums terminal nodes for compatibility
* unless require_output_nodes is set. Empty graphs produce silence.
* Unconnected gain/meter/output inputs receive external input. */
/* timeline is a separate mono input for XTC_NODE_AUDIO, NULL means silence. */
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline,
float *output, size_t frames);
xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output,
size_t frame_count);
/* Resets oscillator phases and meters, without allocation. */

View file

@ -7,15 +7,17 @@ extern "C" {
#define XTC_PROJECT_MAX_TRACKS 64U
#define XTC_PROJECT_MAX_CLIPS 1024U
#define XTC_PROJECT_NAME_BYTES 64U
#define XTC_ASSET_PATH_BYTES 512U
#define XTC_TICKS_PER_QUARTER 960U
#define XTC_PROJECT_MAX_TICK UINT64_C(38400000)
typedef struct xtc_project xtc_project;
typedef struct { uint64_t id; char name[XTC_PROJECT_NAME_BYTES]; } xtc_track_info;
/* Silent arrangement region: audio assets and MIDI content are future phases.
/* Arrangement region: empty asset_path means silent metadata.
* All musical positions are integer ticks, constant tempo and 4/4 meter. */
typedef struct {
uint64_t id, track_id, start_tick, length_tick, offset_tick, source_length_tick;
char name[XTC_PROJECT_NAME_BYTES];
char asset_path[XTC_ASSET_PATH_BYTES];
} xtc_clip_info;
/* Single control-thread ownership. Names: nonempty UTF-8, <=63 bytes, no ASCII controls.
* IDs are monotonic, never reused, UINT64_MAX reserved. Snapshots are copies. */

View file

@ -31,6 +31,8 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
* frame_count must be <= configured max_frames and output must hold it.
* Oversized buffers are silenced too; callers must bound their own frame count.
* NULL input means silence. Zero frames is a no-op. */
xtc_result xtc_realtime_process_audio(xtc_realtime *engine, const float *input, const float *timeline,
float *output, size_t frames);
xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
float *output, size_t frame_count);
#ifdef __cplusplus