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
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue