Phase 8: Add stereo track mixer, automation and undo/redo
This commit is contained in:
parent
cacc086805
commit
7db5fb7738
45 changed files with 1611 additions and 130 deletions
|
|
@ -2,9 +2,12 @@
|
|||
|
||||
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.
|
||||
passes the mono or paired stereo 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.
|
||||
|
||||
Phase 8 track gain/pan/mute/solo and automation are prepared with each region;
|
||||
see ../../docs/MIXER.md. Raw recording remains mono and bypasses track settings.
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
#define _XOPEN_SOURCE 700
|
||||
#include "wave_internal.h"
|
||||
#include "../project/project_internal.h"
|
||||
#include "../mixer/mix_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;
|
||||
typedef struct { uint64_t start, end, offset; size_t asset, track; } region;
|
||||
struct xtc_audio_plan {
|
||||
xtc_mix_snapshot mix;
|
||||
xtc_wave waves[64];
|
||||
char paths[64][XTC_ASSET_PATH_BYTES];
|
||||
region regions[XTC_PROJECT_MAX_CLIPS];
|
||||
|
|
@ -73,6 +74,8 @@ xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
|
|||
plan = calloc(1, sizeof(*plan));
|
||||
if (!plan) return XTC_ERR_OUT_OF_MEMORY;
|
||||
plan->rate = rate;
|
||||
result = xtc_mix_prepare(project, rate, &plan->mix);
|
||||
if (result != XTC_OK) { free(plan); return result; }
|
||||
for (i = 0; i < project->clip_count; ++i) {
|
||||
const xtc_clip_info *clip = project->clips + i;
|
||||
size_t a; region *r;
|
||||
|
|
@ -92,7 +95,7 @@ xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
|
|||
if (total > 67108864U) { result = XTC_ERR_CAPACITY; break; }
|
||||
}
|
||||
r = plan->regions + plan->count++;
|
||||
r->asset = a;
|
||||
r->asset = a; r->track = xtc_mix_track(&plan->mix, clip->track_id);
|
||||
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);
|
||||
|
|
@ -106,11 +109,12 @@ 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)
|
||||
xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t position, float *output, float *right, size_t frames)
|
||||
{
|
||||
size_t i, j;
|
||||
if (!plan || !output || frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!plan || !output || (right && right == output) || frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
|
||||
memset(output, 0, frames * sizeof(*output));
|
||||
if (right) memset(right, 0, frames * sizeof(*right));
|
||||
for (i = 0; i < plan->count; ++i) {
|
||||
const region *r = plan->regions + i;
|
||||
const xtc_wave *wave = plan->waves + r->asset;
|
||||
|
|
@ -123,10 +127,18 @@ xtc_result xtc_audio_plan_render(const xtc_audio_plan *plan, uint64_t position,
|
|||
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) +
|
||||
float sample = (float)(wave->samples[index] * (1.0 - fraction) +
|
||||
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
|
||||
output[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, right ? 0U : 2U);
|
||||
if (right) right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);
|
||||
}
|
||||
}
|
||||
for (j = 0; j < frames; ++j) if (!isfinite(output[j])) output[j] = 0;
|
||||
for (j = 0; j < frames; ++j) {
|
||||
if (!isfinite(output[j])) output[j] = 0;
|
||||
if (right && !isfinite(right[j])) right[j] = 0;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_audio_plan_render(const xtc_audio_plan *p, uint64_t position, float *out, size_t frames)
|
||||
{ return xtc_audio_plan_render_stereo(p, position, out, NULL, frames); }
|
||||
|
|
|
|||
|
|
@ -7,14 +7,15 @@
|
|||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
|
||||
struct xtc_backend {
|
||||
jack_client_t *client;
|
||||
jack_port_t *input, *output, *midi_input;
|
||||
jack_port_t *input, *output, *midi_input, *output_right;
|
||||
xtc_realtime *engine;
|
||||
xtc_audio_plan *audio;
|
||||
xtc_midi_plan *midi;
|
||||
unsigned int panic_requested, panic_seen, midi_dropped;
|
||||
unsigned int panic_requested, panic_seen, midi_dropped, peak_left, peak_right;
|
||||
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
|
||||
float *timeline, *instrument;
|
||||
size_t max_frames;
|
||||
|
|
@ -56,14 +57,20 @@ static xtc_result render_midi(xtc_backend *backend, uint64_t position, jack_nfra
|
|||
}
|
||||
++count;
|
||||
}
|
||||
return xtc_midi_plan_render(backend->midi, position, events, count, backend->instrument, (size_t)frames);
|
||||
return xtc_midi_plan_render_stereo(backend->midi, position, events, count, backend->instrument,
|
||||
backend->output_right ? backend->instrument + backend->max_frames : NULL, (size_t)frames);
|
||||
}
|
||||
static int process(jack_nframes_t frames, void *context)
|
||||
{
|
||||
xtc_backend *backend = context;
|
||||
float *output = jack_port_get_buffer(backend->output, frames);
|
||||
float *right = backend->output_right ? jack_port_get_buffer(backend->output_right, frames) : NULL;
|
||||
xtc_result result = XTC_OK;
|
||||
if (output == NULL) {
|
||||
if (output == NULL || (backend->output_right && !right)) {
|
||||
if (output) memset(output, 0, (size_t)frames * sizeof(*output));
|
||||
if (right) memset(right, 0, (size_t)frames * sizeof(*right));
|
||||
__atomic_store_n(&backend->peak_left, 0U, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&backend->peak_right, 0U, __ATOMIC_RELAXED);
|
||||
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
|
||||
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
|
||||
__atomic_store_n(&backend->process_result, (unsigned int)XTC_ERR_IO, __ATOMIC_RELAXED);
|
||||
|
|
@ -79,12 +86,16 @@ static int process(jack_nframes_t frames, void *context)
|
|||
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);
|
||||
result = xtc_audio_plan_render_stereo(backend->audio, position, backend->timeline,
|
||||
right ? backend->timeline + backend->max_frames : NULL, (size_t)frames);
|
||||
if (result == XTC_OK) result = render_midi(backend, position, frames);
|
||||
if (result == XTC_OK)
|
||||
result = xtc_realtime_process_sources(backend->engine, input,
|
||||
result = xtc_realtime_process_stereo(backend->engine, input,
|
||||
backend->audio ? backend->timeline : NULL,
|
||||
backend->midi ? backend->instrument : NULL, output, (size_t)frames);
|
||||
backend->audio && right ? backend->timeline + backend->max_frames : NULL,
|
||||
backend->midi ? backend->instrument : NULL,
|
||||
backend->midi && right ? backend->instrument + backend->max_frames : NULL,
|
||||
output, right, (size_t)frames);
|
||||
}
|
||||
if (result == XTC_OK) {
|
||||
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
|
||||
|
|
@ -95,6 +106,17 @@ static int process(jack_nframes_t frames, void *context)
|
|||
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
|
||||
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
|
||||
memset(output, 0, (size_t)frames * sizeof(*output));
|
||||
if (right) memset(right, 0, (size_t)frames * sizeof(*right));
|
||||
}
|
||||
{
|
||||
size_t i; float left_peak = 0, right_peak = 0;
|
||||
for (i = 0; i < frames; ++i) {
|
||||
float l = fabsf(output[i]), r = right ? fabsf(right[i]) : l;
|
||||
if (l > left_peak) left_peak = l;
|
||||
if (r > right_peak) right_peak = r;
|
||||
}
|
||||
__atomic_store_n(&backend->peak_left, (unsigned int)(fminf(left_peak, 1000) * 1000000), __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&backend->peak_right, (unsigned int)(fminf(right_peak, 1000) * 1000000), __ATOMIC_RELAXED);
|
||||
}
|
||||
__atomic_store_n(&backend->process_result, (unsigned int)result, __ATOMIC_RELAXED);
|
||||
return 0; /* Keep connected and silent on recoverable processing errors. */
|
||||
|
|
@ -141,15 +163,15 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
if (out_backend == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out_backend = NULL;
|
||||
if (config == NULL || config->client_name == NULL || config->client_name[0] == '\0' ||
|
||||
config->max_frames == 0U || config->max_frames > 65536U || config->channel_count != 1U)
|
||||
config->max_frames == 0U || config->max_frames > 65536U || (config->channel_count != 1U && config->channel_count != 2U))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!__atomic_always_lock_free(sizeof(uint64_t), 0)) return XTC_ERR_UNSUPPORTED;
|
||||
if (config->kind != XTC_BACKEND_JACK) return XTC_ERR_UNSUPPORTED;
|
||||
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));
|
||||
backend->instrument = calloc(config->max_frames, sizeof(float));
|
||||
backend->timeline = calloc(config->max_frames * config->channel_count, sizeof(float));
|
||||
backend->instrument = calloc(config->max_frames * config->channel_count, sizeof(float));
|
||||
if (!backend->timeline || !backend->instrument) {
|
||||
free(backend->timeline); free(backend->instrument); free(backend); return XTC_ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
|
@ -165,7 +187,9 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
backend->input = jack_port_register(backend->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0U);
|
||||
backend->output = jack_port_register(backend->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0U);
|
||||
backend->midi_input = jack_port_register(backend->client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0U);
|
||||
if (backend->input == NULL || backend->output == NULL || backend->midi_input == NULL) { result = XTC_ERR_IO; goto fail; }
|
||||
if (config->channel_count == 2U)
|
||||
backend->output_right = jack_port_register(backend->client, "output_right", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0U);
|
||||
if ((config->channel_count == 2U && !backend->output_right) || backend->input == NULL || backend->output == NULL || backend->midi_input == NULL) { result = XTC_ERR_IO; goto fail; }
|
||||
if (jack_set_port_connect_callback(backend->client, port_connection, backend) != 0 ||
|
||||
jack_set_process_callback(backend->client, process, backend) != 0 ||
|
||||
jack_set_sample_rate_callback(backend->client, rate_callback, backend) != 0 ||
|
||||
|
|
@ -314,6 +338,8 @@ 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->peak_left = backend->running ? (float)__atomic_load_n(&backend->peak_left, __ATOMIC_RELAXED) / 1000000.0f : 0;
|
||||
out_status->peak_right = backend->running ? (float)__atomic_load_n(&backend->peak_right, __ATOMIC_RELAXED) / 1000000.0f : 0;
|
||||
out_status->midi_dropped = __atomic_load_n(&backend->midi_dropped, __ATOMIC_RELAXED);
|
||||
out_status->asset_issues = (unsigned int)xtc_audio_plan_issues(backend->audio, NULL);
|
||||
out_status->sample_rate = __atomic_load_n(&backend->rate, __ATOMIC_RELAXED);
|
||||
|
|
@ -338,6 +364,14 @@ xtc_result xtc_backend_port_name(const xtc_backend *backend, xtc_port_direction
|
|||
*out_name = jack_port_name(direction == XTC_PORT_INPUT ? backend->input : backend->output);
|
||||
return *out_name == NULL ? XTC_ERR_IO : XTC_OK;
|
||||
}
|
||||
xtc_result xtc_backend_output_name(const xtc_backend *backend, size_t channel, const char **out)
|
||||
{
|
||||
if (!backend || !out || channel > 1) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready || __atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE)) return XTC_ERR_INVALID_STATE;
|
||||
if (channel && !backend->output_right) return XTC_ERR_NOT_FOUND;
|
||||
*out = jack_port_name(channel ? backend->output_right : backend->output);
|
||||
return *out ? XTC_OK : XTC_ERR_IO;
|
||||
}
|
||||
xtc_result xtc_backend_visit_ports(xtc_backend *backend, xtc_port_direction direction,
|
||||
xtc_backend_port_visitor visitor, void *context)
|
||||
{
|
||||
|
|
@ -376,6 +410,8 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
|
|||
{ return route(backend, direction, remote, 1); }
|
||||
|
||||
#else
|
||||
xtc_result xtc_backend_output_name(const xtc_backend *b, size_t c, const char **out)
|
||||
{ (void)b; (void)c; (void)out; return XTC_ERR_UNSUPPORTED; }
|
||||
xtc_result xtc_backend_panic(xtc_backend *backend)
|
||||
{ (void)backend; return XTC_ERR_UNSUPPORTED; }
|
||||
xtc_result xtc_backend_midi_port_name(const xtc_backend *backend, const char **out)
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
|
|||
plan->offsets = calloc(plan->node_count + 1U, sizeof(*plan->offsets));
|
||||
plan->sources = calloc(graph->edge_count + 1U, sizeof(*plan->sources));
|
||||
plan->render = calloc(storage_nodes, sizeof(*plan->render));
|
||||
plan->buffers = calloc(storage_nodes * max_frames, sizeof(*plan->buffers));
|
||||
plan->buffers = calloc(storage_nodes * max_frames * 2U, sizeof(*plan->buffers));
|
||||
plan->peaks = calloc(storage_nodes, sizeof(*plan->peaks));
|
||||
plan->phases = calloc(storage_nodes, sizeof(*plan->phases));
|
||||
if (plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
|
||||
|
|
@ -109,50 +109,56 @@ xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_
|
|||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_plan_process_sources(xtc_plan *plan, const float *input, const float *timeline, const float *instrument, float *output,
|
||||
size_t frame_count)
|
||||
xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
|
||||
const float *timeline, const float *timeline_right, const float *instrument, const float *instrument_right,
|
||||
float *output, float *right, size_t frame_count)
|
||||
{
|
||||
size_t index, frame, edge;
|
||||
if (plan == NULL || output == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
size_t index, frame, edge, channel;
|
||||
if (plan == NULL || output == NULL || (right && right == output)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (frame_count > plan->max_frames) return XTC_ERR_CAPACITY;
|
||||
if (frame_count == 0U) return XTC_OK;
|
||||
/* All external input reads finish before output is written, allowing aliasing.
|
||||
* Nonfinite input/arithmetic becomes silence per sample, never persistent DSP state. */
|
||||
for (index = 0U; index < plan->node_count; ++index) {
|
||||
size_t node = plan->order[index];
|
||||
xtc_node spec = plan->nodes[node];
|
||||
float *buffer = plan->buffers + node * plan->max_frames;
|
||||
float peak = 0.0f;
|
||||
double phase = plan->phases[node];
|
||||
for (channel = 0; channel < (right ? 2U : 1U); ++channel) {
|
||||
const float *audio_source = channel ? timeline_right : timeline;
|
||||
const float *midi_source = channel ? instrument_right : instrument;
|
||||
float *destination = channel ? right : output;
|
||||
for (index = 0U; index < plan->node_count; ++index) {
|
||||
size_t node = plan->order[index];
|
||||
xtc_node spec = plan->nodes[node];
|
||||
float *buffer = plan->buffers + (channel * plan->node_count + node) * plan->max_frames;
|
||||
float peak = 0.0f;
|
||||
double phase = plan->phases[node];
|
||||
for (frame = 0U; frame < frame_count; ++frame) {
|
||||
float sample = 0.0f;
|
||||
if (spec.type == XTC_NODE_INSTRUMENT) {
|
||||
sample = midi_source ? midi_source[frame] : 0.0f;
|
||||
} else if (spec.type == XTC_NODE_AUDIO) {
|
||||
sample = audio_source ? audio_source[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;
|
||||
} else {
|
||||
if (plan->offsets[node] == plan->offsets[node + 1U])
|
||||
sample = input == NULL ? 0.0f : input[frame] * plan->input_gain;
|
||||
for (edge = plan->offsets[node]; edge < plan->offsets[node + 1U]; ++edge)
|
||||
sample += plan->buffers[(channel * plan->node_count + plan->sources[edge]) * plan->max_frames + frame];
|
||||
if (spec.type == XTC_NODE_GAIN) sample *= spec.value;
|
||||
}
|
||||
if (!isfinite(sample)) sample = 0.0f;
|
||||
buffer[frame] = sample;
|
||||
if (fabsf(sample) > peak) peak = fabsf(sample);
|
||||
}
|
||||
if (!right || channel == 1) plan->phases[node] = phase;
|
||||
if (spec.type == XTC_NODE_METER) plan->peaks[node] = channel && plan->peaks[node] > peak ? plan->peaks[node] : peak;
|
||||
}
|
||||
for (frame = 0U; frame < frame_count; ++frame) {
|
||||
float sample = 0.0f;
|
||||
if (spec.type == XTC_NODE_INSTRUMENT) {
|
||||
sample = instrument ? instrument[frame] : 0.0f;
|
||||
} else 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;
|
||||
} else {
|
||||
if (plan->offsets[node] == plan->offsets[node + 1U])
|
||||
sample = input == NULL ? 0.0f : input[frame] * plan->input_gain;
|
||||
for (edge = plan->offsets[node]; edge < plan->offsets[node + 1U]; ++edge)
|
||||
sample += plan->buffers[plan->sources[edge] * plan->max_frames + frame];
|
||||
if (spec.type == XTC_NODE_GAIN) sample *= spec.value;
|
||||
}
|
||||
if (!isfinite(sample)) sample = 0.0f;
|
||||
buffer[frame] = sample;
|
||||
if (fabsf(sample) > peak) peak = fabsf(sample);
|
||||
for (index = 0U; index < plan->node_count; ++index)
|
||||
if (plan->render[index]) sample += plan->buffers[(channel * plan->node_count + index) * plan->max_frames + frame];
|
||||
destination[frame] = isfinite(sample) ? sample : 0.0f;
|
||||
}
|
||||
plan->phases[node] = phase;
|
||||
if (spec.type == XTC_NODE_METER) plan->peaks[node] = peak;
|
||||
}
|
||||
for (frame = 0U; frame < frame_count; ++frame) {
|
||||
float sample = 0.0f;
|
||||
for (index = 0U; index < plan->node_count; ++index)
|
||||
if (plan->render[index]) sample += plan->buffers[index * plan->max_frames + frame];
|
||||
output[frame] = isfinite(sample) ? sample : 0.0f;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -177,3 +183,6 @@ xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output, s
|
|||
|
||||
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline, float *output, size_t frames)
|
||||
{ return xtc_plan_process_sources(plan, input, timeline, NULL, output, frames); }
|
||||
|
||||
xtc_result xtc_plan_process_sources(xtc_plan *p, const float *in, const float *audio, const float *midi, float *out, size_t frames)
|
||||
{ return xtc_plan_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames); }
|
||||
|
|
|
|||
|
|
@ -80,11 +80,12 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
|
|||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_realtime_process_sources(xtc_realtime *engine, const float *input, const float *timeline, const float *instrument,
|
||||
float *output, size_t frame_count)
|
||||
xtc_result xtc_realtime_process_stereo(xtc_realtime *engine, const float *input,
|
||||
const float *timeline, const float *timeline_right, const float *instrument, const float *instrument_right,
|
||||
float *output, float *right, size_t frame_count)
|
||||
{
|
||||
xtc_result result = XTC_OK;
|
||||
if (engine == NULL || output == NULL || frame_count > SIZE_MAX / sizeof(*output))
|
||||
if (engine == NULL || output == NULL || (right && right == output) || frame_count > SIZE_MAX / sizeof(*output))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (frame_count == 0U) return XTC_OK;
|
||||
if (frame_count > engine->max_frames) {
|
||||
|
|
@ -107,11 +108,16 @@ xtc_result xtc_realtime_process_sources(xtc_realtime *engine, const float *input
|
|||
}
|
||||
}
|
||||
if (engine->active != NULL)
|
||||
result = xtc_plan_process_sources(engine->active, input, timeline, instrument, output, frame_count);
|
||||
else
|
||||
result = xtc_plan_process_stereo(engine->active, input, timeline, timeline_right, instrument, instrument_right, output, right, frame_count);
|
||||
else {
|
||||
memset(output, 0, frame_count * sizeof(*output));
|
||||
if (right) memset(right, 0, frame_count * sizeof(*right));
|
||||
}
|
||||
}
|
||||
if (result != XTC_OK) {
|
||||
memset(output, 0, frame_count * sizeof(*output));
|
||||
if (right) memset(right, 0, frame_count * sizeof(*right));
|
||||
}
|
||||
if (result != XTC_OK) memset(output, 0, frame_count * sizeof(*output));
|
||||
__atomic_store_n(&engine->result, (unsigned int)result, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&engine->blocks,
|
||||
__atomic_load_n(&engine->blocks, __ATOMIC_RELAXED) + 1U, __ATOMIC_RELAXED);
|
||||
|
|
@ -123,3 +129,6 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input, float
|
|||
|
||||
xtc_result xtc_realtime_process_audio(xtc_realtime *engine, const float *input, const float *timeline, float *output, size_t frames)
|
||||
{ return xtc_realtime_process_sources(engine, input, timeline, NULL, output, frames); }
|
||||
|
||||
xtc_result xtc_realtime_process_sources(xtc_realtime *p, const float *in, const float *audio, const float *midi, float *out, size_t frames)
|
||||
{ return xtc_realtime_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames); }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "graph_widget.hpp"
|
||||
#include "sequencer_widget.hpp"
|
||||
#include "piano_roll.hpp"
|
||||
#include "mixer_widget.hpp"
|
||||
#include <FL/Fl_Native_File_Chooser.H>
|
||||
#include "xtc/xtc_version.h"
|
||||
|
||||
|
|
@ -33,8 +34,15 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
xtc_result result = xtc_project_create(&initial);
|
||||
if (result != XTC_OK) throw std::runtime_error(xtc_result_string(result));
|
||||
project_.reset(initial);
|
||||
xtc_history *history = nullptr;
|
||||
result = xtc_history_create(initial, &history);
|
||||
if (result != XTC_OK) throw std::runtime_error(xtc_result_string(result));
|
||||
history_.reset(history);
|
||||
begin();
|
||||
auto *menu = new Fl_Menu_Bar(0, 0, 1000, 28);
|
||||
menu->add("Edit/Undo", FL_CTRL + 'z', history_callback, this);
|
||||
menu->add("Edit/Redo", FL_CTRL + FL_SHIFT + 'z', history_callback, this);
|
||||
menu->add("Edit/Redo alternate", FL_CTRL + 'y', history_callback, this);
|
||||
menu->add("File/New", FL_CTRL + 'n', file_callback, this);
|
||||
menu->add("File/Open", FL_CTRL + 'o', file_callback, this);
|
||||
menu->add("File/Save", FL_CTRL + 's', file_callback, this);
|
||||
|
|
@ -102,7 +110,9 @@ MainWindow::MainWindow(bool prompt_unsaved)
|
|||
sequencer->end();
|
||||
auto *mixer = new Fl_Group(8, 140, 984, 444, "Mixer");
|
||||
mixer->begin();
|
||||
new Fl_Box(24, 164, 940, 40, "Mixer placeholder — channels and meters are not implemented yet.");
|
||||
mixer_ = new MixerWidget(20, 150, 956, 420, project_.get(), before_arrangement_edit, this);
|
||||
mixer_->callback([](Fl_Widget *, void *c) { static_cast<MainWindow *>(c)->mark_dirty(); }, this);
|
||||
mixer->resizable(mixer_);
|
||||
mixer->end();
|
||||
auto *graph = new Fl_Group(8, 140, 984, 444, "Modular Graph");
|
||||
graph->begin();
|
||||
|
|
@ -216,7 +226,7 @@ xtc_result MainWindow::edit_graph(void *context, const xtc_graph_edit &edit, siz
|
|||
|
||||
void MainWindow::open_backend()
|
||||
{
|
||||
xtc_backend_config config = {XTC_BACKEND_JACK, XTC_PROJECT_NAME, 65536U, 1U};
|
||||
xtc_backend_config config = {XTC_BACKEND_JACK, XTC_PROJECT_NAME, 65536U, 2U};
|
||||
xtc_result result = xtc_backend_open(&config, &backend_);
|
||||
ready_ = result == XTC_OK;
|
||||
running_ = false;
|
||||
|
|
@ -280,6 +290,7 @@ void MainWindow::reconnect_engine()
|
|||
|
||||
void MainWindow::refresh_status()
|
||||
{
|
||||
if (mixer_) mixer_->peaks(0, 0);
|
||||
bool can_play = false, can_record = false;
|
||||
if (!ready_) {
|
||||
set_label(indicator_, "Audio: disconnected");
|
||||
|
|
@ -310,8 +321,9 @@ void MainWindow::refresh_status()
|
|||
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));
|
||||
mixer_->peaks(status.peak_left, status.peak_right);
|
||||
char text[256];
|
||||
std::snprintf(text, sizeof(text), "JACK | %u Hz | %u frames | mono | %u blocks | %u xruns | %s | %u asset issues | %u MIDI drops",
|
||||
std::snprintf(text, sizeof(text), "JACK | %u Hz | %u frames | stereo | %u blocks | %u xruns | %s | %u asset issues | %u MIDI drops",
|
||||
status.sample_rate, status.buffer_frames, status.processed_blocks, status.xruns,
|
||||
xtc_result_string(status.process_result), status.asset_issues, status.midi_dropped);
|
||||
set_label(status_, text);
|
||||
|
|
@ -372,7 +384,7 @@ void MainWindow::view_callback(Fl_Widget *widget, void *view)
|
|||
}
|
||||
void MainWindow::about_callback(Fl_Widget *, void *)
|
||||
{
|
||||
fl_message("%s %s\nModular audio workstation\nPhase 7: MIDI input, sine instrument, persisted notes and piano roll.",
|
||||
fl_message("%s %s\nModular audio workstation\nPhase 8: stereo track mixer, automation, meters and undo/redo.",
|
||||
XTC_PROJECT_NAME, XTC_VERSION_STRING);
|
||||
}
|
||||
void MainWindow::timer_callback(void *context)
|
||||
|
|
@ -392,6 +404,9 @@ void MainWindow::timer_callback(void *context)
|
|||
|
||||
void MainWindow::mark_dirty()
|
||||
{
|
||||
xtc_result history_result = xtc_history_record(history_.get(), project_.get());
|
||||
if (history_result != XTC_OK) report_error("Undo history could not retain edit", history_result);
|
||||
mixer_->set_project(project_.get());
|
||||
graph_widget_->set_graph(xtc_project_graph(project_.get()));
|
||||
if (piano_) piano_->set_project(project_.get(), piano_->clip());
|
||||
dirty_ = true; copy_label("xtc * — unsaved changes");
|
||||
|
|
@ -420,9 +435,16 @@ xtc_result MainWindow::save_project(const char *path)
|
|||
if (r == XTC_OK) { path_ = saved_path; dirty_ = false; copy_label(path_.c_str()); }
|
||||
return r;
|
||||
}
|
||||
xtc_result MainWindow::install_project(xtc_project *candidate)
|
||||
xtc_result MainWindow::install_project(xtc_project *candidate, bool reset_history)
|
||||
{
|
||||
if (recorder_) return XTC_ERR_INVALID_STATE;
|
||||
std::unique_ptr<xtc_history, decltype(&xtc_history_destroy)> new_history(nullptr, xtc_history_destroy);
|
||||
if (reset_history) {
|
||||
xtc_history *h = nullptr;
|
||||
xtc_result result = xtc_history_create(candidate, &h);
|
||||
if (result != XTC_OK) return result;
|
||||
new_history.reset(h);
|
||||
}
|
||||
xtc_result r = XTC_OK;
|
||||
if (ready_) {
|
||||
r = xtc_backend_stop(backend_);
|
||||
|
|
@ -444,6 +466,8 @@ xtc_result MainWindow::install_project(xtc_project *candidate)
|
|||
if (piano_window_) piano_window_->hide();
|
||||
if (piano_) piano_->set_project(candidate, UINT64_MAX);
|
||||
project_.reset(candidate);
|
||||
mixer_->set_project(candidate);
|
||||
if (reset_history) history_.swap(new_history);
|
||||
graph_widget_->reset_layout();
|
||||
graph_widget_->set_graph(xtc_project_graph(candidate));
|
||||
sequencer_->set_project(candidate);
|
||||
|
|
@ -634,3 +658,23 @@ void MainWindow::panic_callback(Fl_Widget *, void *context)
|
|||
xtc_result r = xtc_backend_panic(window->backend_);
|
||||
if (r != XTC_OK) window->report_error("Panic failed", r);
|
||||
}
|
||||
|
||||
xtc_result MainWindow::undo(bool redo)
|
||||
{
|
||||
if (recorder_) return XTC_ERR_INVALID_STATE;
|
||||
xtc_project *candidate = nullptr;
|
||||
xtc_result r = xtc_history_checkout(history_.get(), redo ? 1 : 0, &candidate);
|
||||
if (r == XTC_OK) r = install_project(candidate, false);
|
||||
if (r != XTC_OK) { xtc_project_destroy(candidate); return r; }
|
||||
r = xtc_history_accept(history_.get(), redo ? 1 : 0);
|
||||
dirty_ = true; copy_label("xtc * — unsaved changes");
|
||||
return r;
|
||||
}
|
||||
void MainWindow::history_callback(Fl_Widget *widget, void *context)
|
||||
{
|
||||
auto *window = static_cast<MainWindow *>(context);
|
||||
auto *menu = static_cast<Fl_Menu_Bar *>(widget);
|
||||
bool redo = std::strncmp(menu->mvalue()->label(), "Redo", 4) == 0;
|
||||
xtc_result r = window->undo(redo);
|
||||
if (r != XTC_OK && r != XTC_ERR_NOT_FOUND) window->report_error("Undo/redo failed", r);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,10 @@
|
|||
#include <string>
|
||||
#include "xtc/xtc_project.h"
|
||||
#include "xtc/xtc_backend.h"
|
||||
#include "xtc/xtc_mixer.h"
|
||||
|
||||
class GraphWidget;
|
||||
class MixerWidget;
|
||||
class PianoRollWidget;
|
||||
class SequencerWidget;
|
||||
class Fl_Box;
|
||||
|
|
@ -23,6 +25,8 @@ public:
|
|||
using Fl_Double_Window::show;
|
||||
int handle(int event) override;
|
||||
|
||||
xtc_result undo(bool redo = false);
|
||||
MixerWidget *mixer() const { return mixer_; }
|
||||
xtc_result open_piano_roll();
|
||||
PianoRollWidget *piano_roll() const { return piano_; }
|
||||
xtc_result import_audio(const char *path);
|
||||
|
|
@ -30,6 +34,9 @@ public:
|
|||
xtc_result save_project(const char *path);
|
||||
xtc_result open_project(const char *path);
|
||||
private:
|
||||
MixerWidget *mixer_ = nullptr;
|
||||
std::unique_ptr<xtc_history, decltype(&xtc_history_destroy)> history_{nullptr, xtc_history_destroy};
|
||||
static void history_callback(Fl_Widget *, void *);
|
||||
std::unique_ptr<xtc_project, decltype(&xtc_project_destroy)> project_{nullptr, xtc_project_destroy};
|
||||
std::unique_ptr<Fl_Double_Window> piano_window_;
|
||||
PianoRollWidget *piano_ = nullptr;
|
||||
|
|
@ -46,7 +53,7 @@ private:
|
|||
std::string path_;
|
||||
bool discard_changes();
|
||||
void mark_dirty();
|
||||
xtc_result install_project(xtc_project *candidate);
|
||||
xtc_result install_project(xtc_project *candidate, bool reset_history = true);
|
||||
static xtc_result seek(void *context, uint64_t tick);
|
||||
static void file_callback(Fl_Widget *, void *context);
|
||||
GraphWidget *graph_widget_ = nullptr;
|
||||
|
|
|
|||
314
src/gui/mixer_widget.cpp
Normal file
314
src/gui/mixer_widget.cpp
Normal file
|
|
@ -0,0 +1,314 @@
|
|||
#include "mixer_widget.hpp"
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Check_Button.H>
|
||||
#include <FL/Fl_Choice.H>
|
||||
#include <FL/Fl_Progress.H>
|
||||
#include <FL/Fl_Value_Input.H>
|
||||
#include <FL/Fl_Value_Slider.H>
|
||||
#include <FL/fl_ask.H>
|
||||
#include <FL/fl_draw.H>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
namespace
|
||||
{
|
||||
constexpr uint64_t lane_ticks = 15360;
|
||||
}
|
||||
class AutomationLane : public Fl_Widget
|
||||
{
|
||||
MixerWidget &mixer_;
|
||||
|
||||
public:
|
||||
AutomationLane(int x, int y, int w, int h, MixerWidget &m) : Fl_Widget(x, y, w, h), mixer_(m) {}
|
||||
void draw() override
|
||||
{
|
||||
fl_push_clip(x(), y(), w(), h());
|
||||
fl_color(fl_rgb_color(32, 35, 40));
|
||||
fl_rectf(x(), y(), w(), h());
|
||||
fl_font(FL_HELVETICA, 11);
|
||||
for (int i = 0; i <= 16; ++i) {
|
||||
int px = x() + i * (w() - 1) / 16;
|
||||
fl_color(fl_rgb_color(i % 4 ? 55 : 95, 60, 65));
|
||||
fl_line(px, y(), px, y() + h());
|
||||
if (i % 4 == 0) {
|
||||
char label[32];
|
||||
std::snprintf(label, sizeof(label), "%llu",
|
||||
static_cast<unsigned long long>(mixer_.lane_start() +
|
||||
static_cast<uint64_t>(i) * 960));
|
||||
fl_color(FL_WHITE);
|
||||
fl_draw(label, px + 3, y() + 14);
|
||||
}
|
||||
}
|
||||
size_t count = 0;
|
||||
xtc_project_automation_count(mixer_.project(), &count);
|
||||
std::vector<xtc_automation_point> points;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
xtc_automation_point a{};
|
||||
xtc_project_get_automation(mixer_.project(), i, &a);
|
||||
if (a.track_id == mixer_.selected_track() && a.parameter == mixer_.parameter())
|
||||
points.push_back(a);
|
||||
}
|
||||
std::sort(points.begin(), points.end(),
|
||||
[](const auto &a, const auto &b) { return a.tick < b.tick; });
|
||||
float min = mixer_.parameter() == XTC_AUTOMATION_GAIN ? 0.0f : -1.0f;
|
||||
float range = mixer_.parameter() == XTC_AUTOMATION_GAIN ? 4.0f : 2.0f;
|
||||
auto py = [&](float v) {
|
||||
return y() + h() - 6 - static_cast<int>((v - min) / range * (h() - 28));
|
||||
};
|
||||
auto value = [&](uint64_t tick) {
|
||||
if (points.empty())
|
||||
return min;
|
||||
auto it = std::upper_bound(points.begin(), points.end(), tick,
|
||||
[](uint64_t t, const auto &a) { return t < a.tick; });
|
||||
if (it == points.begin())
|
||||
return it->value;
|
||||
if (it == points.end())
|
||||
return points.back().value;
|
||||
const auto &a = *(it - 1);
|
||||
return a.value + static_cast<float>(static_cast<double>(tick - a.tick) /
|
||||
static_cast<double>(it->tick - a.tick)) *
|
||||
(it->value - a.value);
|
||||
};
|
||||
fl_color(fl_rgb_color(105, 200, 150));
|
||||
if (!points.empty())
|
||||
for (int px = 1; px < w(); ++px) {
|
||||
uint64_t a = mixer_.lane_start() + static_cast<uint64_t>(px - 1) * lane_ticks /
|
||||
static_cast<uint64_t>(w() - 1);
|
||||
uint64_t b = mixer_.lane_start() + static_cast<uint64_t>(px) * lane_ticks /
|
||||
static_cast<uint64_t>(w() - 1);
|
||||
fl_line(x() + px - 1, py(value(a)), x() + px, py(value(b)));
|
||||
}
|
||||
for (const auto &a : points)
|
||||
if (a.tick >= mixer_.lane_start() && a.tick <= mixer_.lane_start() + lane_ticks) {
|
||||
int px = x() + static_cast<int>((a.tick - mixer_.lane_start()) *
|
||||
static_cast<uint64_t>(w() - 1) / lane_ticks);
|
||||
fl_rectf(px - 4, py(a.value) - 4, 8, 8);
|
||||
}
|
||||
if (points.empty()) {
|
||||
fl_color(FL_WHITE);
|
||||
fl_draw("Click to add automation; right-click a point to remove. Values replace the "
|
||||
"static control.",
|
||||
x() + 12, y() + h() / 2);
|
||||
}
|
||||
fl_pop_clip();
|
||||
}
|
||||
int handle(int event) override
|
||||
{
|
||||
if (event != FL_PUSH)
|
||||
return Fl_Widget::handle(event);
|
||||
if (Fl::event_button() != FL_LEFT_MOUSE && Fl::event_button() != FL_RIGHT_MOUSE)
|
||||
return 0;
|
||||
uint64_t tick = mixer_.lane_start() +
|
||||
static_cast<uint64_t>(std::clamp(Fl::event_x() - x(), 0, w() - 1)) *
|
||||
lane_ticks / static_cast<uint64_t>(w() - 1);
|
||||
tick = (tick + 120) / 240 * 240;
|
||||
tick = std::min(tick, XTC_PROJECT_MAX_TICK);
|
||||
float min = mixer_.parameter() == XTC_AUTOMATION_GAIN ? 0.0f : -1.0f;
|
||||
float range = mixer_.parameter() == XTC_AUTOMATION_GAIN ? 4.0f : 2.0f;
|
||||
float value =
|
||||
min + static_cast<float>(std::clamp(y() + h() - 6 - Fl::event_y(), 0, h() - 28)) /
|
||||
static_cast<float>(h() - 28) * range;
|
||||
if (Fl::event_button() == FL_RIGHT_MOUSE) {
|
||||
size_t count = 0;
|
||||
xtc_project_automation_count(mixer_.project(), &count);
|
||||
uint64_t distance =
|
||||
static_cast<uint64_t>(8) * lane_ticks / static_cast<uint64_t>(w() - 1) + 1,
|
||||
closest = UINT64_MAX;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
xtc_automation_point a{};
|
||||
xtc_project_get_automation(mixer_.project(), i, &a);
|
||||
uint64_t d = a.tick > tick ? a.tick - tick : tick - a.tick;
|
||||
if (a.track_id == mixer_.selected_track() && a.parameter == mixer_.parameter() &&
|
||||
d < distance) {
|
||||
distance = d;
|
||||
closest = a.tick;
|
||||
}
|
||||
}
|
||||
if (closest != UINT64_MAX)
|
||||
mixer_.point(closest, 0, true);
|
||||
} else
|
||||
mixer_.point(tick, value);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
MixerWidget::MixerWidget(int x, int y, int w, int h, xtc_project *p, BeforeEdit before,
|
||||
void *context)
|
||||
: Fl_Group(x, y, w, h), project_(p), before_(before), context_(context)
|
||||
{
|
||||
begin();
|
||||
tracks_ = new Fl_Choice(x + 70, y + 8, 240, 26, "Track");
|
||||
tracks_->callback(select, this);
|
||||
gain_ = new Fl_Value_Slider(x + 70, y + 48, 320, 28, "Gain");
|
||||
gain_->type(FL_HOR_NICE_SLIDER);
|
||||
gain_->bounds(0, 4);
|
||||
gain_->step(0.01);
|
||||
gain_->when(FL_WHEN_RELEASE);
|
||||
gain_->callback(control, this);
|
||||
pan_ = new Fl_Value_Slider(x + 480, y + 48, 320, 28, "Pan");
|
||||
pan_->type(FL_HOR_NICE_SLIDER);
|
||||
pan_->bounds(-1, 1);
|
||||
pan_->step(0.01);
|
||||
pan_->when(FL_WHEN_RELEASE);
|
||||
pan_->callback(control, this);
|
||||
mute_ = new Fl_Check_Button(x + 340, y + 8, 90, 26, "Mute");
|
||||
solo_ = new Fl_Check_Button(x + 440, y + 8, 90, 26, "Solo");
|
||||
mute_->callback(control, this);
|
||||
solo_->callback(control, this);
|
||||
left_ = new Fl_Progress(x + 70, y + 90, 320, 20, "Output L");
|
||||
right_ = new Fl_Progress(x + 480, y + 90, 320, 20, "Output R");
|
||||
for (auto *meter : {left_, right_}) {
|
||||
meter->minimum(0);
|
||||
meter->maximum(1);
|
||||
meter->selection_color(fl_rgb_color(80, 180, 120));
|
||||
}
|
||||
parameter_ = new Fl_Choice(x + 110, y + 126, 150, 26, "Automation");
|
||||
parameter_->add("Gain|Pan");
|
||||
parameter_->value(0);
|
||||
parameter_->callback(select, this);
|
||||
start_ = new Fl_Value_Input(x + 420, y + 126, 140, 26, "First tick");
|
||||
start_->bounds(0, XTC_PROJECT_MAX_TICK - lane_ticks);
|
||||
start_->step(3840);
|
||||
start_->callback(select, this);
|
||||
auto *hint = new Fl_Box(x + 10, y + 160, w - 20, 24,
|
||||
"Gain 0..4; pan -1 left / +1 right. M/S toggles; arrows change gain, "
|
||||
"Shift+arrows pan; Up/Down selects track.");
|
||||
hint->labelsize(12);
|
||||
lane_ = new AutomationLane(x + 10, y + 192, w - 20, h - 222, *this);
|
||||
end();
|
||||
set_project(p);
|
||||
}
|
||||
uint64_t MixerWidget::selected_track() const
|
||||
{
|
||||
xtc_track_info t{};
|
||||
return xtc_project_get_track(project_, static_cast<size_t>(tracks_->value()), &t) == XTC_OK
|
||||
? t.id
|
||||
: UINT64_MAX;
|
||||
}
|
||||
xtc_automation_parameter MixerWidget::parameter() const
|
||||
{
|
||||
return parameter_->value() == 1 ? XTC_AUTOMATION_PAN : XTC_AUTOMATION_GAIN;
|
||||
}
|
||||
uint64_t MixerWidget::lane_start() const
|
||||
{
|
||||
if (!std::isfinite(start_->value())) return 0;
|
||||
return static_cast<uint64_t>(
|
||||
std::clamp(start_->value(), 0.0, static_cast<double>(XTC_PROJECT_MAX_TICK - lane_ticks)));
|
||||
}
|
||||
void MixerWidget::set_project(xtc_project *p)
|
||||
{
|
||||
uint64_t selected = project_ == p ? selected_track() : UINT64_MAX;
|
||||
project_ = p;
|
||||
tracks_->clear();
|
||||
size_t n = 0, c = 0;
|
||||
xtc_project_get_counts(p, &n, &c);
|
||||
int index = 0;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
xtc_track_info t{};
|
||||
xtc_project_get_track(p, i, &t);
|
||||
std::string label;
|
||||
for (char ch : std::string(t.name)) {
|
||||
if (ch == '/' || ch == '\\' || ch == '_' || ch == '&')
|
||||
label += '\\';
|
||||
label += ch;
|
||||
}
|
||||
tracks_->add(label.c_str());
|
||||
if (t.id == selected)
|
||||
index = static_cast<int>(i);
|
||||
}
|
||||
tracks_->value(n ? index : -1);
|
||||
selection();
|
||||
redraw();
|
||||
}
|
||||
void MixerWidget::selection()
|
||||
{
|
||||
xtc_track_info t{};
|
||||
bool valid =
|
||||
xtc_project_get_track(project_, static_cast<size_t>(tracks_->value()), &t) == XTC_OK;
|
||||
for (Fl_Widget *w : {static_cast<Fl_Widget *>(gain_), static_cast<Fl_Widget *>(pan_),
|
||||
static_cast<Fl_Widget *>(mute_), static_cast<Fl_Widget *>(solo_),
|
||||
static_cast<Fl_Widget *>(lane_)}) {
|
||||
if (valid)
|
||||
w->activate();
|
||||
else
|
||||
w->deactivate();
|
||||
}
|
||||
gain_->value(valid ? t.gain : 1);
|
||||
pan_->value(t.pan);
|
||||
mute_->value(t.mute);
|
||||
solo_->value(t.solo);
|
||||
lane_->redraw();
|
||||
}
|
||||
void MixerWidget::peaks(float left, float right)
|
||||
{
|
||||
left_->value(std::min(left, 1.0f));
|
||||
right_->value(std::min(right, 1.0f));
|
||||
left_->selection_color(left > 1 ? FL_RED : FL_GREEN);
|
||||
right_->selection_color(right > 1 ? FL_RED : FL_GREEN);
|
||||
}
|
||||
xtc_result MixerWidget::changed(xtc_result r)
|
||||
{
|
||||
if (r == XTC_OK)
|
||||
do_callback();
|
||||
else
|
||||
fl_alert("Mixer edit failed: %s", xtc_result_string(r));
|
||||
selection();
|
||||
return r;
|
||||
}
|
||||
xtc_result MixerWidget::mix(float gain, float pan, int mute, int solo)
|
||||
{
|
||||
xtc_result r = before_ ? before_(context_) : XTC_OK;
|
||||
if (r == XTC_OK)
|
||||
r = xtc_project_set_mix(project_, selected_track(), gain, pan, mute, solo);
|
||||
return changed(r);
|
||||
}
|
||||
xtc_result MixerWidget::point(uint64_t tick, float value, bool remove)
|
||||
{
|
||||
xtc_result r = before_ ? before_(context_) : XTC_OK;
|
||||
xtc_automation_point a{selected_track(), tick, parameter(), value};
|
||||
if (r == XTC_OK)
|
||||
r = remove ? xtc_project_remove_automation(project_, a.track_id, a.parameter, tick)
|
||||
: xtc_project_set_automation(project_, &a);
|
||||
return changed(r);
|
||||
}
|
||||
void MixerWidget::control(Fl_Widget *, void *c)
|
||||
{
|
||||
auto *m = static_cast<MixerWidget *>(c);
|
||||
m->mix(static_cast<float>(m->gain_->value()), static_cast<float>(m->pan_->value()),
|
||||
m->mute_->value(), m->solo_->value());
|
||||
}
|
||||
void MixerWidget::select(Fl_Widget *, void *c)
|
||||
{
|
||||
static_cast<MixerWidget *>(c)->selection();
|
||||
}
|
||||
int MixerWidget::handle(int e)
|
||||
{
|
||||
if (e == FL_KEYDOWN && !(Fl::event_state() & (FL_CTRL | FL_ALT | FL_META))) {
|
||||
int key = Fl::event_key();
|
||||
xtc_track_info t{};
|
||||
if (xtc_project_get_track(project_, static_cast<size_t>(tracks_->value()), &t) == XTC_OK) {
|
||||
if (key == 'm' || key == 's') {
|
||||
mix(t.gain, t.pan, key == 'm' ? !t.mute : t.mute, key == 's' ? !t.solo : t.solo);
|
||||
return 1;
|
||||
}
|
||||
if (key == FL_Left || key == FL_Right) {
|
||||
float d = key == FL_Left ? -0.05f : 0.05f;
|
||||
bool pan = (Fl::event_state() & FL_SHIFT) != 0;
|
||||
mix(pan ? t.gain : std::clamp(t.gain + d, 0.0f, 4.0f),
|
||||
pan ? std::clamp(t.pan + d, -1.0f, 1.0f) : t.pan, t.mute, t.solo);
|
||||
return 1;
|
||||
}
|
||||
if (key == FL_Up || key == FL_Down) {
|
||||
size_t n = 0, c = 0;
|
||||
xtc_project_get_counts(project_, &n, &c);
|
||||
tracks_->value(std::clamp(tracks_->value() + (key == FL_Up ? -1 : 1), 0,
|
||||
static_cast<int>(n) - 1));
|
||||
selection();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Fl_Group::handle(e);
|
||||
}
|
||||
44
src/gui/mixer_widget.hpp
Normal file
44
src/gui/mixer_widget.hpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#ifndef XTC_MIXER_WIDGET_HPP
|
||||
#define XTC_MIXER_WIDGET_HPP
|
||||
#include "xtc/xtc_mixer.h"
|
||||
#include <FL/Fl_Group.H>
|
||||
class Fl_Choice;
|
||||
class Fl_Value_Slider;
|
||||
class Fl_Check_Button;
|
||||
class Fl_Progress;
|
||||
class Fl_Value_Input;
|
||||
class AutomationLane;
|
||||
class MixerWidget : public Fl_Group
|
||||
{
|
||||
public:
|
||||
using BeforeEdit = xtc_result (*)(void *);
|
||||
MixerWidget(int, int, int, int, xtc_project *, BeforeEdit, void *);
|
||||
void set_project(xtc_project *);
|
||||
void peaks(float left, float right);
|
||||
uint64_t selected_track() const;
|
||||
xtc_project *project() const
|
||||
{
|
||||
return project_;
|
||||
}
|
||||
xtc_automation_parameter parameter() const;
|
||||
uint64_t lane_start() const;
|
||||
xtc_result point(uint64_t tick, float value, bool remove = false);
|
||||
xtc_result mix(float gain, float pan, int mute, int solo);
|
||||
int handle(int) override;
|
||||
|
||||
private:
|
||||
xtc_project *project_;
|
||||
BeforeEdit before_;
|
||||
void *context_;
|
||||
Fl_Choice *tracks_, *parameter_;
|
||||
Fl_Value_Slider *gain_, *pan_;
|
||||
Fl_Check_Button *mute_, *solo_;
|
||||
Fl_Progress *left_, *right_;
|
||||
Fl_Value_Input *start_;
|
||||
AutomationLane *lane_;
|
||||
xtc_result changed(xtc_result);
|
||||
void selection();
|
||||
static void control(Fl_Widget *, void *);
|
||||
static void select(Fl_Widget *, void *);
|
||||
};
|
||||
#endif
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
#include "xtc/xtc_midi.h"
|
||||
#include "../project/project_internal.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define TAU 6.28318530717958647692
|
||||
typedef struct { uint64_t start, end, age; unsigned int pitch, velocity, channel; } span;
|
||||
typedef struct { uint64_t start, end, age; unsigned int pitch, velocity, channel; size_t track; } span;
|
||||
typedef struct { uint64_t time; int change; } endpoint;
|
||||
typedef struct { unsigned int active, channel, pitch, velocity; uint64_t age; double phase; } voice;
|
||||
struct xtc_midi_plan {
|
||||
xtc_mix_snapshot mix;
|
||||
span notes[XTC_PROJECT_MAX_NOTES];
|
||||
size_t count;
|
||||
voice voices[XTC_MIDI_VOICES];
|
||||
|
|
@ -51,6 +52,10 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
if (!project || !rate || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
|
||||
p = calloc(1, sizeof(*p)); points = calloc(XTC_PROJECT_MAX_NOTES * 2U, sizeof(*points));
|
||||
if (!p || !points) { free(p); free(points); return XTC_ERR_OUT_OF_MEMORY; }
|
||||
{
|
||||
xtc_result r = xtc_mix_prepare(project, rate, &p->mix);
|
||||
if (r != XTC_OK) { free(points); free(p); return r; }
|
||||
}
|
||||
for (i = 0; i < 128; ++i) {
|
||||
double frequency = 440.0 * pow(2.0, ((double)i - 69.0) / 12.0);
|
||||
p->step[i] = frequency < rate * 0.5 ? frequency / rate : 0;
|
||||
|
|
@ -71,6 +76,7 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
if (r == XTC_OK) r = xtc_project_tick_to_sample(project, begin - n->start_tick, rate, &s->age);
|
||||
if (r != XTC_OK) { free(points); free(p); return r; }
|
||||
if (s->end <= s->start) continue;
|
||||
s->track = xtc_mix_track(&p->mix, c.track_id);
|
||||
s->pitch = n->pitch; s->velocity = n->velocity; s->channel = n->channel;
|
||||
points[p->count * 2].time = s->start; points[p->count * 2].change = 1;
|
||||
points[p->count * 2 + 1].time = s->end; points[p->count * 2 + 1].change = -1;
|
||||
|
|
@ -122,7 +128,7 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
p->voices[slot].pitch = e->pitch; p->voices[slot].velocity = e->velocity;
|
||||
p->voices[slot].phase = 0; p->voices[slot].age = p->age++;
|
||||
}
|
||||
static void segment(xtc_midi_plan *p, uint64_t position, float *out, size_t frames)
|
||||
static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t i, j;
|
||||
for (i = 0; i < p->count; ++i) {
|
||||
|
|
@ -130,27 +136,30 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, size_t fram
|
|||
uint64_t begin = position > s->start ? position : s->start;
|
||||
uint64_t end = position + frames < s->end ? position + frames : s->end;
|
||||
double step = p->step[s->pitch];
|
||||
if (!step || s->start < p->cutoff[s->channel] || end <= begin) continue;
|
||||
if (step == 0.0 || s->start < p->cutoff[s->channel] || end <= begin) continue;
|
||||
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
|
||||
double phase = fmod((double)(position + j - s->start + s->age) * step, 1.0);
|
||||
out[j] += (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
|
||||
float sample = (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
|
||||
out[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, right ? 0U : 2U);
|
||||
if (right) right[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, 1);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < XTC_MIDI_VOICES; ++i) {
|
||||
voice *v = p->voices + i;
|
||||
double step = p->step[v->pitch];
|
||||
if (!v->active || !step) continue;
|
||||
if (!v->active || step == 0.0) continue;
|
||||
for (j = 0; j < frames; ++j) {
|
||||
out[j] += (float)(sin(TAU * v->phase) * 0.1 * v->velocity / 127.0);
|
||||
float sample = (float)(sin(TAU * v->phase) * 0.1 * v->velocity / 127.0);
|
||||
out[j] += sample; if (right) right[j] += sample;
|
||||
v->phase += step; if (v->phase >= 1) v->phase -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position,
|
||||
const xtc_midi_event *events, size_t count, float *out, size_t frames)
|
||||
xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position,
|
||||
const xtc_midi_event *events, size_t count, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t i, cursor = 0;
|
||||
if (!p || !out || count > XTC_MIDI_MAX_EVENTS || (count && !events) ||
|
||||
if (!p || !out || (right && right == out) || count > XTC_MIDI_MAX_EVENTS || (count && !events) ||
|
||||
frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < count; ++i) {
|
||||
const xtc_midi_event *e = events + i;
|
||||
|
|
@ -162,12 +171,16 @@ xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position,
|
|||
if (!frames) return XTC_OK;
|
||||
if (p->next_position != position) xtc_midi_plan_reset(p);
|
||||
memset(out, 0, frames * sizeof(*out));
|
||||
if (right) memset(right, 0, frames * sizeof(*right));
|
||||
for (i = 0; i < count; ++i) {
|
||||
size_t next = events[i].offset;
|
||||
segment(p, position + cursor, out + cursor, next - cursor);
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, next - cursor);
|
||||
event(p, events + i, position + next); cursor = next;
|
||||
}
|
||||
segment(p, position + cursor, out + cursor, frames - cursor);
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, frames - cursor);
|
||||
p->next_position = position + frames;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position, const xtc_midi_event *events, size_t count, float *out, size_t frames)
|
||||
{ return xtc_midi_plan_render_stereo(p, position, events, count, out, NULL, frames); }
|
||||
|
|
|
|||
6
src/mixer/README.md
Normal file
6
src/mixer/README.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# Mixer core
|
||||
|
||||
`model.c` validates track settings and stable track-relative automation references.
|
||||
`render.c` prepares sorted sample endpoints and evaluates linear gain/pan lanes
|
||||
without allocation. `mix_internal.h` is private snapshot storage for audio/MIDI
|
||||
source plans. See ../../docs/MIXER.md for the stereo pan law and ownership.
|
||||
20
src/mixer/mix_internal.h
Normal file
20
src/mixer/mix_internal.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef XTC_MIX_INTERNAL_H
|
||||
#define XTC_MIX_INTERNAL_H
|
||||
#include "../project/project_internal.h"
|
||||
typedef struct {
|
||||
uint64_t sample, tick;
|
||||
size_t track;
|
||||
unsigned int parameter;
|
||||
float value;
|
||||
} xtc_mix_point;
|
||||
typedef struct {
|
||||
xtc_track_info tracks[XTC_PROJECT_MAX_TRACKS];
|
||||
size_t count, offsets[XTC_PROJECT_MAX_TRACKS][2], lengths[XTC_PROJECT_MAX_TRACKS][2];
|
||||
xtc_mix_point points[XTC_MAX_AUTOMATION_POINTS];
|
||||
int any_solo;
|
||||
} xtc_mix_snapshot;
|
||||
xtc_result xtc_mix_prepare(const xtc_project *, unsigned int rate, xtc_mix_snapshot *);
|
||||
size_t xtc_mix_track(const xtc_mix_snapshot *, uint64_t id);
|
||||
/* side 0/1 stereo, 2 mono (gain only). */
|
||||
float xtc_mix_level(const xtc_mix_snapshot *, size_t track, uint64_t sample, unsigned int side);
|
||||
#endif
|
||||
81
src/mixer/model.c
Normal file
81
src/mixer/model.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include "../project/project_internal.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
static size_t find_track(const xtc_project *p, uint64_t id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < p->track_count; ++i)
|
||||
if (p->tracks[i].id == id)
|
||||
return i;
|
||||
return SIZE_MAX;
|
||||
}
|
||||
xtc_result xtc_project_set_mix(xtc_project *p, uint64_t id, float gain, float pan, int mute,
|
||||
int solo)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || !isfinite(gain) || gain < 0 || gain > 4 || !isfinite(pan) || pan < -1 || pan > 1 ||
|
||||
(mute != 0 && mute != 1) || (solo != 0 && solo != 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
i = find_track(p, id);
|
||||
if (i == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
p->tracks[i].gain = gain;
|
||||
p->tracks[i].pan = pan;
|
||||
p->tracks[i].mute = mute;
|
||||
p->tracks[i].solo = solo;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_set_automation(xtc_project *p, const xtc_automation_point *a)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || !a || !isfinite(a->value) || a->tick > XTC_PROJECT_MAX_TICK ||
|
||||
(a->parameter != XTC_AUTOMATION_GAIN && a->parameter != XTC_AUTOMATION_PAN) ||
|
||||
a->value < (a->parameter == XTC_AUTOMATION_GAIN ? 0 : -1) ||
|
||||
a->value > (a->parameter == XTC_AUTOMATION_GAIN ? 4 : 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (find_track(p, a->track_id) == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
for (i = 0; i < p->automation_count; ++i)
|
||||
if (p->automation[i].track_id == a->track_id &&
|
||||
p->automation[i].parameter == a->parameter && p->automation[i].tick == a->tick)
|
||||
break;
|
||||
if (i == XTC_MAX_AUTOMATION_POINTS)
|
||||
return XTC_ERR_CAPACITY;
|
||||
if (i == p->automation_count)
|
||||
++p->automation_count;
|
||||
p->automation[i] = *a;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_remove_automation(xtc_project *p, uint64_t id,
|
||||
xtc_automation_parameter param, uint64_t tick)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || (param != XTC_AUTOMATION_GAIN && param != XTC_AUTOMATION_PAN) ||
|
||||
tick > XTC_PROJECT_MAX_TICK)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < p->automation_count; ++i)
|
||||
if (p->automation[i].track_id == id && p->automation[i].parameter == param &&
|
||||
p->automation[i].tick == tick)
|
||||
break;
|
||||
if (i == p->automation_count)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
memmove(p->automation + i, p->automation + i + 1,
|
||||
(--p->automation_count - i) * sizeof(*p->automation));
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_automation_count(const xtc_project *p, size_t *n)
|
||||
{
|
||||
if (!p || !n)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
*n = p->automation_count;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_get_automation(const xtc_project *p, size_t i, xtc_automation_point *a)
|
||||
{
|
||||
if (!p || !a)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (i >= p->automation_count)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
*a = p->automation[i];
|
||||
return XTC_OK;
|
||||
}
|
||||
87
src/mixer/render.c
Normal file
87
src/mixer/render.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include "mix_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
size_t xtc_mix_track(const xtc_mix_snapshot *m, uint64_t id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < m->count; ++i)
|
||||
if (m->tracks[i].id == id)
|
||||
return i;
|
||||
return SIZE_MAX;
|
||||
}
|
||||
static int compare(const void *left, const void *right)
|
||||
{
|
||||
const xtc_mix_point *a = left, *b = right;
|
||||
if (a->track != b->track)
|
||||
return a->track < b->track ? -1 : 1;
|
||||
if (a->parameter != b->parameter)
|
||||
return a->parameter < b->parameter ? -1 : 1;
|
||||
return a->tick < b->tick ? -1 : a->tick != b->tick;
|
||||
}
|
||||
xtc_result xtc_mix_prepare(const xtc_project *p, unsigned int rate, xtc_mix_snapshot *m)
|
||||
{
|
||||
size_t i;
|
||||
xtc_result r;
|
||||
memset(m, 0, sizeof(*m));
|
||||
m->count = p->track_count;
|
||||
memcpy(m->tracks, p->tracks, p->track_count * sizeof(*m->tracks));
|
||||
for (i = 0; i < m->count; ++i)
|
||||
if (m->tracks[i].solo)
|
||||
m->any_solo = 1;
|
||||
for (i = 0; i < p->automation_count; ++i) {
|
||||
const xtc_automation_point *a = p->automation + i;
|
||||
xtc_mix_point *b = m->points + i;
|
||||
b->track = xtc_mix_track(m, a->track_id);
|
||||
b->parameter = (unsigned int)a->parameter;
|
||||
if (b->track == SIZE_MAX || b->parameter > 1)
|
||||
return XTC_ERR_INVALID_STATE;
|
||||
b->tick = a->tick;
|
||||
b->value = a->value;
|
||||
r = xtc_project_tick_to_sample(p, a->tick, rate, &b->sample);
|
||||
if (r != XTC_OK)
|
||||
return r;
|
||||
}
|
||||
qsort(m->points, p->automation_count, sizeof(*m->points), compare);
|
||||
for (i = 0; i < p->automation_count; ++i) {
|
||||
xtc_mix_point *a = m->points + i;
|
||||
size_t *n = &m->lengths[a->track][a->parameter];
|
||||
if (!*n)
|
||||
m->offsets[a->track][a->parameter] = i;
|
||||
++*n;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
static float value(const xtc_mix_snapshot *m, size_t track, unsigned int param, uint64_t sample)
|
||||
{
|
||||
size_t low = 0, high = m->lengths[track][param], count = high;
|
||||
const xtc_mix_point *p = m->points + m->offsets[track][param];
|
||||
if (!count)
|
||||
return param ? m->tracks[track].pan : m->tracks[track].gain;
|
||||
while (low < high) {
|
||||
size_t mid = low + (high - low) / 2;
|
||||
if (p[mid].sample <= sample)
|
||||
low = mid + 1;
|
||||
else
|
||||
high = mid;
|
||||
}
|
||||
if (!low)
|
||||
return p[0].value;
|
||||
if (low == count)
|
||||
return p[count - 1].value;
|
||||
return (float)(p[low - 1].value + ((double)sample - (double)p[low - 1].sample) /
|
||||
(double)(p[low].sample - p[low - 1].sample) *
|
||||
(p[low].value - p[low - 1].value));
|
||||
}
|
||||
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
|
||||
{
|
||||
float gain, pan;
|
||||
if (track >= m->count)
|
||||
return 0;
|
||||
if (m->tracks[track].mute || (m->any_solo && !m->tracks[track].solo))
|
||||
return 0;
|
||||
gain = value(m, track, 0, sample);
|
||||
if (side == 2)
|
||||
return gain;
|
||||
pan = value(m, track, 1, sample);
|
||||
return gain * (side == 0 ? (pan > 0 ? 1 - pan : 1) : (pan < 0 ? 1 + pan : 1));
|
||||
}
|
||||
|
|
@ -4,3 +4,7 @@ project_file.c implements bounded version-2 project metadata loading (including
|
|||
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.
|
||||
|
||||
Phase 8 writes format v4 with per-track MIX records and bounded AUTOMATION/POINT
|
||||
records, reading v1–v3 with unity/center/no-automation defaults. History stays
|
||||
session-only. See ../../docs/PROJECT_FORMAT.md.
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
size_t i, j;
|
||||
xtc_graph *graph = project->graph;
|
||||
READ("XTC_PROJECT", 2U); NUM(1U, version);
|
||||
if (version < 1U || version > 3U) return XTC_ERR_UNSUPPORTED;
|
||||
if (version < 1U || version > 4U) 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; }
|
||||
|
|
@ -124,6 +124,14 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
for (i = 0U; i < project->track_count; ++i) {
|
||||
READ("TRACK", 3U); NUM(1U, project->tracks[i].id);
|
||||
if (!decode_name(input->tokens[2], project->tracks[i].name)) return XTC_ERR_FORMAT;
|
||||
project->tracks[i].gain = 1;
|
||||
if (format_version >= 4U) {
|
||||
READ("MIX", 5U);
|
||||
if (!decode_float(input->tokens[1], &project->tracks[i].gain) ||
|
||||
!decode_float(input->tokens[2], &project->tracks[i].pan)) return XTC_ERR_FORMAT;
|
||||
NUM(3U, value); if (value > 1) return XTC_ERR_FORMAT; project->tracks[i].mute = (int)value;
|
||||
NUM(4U, value); if (value > 1) return XTC_ERR_FORMAT; project->tracks[i].solo = (int)value;
|
||||
}
|
||||
}
|
||||
READ("CLIPS", 2U); NUM(1U, value);
|
||||
if (value > XTC_PROJECT_MAX_CLIPS) return XTC_ERR_FORMAT;
|
||||
|
|
@ -167,6 +175,19 @@ static xtc_result read_project(reader *input, xtc_project *project)
|
|||
NUM(7U, value); if (value > 15) return XTC_ERR_FORMAT; n->channel = (unsigned int)value;
|
||||
}
|
||||
}
|
||||
if (format_version >= 4U) {
|
||||
READ("AUTOMATION", 2U); NUM(1U, value);
|
||||
if (value > XTC_MAX_AUTOMATION_POINTS) return XTC_ERR_FORMAT;
|
||||
project->automation_count = (size_t)value;
|
||||
for (i = 0; i < project->automation_count; ++i) {
|
||||
xtc_automation_point *a = project->automation + i;
|
||||
READ("POINT", 5U); NUM(1U, a->track_id); NUM(2U, value);
|
||||
if (value > 1) return XTC_ERR_FORMAT;
|
||||
a->parameter = (xtc_automation_parameter)value;
|
||||
NUM(3U, a->tick);
|
||||
if (!decode_float(input->tokens[4], &a->value)) return XTC_ERR_FORMAT;
|
||||
}
|
||||
}
|
||||
READ("END", 1U);
|
||||
if (fgetc(input->file) != EOF) return XTC_ERR_FORMAT;
|
||||
if (ferror(input->file)) return XTC_ERR_IO;
|
||||
|
|
@ -183,7 +204,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 3\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
|
||||
if (fprintf(file, "XTC_PROJECT 4\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;
|
||||
|
|
@ -197,8 +218,13 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
if (fprintf(file, "EDGE %zu %zu\n", graph->nodes[graph->edges[i].source].id,
|
||||
graph->nodes[graph->edges[i].destination].id) < 0) return XTC_ERR_IO;
|
||||
if (fprintf(file, "TRACKS %zu\n", project->track_count) < 0) return XTC_ERR_IO;
|
||||
for (i = 0U; i < project->track_count; ++i)
|
||||
if (fprintf(file, "TRACK %" PRIu64 " ", project->tracks[i].id) < 0 || !write_name(file, project->tracks[i].name)) return XTC_ERR_IO;
|
||||
for (i = 0U; i < project->track_count; ++i) {
|
||||
uint32_t gain, pan;
|
||||
const xtc_track_info *t = project->tracks + i;
|
||||
memcpy(&gain, &t->gain, sizeof(gain)); memcpy(&pan, &t->pan, sizeof(pan));
|
||||
if (fprintf(file, "TRACK %" PRIu64 " ", t->id) < 0 || !write_name(file, t->name) ||
|
||||
fprintf(file, "MIX %08" PRIx32 " %08" PRIx32 " %d %d\n", gain, pan, t->mute, t->solo) < 0) return XTC_ERR_IO;
|
||||
}
|
||||
if (fprintf(file, "CLIPS %zu\n", project->clip_count) < 0) return XTC_ERR_IO;
|
||||
for (i = 0U; i < project->clip_count; ++i) {
|
||||
const xtc_clip_info *clip = project->clips + i;
|
||||
|
|
@ -217,6 +243,13 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
|
|||
if (fprintf(file, "NOTE %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %u %u %u\n",
|
||||
n->id, n->clip_id, n->start_tick, n->length_tick, n->pitch, n->velocity, n->channel) < 0) return XTC_ERR_IO;
|
||||
}
|
||||
if (fprintf(file, "AUTOMATION %zu\n", project->automation_count) < 0) return XTC_ERR_IO;
|
||||
for (i = 0; i < project->automation_count; ++i) {
|
||||
uint32_t bits; const xtc_automation_point *a = project->automation + i;
|
||||
memcpy(&bits, &a->value, sizeof(bits));
|
||||
if (fprintf(file, "POINT %" PRIu64 " %u %" PRIu64 " %08" PRIx32 "\n",
|
||||
a->track_id, (unsigned int)a->parameter, a->tick, bits) < 0) return XTC_ERR_IO;
|
||||
}
|
||||
return fputs("END\n", file) == EOF ? XTC_ERR_IO : XTC_OK;
|
||||
}
|
||||
static size_t path_length(const char *path)
|
||||
|
|
|
|||
|
|
@ -4,3 +4,8 @@ project.c owns the opaque C project: graph, bounded tracks, silent/audio arrange
|
|||
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.
|
||||
|
||||
Phase 8 adds track mix settings, bounded automation and 32-edit C snapshot
|
||||
history in history.c. Checkout/accept separates candidate installation from
|
||||
history cursor movement; allocation IDs stay monotonic across history branches.
|
||||
See ../../docs/MIXER.md.
|
||||
|
|
|
|||
111
src/project/history.c
Normal file
111
src/project/history.c
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include "../graph/graph_internal.h"
|
||||
#include "project_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
struct xtc_history {
|
||||
xtc_project *states[XTC_HISTORY_EDITS + 1];
|
||||
size_t count, cursor;
|
||||
uint64_t next_track, next_clip, next_note;
|
||||
size_t next_node;
|
||||
};
|
||||
static void remember_ids(xtc_history *h, const xtc_project *p)
|
||||
{
|
||||
if (p->next_track_id > h->next_track)
|
||||
h->next_track = p->next_track_id;
|
||||
if (p->next_clip_id > h->next_clip)
|
||||
h->next_clip = p->next_clip_id;
|
||||
if (p->next_note_id > h->next_note)
|
||||
h->next_note = p->next_note_id;
|
||||
if (p->graph->next_id > h->next_node)
|
||||
h->next_node = p->graph->next_id;
|
||||
}
|
||||
void xtc_history_destroy(xtc_history *h)
|
||||
{
|
||||
size_t i;
|
||||
if (!h)
|
||||
return;
|
||||
for (i = 0; i < h->count; ++i)
|
||||
xtc_project_destroy(h->states[i]);
|
||||
free(h);
|
||||
}
|
||||
xtc_result xtc_history_create(const xtc_project *p, xtc_history **out)
|
||||
{
|
||||
xtc_history *h;
|
||||
xtc_result r;
|
||||
if (!out)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out = NULL;
|
||||
if (!p)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
h = calloc(1, sizeof(*h));
|
||||
if (!h)
|
||||
return XTC_ERR_OUT_OF_MEMORY;
|
||||
r = xtc_project_clone(p, h->states);
|
||||
if (r != XTC_OK) {
|
||||
free(h);
|
||||
return r;
|
||||
}
|
||||
remember_ids(h, p);
|
||||
h->count = 1;
|
||||
*out = h;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_history_record(xtc_history *h, const xtc_project *p)
|
||||
{
|
||||
xtc_project *copy;
|
||||
size_t i;
|
||||
xtc_result r;
|
||||
if (!h || !p)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
r = xtc_project_clone(p, ©);
|
||||
if (r != XTC_OK)
|
||||
return r;
|
||||
remember_ids(h, p);
|
||||
for (i = h->cursor + 1; i < h->count; ++i)
|
||||
xtc_project_destroy(h->states[i]);
|
||||
h->count = h->cursor + 1;
|
||||
if (h->count == XTC_HISTORY_EDITS + 1) {
|
||||
xtc_project_destroy(h->states[0]);
|
||||
memmove(h->states, h->states + 1, XTC_HISTORY_EDITS * sizeof(*h->states));
|
||||
--h->count;
|
||||
}
|
||||
h->states[h->count++] = copy;
|
||||
h->cursor = h->count - 1;
|
||||
return XTC_OK;
|
||||
}
|
||||
static int available(const xtc_history *h, int redo)
|
||||
{
|
||||
return redo ? h->cursor + 1 < h->count : h->cursor > 0;
|
||||
}
|
||||
xtc_result xtc_history_checkout(const xtc_history *h, int redo, xtc_project **out)
|
||||
{
|
||||
if (!out)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
*out = NULL;
|
||||
if (!h || (redo != 0 && redo != 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!available(h, redo))
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
{
|
||||
xtc_result r = xtc_project_clone(h->states[redo ? h->cursor + 1 : h->cursor - 1], out);
|
||||
if (r == XTC_OK) {
|
||||
(*out)->next_track_id = h->next_track;
|
||||
(*out)->next_clip_id = h->next_clip;
|
||||
(*out)->next_note_id = h->next_note;
|
||||
(*out)->graph->next_id = h->next_node;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
xtc_result xtc_history_accept(xtc_history *h, int redo)
|
||||
{
|
||||
if (!h || (redo != 0 && redo != 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!available(h, redo))
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
if (redo)
|
||||
++h->cursor;
|
||||
else
|
||||
--h->cursor;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
#include "xtc/xtc_audio.h"
|
||||
#include "../graph/graph_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
int xtc_project_valid_name(const char *name)
|
||||
|
|
@ -127,7 +128,7 @@ xtc_result xtc_project_add_track(xtc_project *project, const char *name, uint64_
|
|||
xtc_track_info track = {0};
|
||||
if (project == NULL || id == NULL || !xtc_project_valid_name(name)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (project->track_count == XTC_PROJECT_MAX_TRACKS || project->next_track_id == UINT64_MAX) return XTC_ERR_CAPACITY;
|
||||
track.id = project->next_track_id++;
|
||||
track.id = project->next_track_id++; track.gain = 1.0f;
|
||||
memcpy(track.name, name, strlen(name) + 1U);
|
||||
project->tracks[project->track_count++] = track; *id = track.id; return XTC_OK;
|
||||
}
|
||||
|
|
@ -146,7 +147,11 @@ xtc_result xtc_project_remove_track(xtc_project *project, uint64_t id)
|
|||
count = 0;
|
||||
for (i = 0; i < project->note_count; ++i)
|
||||
if (clip_index(project, project->notes[i].clip_id) != SIZE_MAX) project->notes[count++] = project->notes[i];
|
||||
project->note_count = count; return XTC_OK;
|
||||
project->note_count = count;
|
||||
count = 0;
|
||||
for (i = 0; i < project->automation_count; ++i)
|
||||
if (project->automation[i].track_id != id) project->automation[count++] = project->automation[i];
|
||||
project->automation_count = count; return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_add_clip(xtc_project *project, uint64_t track, const char *name,
|
||||
uint64_t start, uint64_t length, uint64_t *id)
|
||||
|
|
@ -246,6 +251,21 @@ xtc_result xtc_project_validate(const xtc_project *project)
|
|||
n->length_tick > project->clips[c].source_length_tick - n->start_tick) return XTC_ERR_FORMAT;
|
||||
for (j = 0; j < i; ++j) if (project->notes[j].id == n->id) return XTC_ERR_FORMAT;
|
||||
}
|
||||
if (project->automation_count > XTC_MAX_AUTOMATION_POINTS) return XTC_ERR_FORMAT;
|
||||
for (i = 0; i < project->track_count; ++i) {
|
||||
const xtc_track_info *t = project->tracks + i;
|
||||
if (!isfinite(t->gain) || t->gain < 0 || t->gain > 4 || !isfinite(t->pan) || t->pan < -1 || t->pan > 1 ||
|
||||
(t->mute != 0 && t->mute != 1) || (t->solo != 0 && t->solo != 1)) return XTC_ERR_FORMAT;
|
||||
}
|
||||
for (i = 0; i < project->automation_count; ++i) {
|
||||
const xtc_automation_point *a = project->automation + i;
|
||||
if (track_index(project, a->track_id) == SIZE_MAX || a->tick > XTC_PROJECT_MAX_TICK ||
|
||||
!isfinite(a->value) || (a->parameter != XTC_AUTOMATION_GAIN && a->parameter != XTC_AUTOMATION_PAN) ||
|
||||
a->value < (a->parameter == XTC_AUTOMATION_GAIN ? 0 : -1) ||
|
||||
a->value > (a->parameter == XTC_AUTOMATION_GAIN ? 4 : 1)) return XTC_ERR_FORMAT;
|
||||
for (j = 0; j < i; ++j) if (project->automation[j].track_id == a->track_id &&
|
||||
project->automation[j].parameter == a->parameter && project->automation[j].tick == a->tick) return XTC_ERR_FORMAT;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define XTC_PROJECT_INTERNAL_H
|
||||
#include "xtc/xtc_project.h"
|
||||
#include "xtc/xtc_midi.h"
|
||||
#include "xtc/xtc_mixer.h"
|
||||
struct xtc_project {
|
||||
xtc_graph *graph;
|
||||
uint64_t next_track_id, next_clip_id;
|
||||
|
|
@ -9,6 +10,8 @@ struct xtc_project {
|
|||
size_t track_count, clip_count;
|
||||
uint64_t next_note_id;
|
||||
size_t note_count;
|
||||
size_t automation_count;
|
||||
xtc_automation_point automation[XTC_MAX_AUTOMATION_POINTS];
|
||||
xtc_note_info notes[XTC_PROJECT_MAX_NOTES];
|
||||
xtc_track_info tracks[XTC_PROJECT_MAX_TRACKS];
|
||||
xtc_clip_info clips[XTC_PROJECT_MAX_CLIPS];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue