Phase 6: Add WAV clips, graph playback and bounded recording
This commit is contained in:
parent
65640c4aad
commit
8a37090b81
40 changed files with 1450 additions and 69 deletions
10
src/audio/README.md
Normal file
10
src/audio/README.md
Normal 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
132
src/audio/arrangement.c
Normal 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
105
src/audio/recorder.c
Normal 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
115
src/audio/wave.c
Normal 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;
|
||||
}
|
||||
9
src/audio/wave_internal.h
Normal file
9
src/audio/wave_internal.h
Normal 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
|
||||
|
|
@ -11,6 +11,9 @@ struct xtc_backend {
|
|||
jack_client_t *client;
|
||||
jack_port_t *input, *output;
|
||||
xtc_realtime *engine;
|
||||
xtc_audio_plan *audio;
|
||||
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
|
||||
float *timeline;
|
||||
size_t max_frames;
|
||||
uint64_t sample_position;
|
||||
unsigned int expected_rate;
|
||||
|
|
@ -25,6 +28,7 @@ static int process(jack_nframes_t frames, void *context)
|
|||
float *output = jack_port_get_buffer(backend->output, frames);
|
||||
xtc_result result = XTC_OK;
|
||||
if (output == NULL) {
|
||||
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
|
||||
__atomic_store_n(&backend->process_result, (unsigned int)XTC_ERR_IO, __ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -35,14 +39,23 @@ static int process(jack_nframes_t frames, void *context)
|
|||
result = XTC_ERR_CAPACITY;
|
||||
} else {
|
||||
const float *input = jack_port_get_buffer(backend->input, frames);
|
||||
result = xtc_realtime_process(backend->engine, input, output, (size_t)frames);
|
||||
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
|
||||
if (backend->recorder) xtc_recorder_push(backend->recorder, input, (size_t)frames);
|
||||
if (backend->audio)
|
||||
result = xtc_audio_plan_render(backend->audio, position, backend->timeline, (size_t)frames);
|
||||
if (result == XTC_OK)
|
||||
result = xtc_realtime_process_audio(backend->engine, input,
|
||||
backend->audio ? backend->timeline : NULL, output, (size_t)frames);
|
||||
}
|
||||
if (result == XTC_OK) {
|
||||
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
|
||||
if (position > UINT64_MAX - frames) result = XTC_ERR_CAPACITY;
|
||||
else __atomic_store_n(&backend->sample_position, position + frames, __ATOMIC_RELAXED);
|
||||
}
|
||||
if (result != XTC_OK) memset(output, 0, (size_t)frames * sizeof(*output));
|
||||
if (result != XTC_OK) {
|
||||
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
|
||||
memset(output, 0, (size_t)frames * sizeof(*output));
|
||||
}
|
||||
__atomic_store_n(&backend->process_result, (unsigned int)result, __ATOMIC_RELAXED);
|
||||
return 0; /* Keep connected and silent on recoverable processing errors. */
|
||||
}
|
||||
|
|
@ -87,9 +100,11 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
if (strlen(config->client_name) >= (size_t)jack_client_name_size()) return XTC_ERR_INVALID_ARGUMENT;
|
||||
backend = calloc(1U, sizeof(*backend));
|
||||
if (backend == NULL) return XTC_ERR_OUT_OF_MEMORY;
|
||||
backend->timeline = calloc(config->max_frames, sizeof(float));
|
||||
if (!backend->timeline) { free(backend); return XTC_ERR_OUT_OF_MEMORY; }
|
||||
backend->max_frames = config->max_frames;
|
||||
backend->client = jack_client_open(config->client_name, JackNoStartServer, &status);
|
||||
if (backend->client == NULL) { free(backend); return XTC_ERR_IO; }
|
||||
if (backend->client == NULL) { free(backend->timeline); free(backend); return XTC_ERR_IO; }
|
||||
backend->expected_rate = (unsigned int)jack_get_sample_rate(backend->client);
|
||||
backend->rate = backend->expected_rate;
|
||||
backend->frames = (unsigned int)jack_get_buffer_size(backend->client);
|
||||
|
|
@ -118,6 +133,8 @@ fail:
|
|||
return XTC_ERR_IO;
|
||||
}
|
||||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
return result;
|
||||
}
|
||||
|
|
@ -128,6 +145,8 @@ xtc_result xtc_backend_close(xtc_backend *backend)
|
|||
/* JACK removes this client and joins callbacks before returning success. */
|
||||
if (jack_client_close(backend->client) != 0) return XTC_ERR_IO;
|
||||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -182,6 +201,33 @@ xtc_result xtc_backend_submit(xtc_backend *backend, const xtc_graph *graph)
|
|||
}
|
||||
return xtc_realtime_submit(backend->engine, graph);
|
||||
}
|
||||
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project)
|
||||
{
|
||||
xtc_audio_plan *candidate = NULL;
|
||||
xtc_result result;
|
||||
if (!backend || !project) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready || backend->running) return XTC_ERR_INVALID_STATE;
|
||||
result = xtc_audio_plan_create(project, backend->expected_rate, &candidate);
|
||||
if (result == XTC_OK) result = xtc_backend_submit(backend, xtc_project_graph(project));
|
||||
if (result != XTC_OK) { xtc_audio_plan_destroy(candidate); return result; }
|
||||
xtc_audio_plan_destroy(backend->audio); backend->audio = candidate;
|
||||
return XTC_OK;
|
||||
}
|
||||
const char *xtc_backend_asset_issue(const xtc_backend *backend)
|
||||
{
|
||||
const char *path = "";
|
||||
if (backend) xtc_audio_plan_issues(backend->audio, &path);
|
||||
return path;
|
||||
}
|
||||
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder)
|
||||
{
|
||||
if (!backend) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready || backend->running) return XTC_ERR_INVALID_STATE;
|
||||
if (recorder && (__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
|
||||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE))) return XTC_ERR_INVALID_STATE;
|
||||
backend->recorder = recorder;
|
||||
return XTC_OK;
|
||||
}
|
||||
void xtc_backend_collect(xtc_backend *backend)
|
||||
{ if (backend != NULL) xtc_realtime_collect(backend->engine); }
|
||||
xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status *out_status)
|
||||
|
|
@ -190,6 +236,7 @@ xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status
|
|||
if (backend == NULL || out_status == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready) return XTC_ERR_INVALID_STATE;
|
||||
(void)xtc_realtime_get_status(backend->engine, &engine_status);
|
||||
out_status->asset_issues = (unsigned int)xtc_audio_plan_issues(backend->audio, NULL);
|
||||
out_status->sample_rate = __atomic_load_n(&backend->rate, __ATOMIC_RELAXED);
|
||||
out_status->buffer_frames = __atomic_load_n(&backend->frames, __ATOMIC_RELAXED);
|
||||
out_status->xruns = __atomic_load_n(&backend->xruns, __ATOMIC_RELAXED);
|
||||
|
|
@ -250,6 +297,12 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
|
|||
{ return route(backend, direction, remote, 1); }
|
||||
|
||||
#else
|
||||
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project)
|
||||
{ (void)backend; (void)project; return XTC_ERR_UNSUPPORTED; }
|
||||
const char *xtc_backend_asset_issue(const xtc_backend *backend)
|
||||
{ (void)backend; return ""; }
|
||||
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder)
|
||||
{ (void)backend; (void)recorder; return XTC_ERR_UNSUPPORTED; }
|
||||
xtc_result xtc_backend_seek(xtc_backend *backend, uint64_t sample)
|
||||
{ (void)backend; (void)sample; return XTC_ERR_UNSUPPORTED; }
|
||||
/* Keep core/offline builds available on machines without JACK headers. */
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_
|
|||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output,
|
||||
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline, float *output,
|
||||
size_t frame_count)
|
||||
{
|
||||
size_t index, frame, edge;
|
||||
|
|
@ -126,7 +126,9 @@ xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output,
|
|||
double phase = plan->phases[node];
|
||||
for (frame = 0U; frame < frame_count; ++frame) {
|
||||
float sample = 0.0f;
|
||||
if (spec.type == XTC_NODE_OSCILLATOR) {
|
||||
if (spec.type == XTC_NODE_AUDIO) {
|
||||
sample = timeline ? timeline[frame] : 0.0f;
|
||||
} else if (spec.type == XTC_NODE_OSCILLATOR) {
|
||||
sample = (float)sin(XTC_TAU * phase);
|
||||
phase += (double)spec.value / plan->sample_rate;
|
||||
if (phase >= 1.0) phase -= 1.0;
|
||||
|
|
@ -167,3 +169,6 @@ xtc_result xtc_graph_process_offline(xtc_graph *graph, const float *input,
|
|||
xtc_plan_destroy(plan);
|
||||
return result;
|
||||
}
|
||||
|
||||
xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output, size_t frames)
|
||||
{ return xtc_plan_process_audio(plan, input, NULL, output, frames); }
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
|
|||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
|
||||
xtc_result xtc_realtime_process_audio(xtc_realtime *engine, const float *input, const float *timeline,
|
||||
float *output, size_t frame_count)
|
||||
{
|
||||
xtc_result result = XTC_OK;
|
||||
|
|
@ -107,7 +107,7 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
|
|||
}
|
||||
}
|
||||
if (engine->active != NULL)
|
||||
result = xtc_plan_process(engine->active, input, output, frame_count);
|
||||
result = xtc_plan_process_audio(engine->active, input, timeline, output, frame_count);
|
||||
else
|
||||
memset(output, 0, frame_count * sizeof(*output));
|
||||
}
|
||||
|
|
@ -117,3 +117,6 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
|
|||
__atomic_load_n(&engine->blocks, __ATOMIC_RELAXED) + 1U, __ATOMIC_RELAXED);
|
||||
return result;
|
||||
}
|
||||
|
||||
xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input, float *output, size_t frames)
|
||||
{ return xtc_realtime_process_audio(engine, input, NULL, output, frames); }
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ xtc_result xtc_graph_add_oscillator(xtc_graph *graph, float frequency, size_t *o
|
|||
}
|
||||
xtc_result xtc_graph_add_meter(xtc_graph *graph, size_t *out_node_id)
|
||||
{ return add_node(graph, XTC_NODE_METER, 0.0f, out_node_id); }
|
||||
xtc_result xtc_graph_add_audio(xtc_graph *graph, size_t *out_node_id)
|
||||
{ return add_node(graph, XTC_NODE_AUDIO, 0.0f, out_node_id); }
|
||||
xtc_result xtc_graph_add_output(xtc_graph *graph, size_t *out_node_id)
|
||||
{ return add_node(graph, XTC_NODE_OUTPUT, 0.0f, out_node_id); }
|
||||
|
||||
|
|
@ -70,7 +72,7 @@ xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
|
|||
node_id = node_index(graph, node_id);
|
||||
if (node_id == SIZE_MAX) return XTC_ERR_NOT_FOUND;
|
||||
type = graph->nodes[node_id].type;
|
||||
if (port_id > 1U || (port_id == 0U && type == XTC_NODE_OSCILLATOR) ||
|
||||
if (port_id > 1U || (port_id == 0U && (type == XTC_NODE_OSCILLATOR || type == XTC_NODE_AUDIO)) ||
|
||||
(port_id == 1U && type == XTC_NODE_OUTPUT)) return XTC_ERR_NOT_FOUND;
|
||||
out_port->direction = port_id == 0U ? XTC_PORT_INPUT : XTC_PORT_OUTPUT;
|
||||
out_port->signal_type = XTC_SIGNAL_AUDIO;
|
||||
|
|
@ -226,6 +228,7 @@ xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *e
|
|||
case XTC_NODE_GAIN: result = xtc_graph_add_gain(graph, edit->data.add.value, &added_id); break;
|
||||
case XTC_NODE_OSCILLATOR: result = xtc_graph_add_oscillator(graph, edit->data.add.value, &added_id); break;
|
||||
case XTC_NODE_METER: result = xtc_graph_add_meter(graph, &added_id); break;
|
||||
case XTC_NODE_AUDIO: result = xtc_graph_add_audio(graph, &added_id); break;
|
||||
case XTC_NODE_OUTPUT: result = xtc_graph_add_output(graph, &added_id); break;
|
||||
default: result = XTC_ERR_INVALID_ARGUMENT; break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ const char *node_name(xtc_node_type type)
|
|||
case XTC_NODE_OSCILLATOR: return "Oscillator";
|
||||
case XTC_NODE_GAIN: return "Gain";
|
||||
case XTC_NODE_METER: return "Meter";
|
||||
case XTC_NODE_AUDIO: return "Audio clips";
|
||||
case XTC_NODE_OUTPUT: return "Output";
|
||||
}
|
||||
return "Unknown";
|
||||
|
|
@ -186,7 +187,7 @@ void GraphWidget::draw()
|
|||
fl_font(FL_HELVETICA, 12);
|
||||
if (node.type == XTC_NODE_OSCILLATOR) std::snprintf(text, sizeof(text), "%.0f Hz", static_cast<double>(node.value));
|
||||
else if (node.type == XTC_NODE_GAIN) std::snprintf(text, sizeof(text), "x %.2f", static_cast<double>(node.value));
|
||||
else std::snprintf(text, sizeof(text), "%s", node.type == XTC_NODE_METER ? "Peak pass-through" : "Mono audio output");
|
||||
else std::snprintf(text, sizeof(text), "%s", node.type == XTC_NODE_METER ? "Peak pass-through" : node.type == XTC_NODE_AUDIO ? "Timeline mix source" : "Mono audio output");
|
||||
fl_draw(text, left + 10, top + 37);
|
||||
for (size_t port = 0U; port < 2U; ++port) {
|
||||
int px, py;
|
||||
|
|
|
|||
|
|
@ -38,6 +38,9 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
menu->add("File/Open", FL_CTRL + 'o', file_callback, this);
|
||||
menu->add("File/Save", FL_CTRL + 's', file_callback, this);
|
||||
menu->add("File/Save As", FL_CTRL + FL_SHIFT + 's', file_callback, this);
|
||||
menu->add("File/Import WAV", FL_CTRL + 'i', audio_file_callback, this);
|
||||
menu->add("File/Relink selected audio", 0, audio_file_callback, this);
|
||||
menu->add("Audio/Record WAV", FL_SHIFT + FL_F + 5, audio_file_callback, this);
|
||||
menu->add("File/Quit", FL_CTRL + 'q', close_callback, this);
|
||||
menu->add("Audio/Start engine", FL_F + 5, play_callback, this);
|
||||
menu->add("Audio/Stop engine", FL_F + 8, stop_callback, this);
|
||||
|
|
@ -45,18 +48,21 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
|
||||
play_ = new Fl_Button(12, 40, 80, 30, "Play");
|
||||
play_->callback(play_callback, this);
|
||||
play_->tooltip("Start the audio engine (F5 or Space). Regions are silent until audio/MIDI phases.");
|
||||
play_->tooltip("Start the audio engine (F5 or Space). Play audio clips and the modular graph.");
|
||||
stop_ = new Fl_Button(100, 40, 80, 30, "Stop");
|
||||
stop_->callback(stop_callback, this);
|
||||
stop_->tooltip("Stop the audio engine (F8 or Space).");
|
||||
auto *reconnect = new Fl_Button(188, 40, 110, 30, "Reconnect");
|
||||
reconnect->callback(reconnect_callback, this);
|
||||
reconnect->tooltip("Close and reopen JACK, leaving the engine stopped (F6).");
|
||||
indicator_ = new Fl_Box(316, 40, 672, 30, "Audio: disconnected");
|
||||
record_ = new Fl_Button(306, 40, 90, 30, "Record");
|
||||
record_->callback(audio_file_callback, this);
|
||||
record_->tooltip("Record JACK input to WAV; choose path, then Stop to finalize and add the take.");
|
||||
indicator_ = new Fl_Box(404, 40, 584, 30, "Audio: disconnected");
|
||||
indicator_->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
|
||||
indicator_->labelfont(FL_BOLD);
|
||||
auto *hint = new Fl_Box(12, 78, 976, 24,
|
||||
"Edit signal flow in Modular Graph. No hardware ports are connected automatically; timeline regions are silent; recording is a future phase.");
|
||||
"Edit signal flow in Modular Graph. No hardware ports are connected automatically; Import WAV clips or record JACK input; connect external ports explicitly.");
|
||||
hint->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
|
||||
hint->labelsize(12);
|
||||
|
||||
|
|
@ -70,11 +76,12 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
auto *zoom_in = new Fl_Button(384, 150, 80, 26, "Zoom +");
|
||||
auto *zoom_out = new Fl_Button(472, 150, 80, 26, "Zoom -");
|
||||
auto *timeline_hint = new Fl_Box(20, 548, 956, 28,
|
||||
"Silent regions. Drag body to move, edges to trim; ruler seeks while stopped.");
|
||||
"Import WAV (Ctrl+I) or Record. Drag body to move, edges to trim; ruler seeks while stopped.");
|
||||
timeline_hint->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE); timeline_hint->labelsize(12);
|
||||
auto *timeline_scroll = new Fl_Scroll(20, 188, 956, 350);
|
||||
sequencer_ = new SequencerWidget(20, 188, project_.get(), seek, this);
|
||||
timeline_scroll->end();
|
||||
sequencer_->before_edit = before_arrangement_edit;
|
||||
sequencer_->callback([](Fl_Widget *, void *c) { static_cast<MainWindow *>(c)->mark_dirty(); }, this);
|
||||
add_track->callback([](Fl_Widget *, void *c) { static_cast<SequencerWidget *>(c)->add_track(); }, sequencer_);
|
||||
add_region->callback([](Fl_Widget *, void *c) { static_cast<SequencerWidget *>(c)->add_region(); }, sequencer_);
|
||||
|
|
@ -115,6 +122,10 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
output->callback([](Fl_Widget *, void *context) {
|
||||
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_OUTPUT);
|
||||
}, graph_widget_);
|
||||
auto *audio = new Fl_Button(650, 150, 130, 26, "Add audio source");
|
||||
audio->callback([](Fl_Widget *, void *context) {
|
||||
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_AUDIO);
|
||||
}, graph_widget_);
|
||||
auto *remove = new Fl_Button(512, 150, 130, 26, "Delete selection");
|
||||
remove->callback([](Fl_Widget *, void *context) {
|
||||
(void)static_cast<GraphWidget *>(context)->delete_selected();
|
||||
|
|
@ -150,7 +161,11 @@ MainWindow::~MainWindow()
|
|||
// covers exceptional event-loop exit. A failed close must retain its C
|
||||
// callback context until process exit, rather than free active audio state.
|
||||
if (backend_ != nullptr && xtc_backend_close(backend_) != XTC_OK)
|
||||
std::fputs("Failed to close JACK during GUI destruction; backend retained until process exit.\n", stderr);
|
||||
std::fputs("Failed to close JACK during GUI destruction; backend and recording retained until process exit.\n", stderr);
|
||||
else if (recorder_) {
|
||||
(void)xtc_recorder_finish(recorder_);
|
||||
xtc_recorder_destroy(recorder_);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::report_error(const char *operation, xtc_result result)
|
||||
|
|
@ -166,6 +181,7 @@ void MainWindow::report_error(const char *operation, xtc_result result)
|
|||
xtc_result MainWindow::edit_graph(void *context, const xtc_graph_edit &edit, size_t *out_added_id)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
if (window->recorder_) return XTC_ERR_INVALID_STATE;
|
||||
xtc_graph *candidate = nullptr;
|
||||
size_t added_id = SIZE_MAX;
|
||||
xtc_result result = xtc_graph_apply_edit(xtc_project_graph(window->project_.get()), &edit, &candidate, &added_id);
|
||||
|
|
@ -210,9 +226,17 @@ void MainWindow::start_engine()
|
|||
return;
|
||||
}
|
||||
if (running_) return;
|
||||
xtc_result result = xtc_backend_start(backend_);
|
||||
xtc_result result = xtc_backend_prepare_project(backend_, project_.get());
|
||||
if (result == XTC_OK) result = xtc_backend_start(backend_);
|
||||
if (result != XTC_OK) report_error("Start engine failed", result);
|
||||
else set_label(error_, "");
|
||||
else {
|
||||
set_label(error_, "");
|
||||
const char *issue = xtc_backend_asset_issue(backend_);
|
||||
if (issue[0]) {
|
||||
char message[640]; std::snprintf(message, sizeof(message), "Missing/unreadable audio (silent): %s. Select clip and Relink to repair.", issue);
|
||||
set_label(error_, message);
|
||||
}
|
||||
}
|
||||
refresh_status();
|
||||
}
|
||||
|
||||
|
|
@ -221,12 +245,16 @@ void MainWindow::stop_engine()
|
|||
if (!ready_) return;
|
||||
xtc_result result = xtc_backend_stop(backend_);
|
||||
if (result != XTC_OK) report_error("Stop engine failed", result);
|
||||
else set_label(error_, "");
|
||||
else {
|
||||
set_label(error_, "");
|
||||
if (recorder_) finish_recording();
|
||||
}
|
||||
refresh_status();
|
||||
}
|
||||
|
||||
void MainWindow::reconnect_engine()
|
||||
{
|
||||
if (recorder_) { report_error("Stop recording before reconnecting", XTC_ERR_INVALID_STATE); return; }
|
||||
xtc_result result = xtc_backend_close(backend_);
|
||||
if (result != XTC_OK) {
|
||||
report_error("Reconnect could not close JACK", result);
|
||||
|
|
@ -240,7 +268,7 @@ void MainWindow::reconnect_engine()
|
|||
|
||||
void MainWindow::refresh_status()
|
||||
{
|
||||
bool can_play = false;
|
||||
bool can_play = false, can_record = false;
|
||||
if (!ready_) {
|
||||
set_label(indicator_, "Audio: disconnected");
|
||||
set_label(status_, "JACK: not connected — use Reconnect after starting the server");
|
||||
|
|
@ -262,17 +290,18 @@ void MainWindow::refresh_status()
|
|||
running_ = status.running != 0;
|
||||
const bool fault = status.server_lost || status.sample_rate_changed || status.process_result != XTC_OK;
|
||||
can_play = !running_ && !fault;
|
||||
can_record = !fault && recorder_ == nullptr;
|
||||
const char *state = status.server_lost ? "Audio: server lost — Reconnect required" :
|
||||
status.sample_rate_changed ? "Audio: sample rate changed — Reconnect required" :
|
||||
status.process_result != XTC_OK ? "Audio: processing error" :
|
||||
running_ ? "Audio: running — modular graph" : "Audio: ready / stopped";
|
||||
recorder_ ? "Audio: recording JACK input — Stop to finish" : running_ ? "Audio: running — modular graph" : "Audio: ready / stopped";
|
||||
set_label(indicator_, state);
|
||||
indicator_->labelcolor(fault ? fl_rgb_color(255, 155, 120) :
|
||||
running_ ? fl_rgb_color(125, 225, 160) : fl_rgb_color(215, 215, 215));
|
||||
char text[256];
|
||||
std::snprintf(text, sizeof(text), "JACK | %u Hz | %u frames | mono | %u blocks | %u xruns | %s",
|
||||
std::snprintf(text, sizeof(text), "JACK | %u Hz | %u frames | mono | %u blocks | %u xruns | %s | %u asset issues",
|
||||
status.sample_rate, status.buffer_frames, status.processed_blocks, status.xruns,
|
||||
xtc_result_string(status.process_result));
|
||||
xtc_result_string(status.process_result), status.asset_issues);
|
||||
set_label(status_, text);
|
||||
if (fault) set_label(graph_state_, "Graph: engine unavailable — Reconnect required");
|
||||
else if (status.submitted_revision != status.active_revision)
|
||||
|
|
@ -283,11 +312,13 @@ void MainWindow::refresh_status()
|
|||
xtc_backend_collect(backend_);
|
||||
}
|
||||
if (can_play) play_->activate(); else play_->deactivate();
|
||||
if (ready_ && running_) stop_->activate(); else stop_->deactivate();
|
||||
if (can_record) record_->activate(); else record_->deactivate();
|
||||
if (ready_ && (running_ || recorder_)) stop_->activate(); else stop_->deactivate();
|
||||
}
|
||||
|
||||
bool MainWindow::close_window()
|
||||
{
|
||||
if (recorder_) { report_error("Stop recording before closing", XTC_ERR_INVALID_STATE); return false; }
|
||||
if (!discard_changes()) return false;
|
||||
xtc_result result = xtc_backend_close(backend_);
|
||||
if (result != XTC_OK) {
|
||||
|
|
@ -328,12 +359,19 @@ void MainWindow::view_callback(Fl_Widget *widget, void *view)
|
|||
}
|
||||
void MainWindow::about_callback(Fl_Widget *, void *)
|
||||
{
|
||||
fl_message("%s %s\nModular audio workstation\nPhase 5: projects and silent arrangement regions; audio/MIDI content follows.",
|
||||
fl_message("%s %s\nModular audio workstation\nPhase 6: WAV import, graph clip playback and bounded recording; MIDI follows.",
|
||||
XTC_PROJECT_NAME, XTC_VERSION_STRING);
|
||||
}
|
||||
void MainWindow::timer_callback(void *context)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
if (window->recorder_) {
|
||||
xtc_result r = xtc_recorder_drain(window->recorder_);
|
||||
xtc_backend_status status{};
|
||||
xtc_backend_get_status(window->backend_, &status);
|
||||
if (r != XTC_OK || status.server_lost || status.sample_rate_changed || status.process_result != XTC_OK)
|
||||
window->stop_engine();
|
||||
}
|
||||
window->refresh_status();
|
||||
if (!window->closing_) Fl::repeat_timeout(0.1, timer_callback, context);
|
||||
}
|
||||
|
|
@ -358,6 +396,7 @@ xtc_result MainWindow::seek(void *context, uint64_t tick)
|
|||
}
|
||||
xtc_result MainWindow::save_project(const char *path)
|
||||
{
|
||||
if (recorder_) return XTC_ERR_INVALID_STATE;
|
||||
if (path == nullptr) return XTC_ERR_INVALID_ARGUMENT;
|
||||
std::string saved_path(path);
|
||||
xtc_result r = xtc_project_save(project_.get(), path);
|
||||
|
|
@ -366,18 +405,31 @@ xtc_result MainWindow::save_project(const char *path)
|
|||
}
|
||||
xtc_result MainWindow::install_project(xtc_project *candidate)
|
||||
{
|
||||
if (recorder_) return XTC_ERR_INVALID_STATE;
|
||||
xtc_result r = XTC_OK;
|
||||
if (ready_) {
|
||||
r = xtc_backend_stop(backend_);
|
||||
if (r == XTC_OK) r = xtc_backend_seek(backend_, 0);
|
||||
if (r == XTC_OK) r = xtc_backend_submit(backend_, xtc_project_graph(candidate));
|
||||
if (r == XTC_OK) r = xtc_backend_prepare_project(backend_, candidate);
|
||||
}
|
||||
xtc_audio_plan *offline = nullptr;
|
||||
if (r == XTC_OK && !ready_) r = xtc_audio_plan_create(candidate, 48000, &offline);
|
||||
if (r != XTC_OK) return r;
|
||||
std::string issue = ready_ ? xtc_backend_asset_issue(backend_) : "";
|
||||
if (offline) {
|
||||
const char *path = "";
|
||||
xtc_audio_plan_issues(offline, &path); issue = path;
|
||||
xtc_audio_plan_destroy(offline);
|
||||
}
|
||||
project_.reset(candidate);
|
||||
graph_widget_->reset_layout();
|
||||
graph_widget_->set_graph(xtc_project_graph(candidate));
|
||||
sequencer_->set_project(candidate);
|
||||
dirty_ = false;
|
||||
if (!issue.empty()) {
|
||||
char text[640]; std::snprintf(text, sizeof(text), "Missing/unreadable audio (silent): %s. Select clip and Relink to repair.", issue.c_str());
|
||||
set_label(error_, text);
|
||||
} else set_label(error_, "");
|
||||
refresh_status();
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -396,6 +448,7 @@ xtc_result MainWindow::open_project(const char *path)
|
|||
void MainWindow::file_callback(Fl_Widget *widget, void *context)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
if (window->recorder_) { window->report_error("Stop recording before file operations", XTC_ERR_INVALID_STATE); return; }
|
||||
auto *menu = static_cast<Fl_Menu_Bar *>(widget);
|
||||
const char *action = menu->mvalue()->label();
|
||||
xtc_result r = XTC_OK;
|
||||
|
|
@ -425,3 +478,102 @@ void MainWindow::file_callback(Fl_Widget *widget, void *context)
|
|||
}
|
||||
if (r != XTC_OK) window->report_error("Project operation failed", r);
|
||||
}
|
||||
|
||||
|
||||
xtc_result MainWindow::before_arrangement_edit(void *context)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
if (window->recorder_) return XTC_ERR_INVALID_STATE;
|
||||
if (!window->ready_) return XTC_OK;
|
||||
xtc_result r = xtc_backend_stop(window->backend_);
|
||||
window->refresh_status();
|
||||
return r;
|
||||
}
|
||||
xtc_result MainWindow::import_audio(const char *path)
|
||||
{
|
||||
xtc_result r = before_arrangement_edit(this);
|
||||
if (r != XTC_OK) return r;
|
||||
xtc_project *candidate = nullptr;
|
||||
r = xtc_project_clone(project_.get(), &candidate);
|
||||
uint64_t track = sequencer_->selected_track(), clip = 0;
|
||||
if (r == XTC_OK && track == UINT64_MAX) r = xtc_project_add_track(candidate, "Audio", &track);
|
||||
if (r == XTC_OK) r = xtc_project_import_audio(candidate, track, path, sequencer_->position(), &clip);
|
||||
if (r == XTC_OK && ready_) r = xtc_backend_prepare_project(backend_, candidate);
|
||||
if (r != XTC_OK) { xtc_project_destroy(candidate); return r; }
|
||||
project_.reset(candidate);
|
||||
sequencer_->set_project(candidate, false);
|
||||
graph_widget_->set_graph(xtc_project_graph(candidate));
|
||||
mark_dirty(); refresh_status(); return XTC_OK;
|
||||
}
|
||||
xtc_result MainWindow::record_audio(const char *path)
|
||||
{
|
||||
if (!ready_ || recorder_) return XTC_ERR_INVALID_STATE;
|
||||
xtc_result r = xtc_backend_stop(backend_);
|
||||
xtc_backend_status status{};
|
||||
if (r == XTC_OK) r = xtc_backend_get_status(backend_, &status);
|
||||
if (r == XTC_OK) r = xtc_project_sample_to_tick(project_.get(), status.sample_position, status.sample_rate, &recording_tick_);
|
||||
if (r == XTC_OK) r = xtc_backend_prepare_project(backend_, project_.get());
|
||||
if (r == XTC_OK) r = xtc_recorder_create(path, status.sample_rate, static_cast<size_t>(status.sample_rate) * 2, &recorder_);
|
||||
if (r != XTC_OK) return r;
|
||||
recording_path_ = path;
|
||||
recording_track_ = sequencer_->selected_track();
|
||||
r = xtc_backend_set_recorder(backend_, recorder_);
|
||||
if (r == XTC_OK) r = xtc_backend_start(backend_);
|
||||
if (r != XTC_OK) {
|
||||
(void)xtc_backend_set_recorder(backend_, nullptr);
|
||||
xtc_recorder_destroy(recorder_); recorder_ = nullptr;
|
||||
} else mark_dirty();
|
||||
refresh_status(); return r;
|
||||
}
|
||||
void MainWindow::finish_recording()
|
||||
{
|
||||
// Called only after successful backend_stop: no producer can reference ring.
|
||||
(void)xtc_backend_set_recorder(backend_, nullptr);
|
||||
uint64_t frames = 0;
|
||||
(void)xtc_recorder_status(recorder_, &frames);
|
||||
xtc_result r = xtc_recorder_finish(recorder_);
|
||||
xtc_recorder_destroy(recorder_); recorder_ = nullptr;
|
||||
if (r != XTC_OK) {
|
||||
char text[768]; std::snprintf(text, sizeof(text), "Recording failed (%s). Partial take, if finalized: %s. It was not added to the project.",
|
||||
xtc_result_string(r), recording_path_.c_str());
|
||||
set_label(error_, text); return;
|
||||
}
|
||||
if (!frames) { set_label(error_, "Empty recording saved; no clip added."); return; }
|
||||
xtc_project *candidate = nullptr;
|
||||
r = xtc_project_clone(project_.get(), &candidate);
|
||||
uint64_t clip = 0, track = recording_track_;
|
||||
if (r == XTC_OK && track == UINT64_MAX) r = xtc_project_add_track(candidate, "Recording", &track);
|
||||
if (r == XTC_OK) r = xtc_project_import_audio(candidate, track, recording_path_.c_str(), recording_tick_, &clip);
|
||||
if (r == XTC_OK) r = xtc_backend_prepare_project(backend_, candidate);
|
||||
if (r != XTC_OK) {
|
||||
xtc_project_destroy(candidate);
|
||||
char text[768]; std::snprintf(text, sizeof(text), "Take saved to %s; adding clip failed: %s. Import the WAV to recover.",
|
||||
recording_path_.c_str(), xtc_result_string(r));
|
||||
set_label(error_, text); return;
|
||||
}
|
||||
project_.reset(candidate); sequencer_->set_project(candidate, false);
|
||||
graph_widget_->set_graph(xtc_project_graph(candidate)); mark_dirty();
|
||||
}
|
||||
void MainWindow::audio_file_callback(Fl_Widget *widget, void *context)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
auto *menu = dynamic_cast<Fl_Menu_Bar *>(widget);
|
||||
const char *action = menu ? menu->mvalue()->label() : "Record WAV";
|
||||
bool recording = std::strcmp(action, "Record WAV") == 0;
|
||||
bool relink = std::strcmp(action, "Relink selected audio") == 0;
|
||||
if (window->recorder_) { window->report_error("Stop recording first", XTC_ERR_INVALID_STATE); return; }
|
||||
Fl_Native_File_Chooser chooser;
|
||||
chooser.title(recording ? "Record JACK input to WAV" : relink ? "Relink selected clip" : "Import WAV");
|
||||
chooser.type(recording ? Fl_Native_File_Chooser::BROWSE_SAVE_FILE : Fl_Native_File_Chooser::BROWSE_FILE);
|
||||
chooser.filter("WAV audio\t*.wav");
|
||||
if (recording) chooser.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM);
|
||||
int chosen = chooser.show();
|
||||
if (chosen == 1) return;
|
||||
xtc_result r = chosen < 0 ? XTC_ERR_IO : XTC_OK;
|
||||
if (r == XTC_OK && relink) {
|
||||
r = before_arrangement_edit(window);
|
||||
if (r == XTC_OK) r = xtc_project_set_clip_asset(window->project_.get(), window->sequencer_->selected_clip(), chooser.filename());
|
||||
if (r == XTC_OK) { window->mark_dirty(); window->sequencer_->redraw(); }
|
||||
} else if (r == XTC_OK) r = recording ? window->record_audio(chooser.filename()) : window->import_audio(chooser.filename());
|
||||
if (r != XTC_OK) window->report_error("Project audio operation failed", r);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,11 +22,19 @@ public:
|
|||
using Fl_Double_Window::show;
|
||||
int handle(int event) override;
|
||||
|
||||
xtc_result import_audio(const char *path);
|
||||
xtc_result record_audio(const char *path);
|
||||
xtc_result save_project(const char *path);
|
||||
xtc_result open_project(const char *path);
|
||||
private:
|
||||
std::unique_ptr<xtc_project, decltype(&xtc_project_destroy)> project_{nullptr, xtc_project_destroy};
|
||||
SequencerWidget *sequencer_ = nullptr;
|
||||
xtc_recorder *recorder_ = nullptr;
|
||||
std::string recording_path_;
|
||||
uint64_t recording_tick_ = 0, recording_track_ = UINT64_MAX;
|
||||
void finish_recording();
|
||||
static xtc_result before_arrangement_edit(void *context);
|
||||
static void audio_file_callback(Fl_Widget *, void *context);
|
||||
bool dirty_ = false, prompt_unsaved_ = true;
|
||||
std::string path_;
|
||||
bool discard_changes();
|
||||
|
|
@ -40,6 +48,7 @@ private:
|
|||
bool ready_ = false;
|
||||
bool running_ = false;
|
||||
bool closing_ = false;
|
||||
Fl_Button *record_ = nullptr;
|
||||
Fl_Button *play_ = nullptr;
|
||||
Fl_Button *stop_ = nullptr;
|
||||
Fl_Box *indicator_ = nullptr;
|
||||
|
|
|
|||
|
|
@ -39,11 +39,13 @@ void SequencerWidget::changed(xtc_result result)
|
|||
}
|
||||
xtc_result SequencerWidget::add_track()
|
||||
{
|
||||
if (before_edit) { xtc_result r = before_edit(context_); if (r != XTC_OK) { changed(r); return r; } }
|
||||
uint64_t id; xtc_result r = xtc_project_add_track(project_, "Track", &id);
|
||||
if (r == XTC_OK) { track_ = id; clip_ = UINT64_MAX; } changed(r); return r;
|
||||
}
|
||||
xtc_result SequencerWidget::add_region()
|
||||
{
|
||||
if (before_edit) { xtc_result r = before_edit(context_); if (r != XTC_OK) { changed(r); return r; } }
|
||||
size_t tracks, clips; xtc_project_get_counts(project_, &tracks, &clips);
|
||||
if (track_ == UINT64_MAX && tracks) {
|
||||
xtc_track_info t{}; xtc_project_get_track(project_, 0, &t); track_ = t.id;
|
||||
|
|
@ -55,6 +57,7 @@ xtc_result SequencerWidget::add_region()
|
|||
}
|
||||
xtc_result SequencerWidget::delete_selection()
|
||||
{
|
||||
if (before_edit) { xtc_result r = before_edit(context_); if (r != XTC_OK) { changed(r); return r; } }
|
||||
xtc_result r = clip_ != UINT64_MAX ? xtc_project_remove_clip(project_, clip_) :
|
||||
xtc_project_remove_track(project_, track_);
|
||||
if (r == XTC_OK) clip_ = track_ = UINT64_MAX;
|
||||
|
|
@ -96,7 +99,7 @@ void SequencerWidget::draw()
|
|||
fl_color(c.id == clip_ ? fl_rgb_color(100, 170, 205) : fl_rgb_color(65, 120, 150));
|
||||
fl_rectf(a, py + 5, b - a, 44);
|
||||
fl_push_clip(a, py + 5, b - a, 44); fl_color(FL_WHITE);
|
||||
fl_draw(c.name, std::max(a, left + 6), py + 31); fl_pop_clip();
|
||||
fl_draw(c.asset_path[0] ? "Audio" : c.name, std::max(a, left + 6), py + 31); fl_pop_clip();
|
||||
}
|
||||
}
|
||||
int playhead = x() + 120 + static_cast<int>(cursor_ * static_cast<unsigned int>(scale_) / 960U);
|
||||
|
|
@ -148,6 +151,7 @@ int SequencerWidget::handle(int event)
|
|||
} else if (mode == 2) { start += delta; offset += delta; length -= delta; }
|
||||
else length += delta;
|
||||
if (start < 0 || offset < 0 || length <= 0) return 1;
|
||||
if (before_edit) { xtc_result r = before_edit(context_); if (r != XTC_OK) { changed(r); return 1; } }
|
||||
changed(xtc_project_edit_clip(project_, clip_, target, static_cast<uint64_t>(start),
|
||||
static_cast<uint64_t>(length), static_cast<uint64_t>(offset)));
|
||||
return 1;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,10 @@
|
|||
#include "xtc/xtc_project.h"
|
||||
class SequencerWidget : public Fl_Widget {
|
||||
public:
|
||||
using BeforeEdit = xtc_result (*)(void *);
|
||||
BeforeEdit before_edit = nullptr;
|
||||
uint64_t selected_track() const { return track_; }
|
||||
uint64_t selected_clip() const { return clip_; }
|
||||
using Seek = xtc_result (*)(void *, uint64_t);
|
||||
SequencerWidget(int x, int y, xtc_project *project, Seek seek, void *context);
|
||||
void set_project(xtc_project *project, bool reset = true);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Persistence
|
||||
|
||||
project_file.c implements bounded version-1 project metadata loading and atomic
|
||||
project_file.c implements bounded version-2 project metadata loading (including explicit version-1 migration) and atomic
|
||||
temporary-file/fsync/rename saving in C. Loading validates a candidate before
|
||||
replacing the existing model. All I/O is control-thread only.
|
||||
See docs/PROJECT_FORMAT.md for the schema, limits and durability guarantees.
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct { FILE *file; size_t bytes; char line[512]; char *tokens[12]; size_t count; } reader;
|
||||
typedef struct { FILE *file; size_t bytes; char line[1536]; char *tokens[12]; size_t count; } reader;
|
||||
static xtc_result line(reader *input)
|
||||
{
|
||||
size_t used = 0U;
|
||||
|
|
@ -19,7 +19,7 @@ static xtc_result line(reader *input)
|
|||
char *cursor;
|
||||
input->count = 0U;
|
||||
while ((byte = fgetc(input->file)) != EOF) {
|
||||
if (++input->bytes > 1048576U) return XTC_ERR_FORMAT;
|
||||
if (++input->bytes > 2097152U) return XTC_ERR_FORMAT;
|
||||
if (byte == '\n') break;
|
||||
if (byte < 32 || byte > 126 || used == sizeof(input->line) - 1U) return XTC_ERR_FORMAT;
|
||||
input->line[used++] = (char)byte;
|
||||
|
|
@ -85,11 +85,12 @@ static int decode_float(const char *text, float *value)
|
|||
#define NUM(index, variable) do { if (!number(input->tokens[(index)], &(variable))) return XTC_ERR_FORMAT; } while (0)
|
||||
static xtc_result read_project(reader *input, xtc_project *project)
|
||||
{
|
||||
uint64_t version, value, next_id, node_count, edge_count;
|
||||
uint64_t version, value, next_id, node_count, edge_count, format_version;
|
||||
size_t i, j;
|
||||
xtc_graph *graph = project->graph;
|
||||
READ("XTC_PROJECT", 2U); NUM(1U, version);
|
||||
if (version != 1U) return XTC_ERR_UNSUPPORTED;
|
||||
if (version != 1U && version != 2U) return XTC_ERR_UNSUPPORTED;
|
||||
format_version = version;
|
||||
READ("APP", 4U);
|
||||
for (i = 1U; i < 4U; ++i) { NUM(i, value); if (value > UINT_MAX) return XTC_ERR_FORMAT; }
|
||||
READ("TEMPO", 2U); NUM(1U, value);
|
||||
|
|
@ -103,11 +104,11 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
for (i = 0U; i < (size_t)node_count; ++i) {
|
||||
xtc_node node;
|
||||
READ("NODE", 4U); NUM(1U, value); NUM(2U, version);
|
||||
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN || version > XTC_NODE_OUTPUT ||
|
||||
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN || version > (format_version == 1U ? XTC_NODE_OUTPUT : XTC_NODE_AUDIO) ||
|
||||
!decode_float(input->tokens[3], &node.value)) return XTC_ERR_FORMAT;
|
||||
node.id = (size_t)value; node.type = (xtc_node_type)version;
|
||||
if (node.type == XTC_NODE_OSCILLATOR && node.value < 0.0f) return XTC_ERR_FORMAT;
|
||||
if ((node.type == XTC_NODE_METER || node.type == XTC_NODE_OUTPUT) && node.value != 0.0f) return XTC_ERR_FORMAT;
|
||||
if ((node.type == XTC_NODE_METER || node.type == XTC_NODE_OUTPUT || node.type == XTC_NODE_AUDIO) && node.value != 0.0f) return XTC_ERR_FORMAT;
|
||||
for (j = 0U; j < i; ++j) if (graph->nodes[j].id == node.id) return XTC_ERR_FORMAT;
|
||||
graph->nodes[graph->node_count++] = node;
|
||||
}
|
||||
|
|
@ -133,6 +134,19 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
NUM(1U, clip->id); NUM(2U, clip->track_id); NUM(3U, clip->start_tick);
|
||||
NUM(4U, clip->length_tick); NUM(5U, clip->offset_tick); NUM(6U, clip->source_length_tick);
|
||||
if (!decode_name(input->tokens[7], clip->name)) return XTC_ERR_FORMAT;
|
||||
if (format_version == 2U) {
|
||||
size_t k, length;
|
||||
READ("ASSET", 2U);
|
||||
if (!strcmp(input->tokens[1], "-")) continue;
|
||||
length = strlen(input->tokens[1]);
|
||||
if (!length || length % 2 || length >= XTC_ASSET_PATH_BYTES * 2U) return XTC_ERR_FORMAT;
|
||||
for (k = 0; k < length / 2; ++k) {
|
||||
int high = hex_digit(input->tokens[1][k * 2]), low = hex_digit(input->tokens[1][k * 2 + 1]);
|
||||
if (high < 0 || low < 0 || (high == 0 && low == 0)) return XTC_ERR_FORMAT;
|
||||
clip->asset_path[k] = (char)(high * 16 + low);
|
||||
}
|
||||
clip->asset_path[length / 2] = 0;
|
||||
}
|
||||
}
|
||||
READ("END", 1U);
|
||||
if (fgetc(input->file) != EOF) return XTC_ERR_FORMAT;
|
||||
|
|
@ -150,7 +164,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
const xtc_graph *graph = project->graph;
|
||||
size_t i;
|
||||
if (sizeof(float) != sizeof(uint32_t) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) return XTC_ERR_UNSUPPORTED;
|
||||
if (fprintf(file, "XTC_PROJECT 1\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
|
||||
if (fprintf(file, "XTC_PROJECT 2\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
|
||||
XTC_VERSION_MAJOR, XTC_VERSION_MINOR, XTC_VERSION_PATCH, project->tempo,
|
||||
project->next_track_id, project->next_clip_id, graph->require_output_nodes,
|
||||
graph->next_id, graph->node_count, graph->edge_count) < 0) return XTC_ERR_IO;
|
||||
|
|
@ -172,6 +186,10 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
if (fprintf(file, "CLIP %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " ",
|
||||
clip->id, clip->track_id, clip->start_tick, clip->length_tick, clip->offset_tick, clip->source_length_tick) < 0 ||
|
||||
!write_name(file, clip->name)) return XTC_ERR_IO;
|
||||
if (fputs("ASSET ", file) == EOF) return XTC_ERR_IO;
|
||||
if (clip->asset_path[0]) {
|
||||
if (!write_name(file, clip->asset_path)) return XTC_ERR_IO;
|
||||
} else if (fputs("-\n", file) == EOF) return XTC_ERR_IO;
|
||||
}
|
||||
return fputs("END\n", file) == EOF ? XTC_ERR_IO : XTC_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Project model
|
||||
|
||||
project.c owns the opaque C project: graph, bounded tracks, silent arrangement
|
||||
regions, tempo and stable ID allocation. The GUI borrows handles and edits through
|
||||
project.c owns the opaque C project: graph, bounded tracks, silent/audio arrangement
|
||||
regions and external asset references, tempo and stable ID allocation. The GUI borrows handles and edits through
|
||||
xtc_project.h. Tick/sample conversion and source-preserving trim validation live
|
||||
here; no FLTK, file I/O or callback scheduling belongs in this module.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "project_internal.h"
|
||||
#include "xtc/xtc_audio.h"
|
||||
#include "../graph/graph_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -214,9 +215,29 @@ xtc_result xtc_project_validate(const xtc_project *project)
|
|||
}
|
||||
for (i = 0U; i < project->clip_count; ++i) {
|
||||
const xtc_clip_info *clip = project->clips + i;
|
||||
size_t path_length = 0U;
|
||||
while (path_length < XTC_ASSET_PATH_BYTES && clip->asset_path[path_length]) {
|
||||
unsigned char byte = (unsigned char)clip->asset_path[path_length++];
|
||||
if (byte < 32U || byte == 127U) return XTC_ERR_FORMAT;
|
||||
}
|
||||
if (path_length == XTC_ASSET_PATH_BYTES || (path_length && clip->asset_path[0] != '/')) return XTC_ERR_FORMAT;
|
||||
if (clip->id >= project->next_clip_id || track_index(project, clip->track_id) == SIZE_MAX ||
|
||||
!xtc_project_valid_name(clip->name) || !valid_range(clip->start_tick, clip->length_tick, clip->offset_tick, clip->source_length_tick)) return XTC_ERR_FORMAT;
|
||||
for (j = 0U; j < i; ++j) if (project->clips[j].id == clip->id) return XTC_ERR_FORMAT;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_project_set_clip_asset(xtc_project *project, uint64_t id, const char *path)
|
||||
{
|
||||
size_t index, length = 0U, i;
|
||||
if (!project || !path) return XTC_ERR_INVALID_ARGUMENT;
|
||||
while (length < XTC_ASSET_PATH_BYTES && path[length]) ++length;
|
||||
if (length == XTC_ASSET_PATH_BYTES || (length && path[0] != '/')) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < length; ++i)
|
||||
if ((unsigned char)path[i] < 32U || path[i] == 127) return XTC_ERR_INVALID_ARGUMENT;
|
||||
index = clip_index(project, id);
|
||||
if (index == SIZE_MAX) return XTC_ERR_NOT_FOUND;
|
||||
memmove(project->clips[index].asset_path, path, length + 1U);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue