Phase 7: Add MIDI clips, instrument and piano roll
This commit is contained in:
parent
8a37090b81
commit
cacc086805
38 changed files with 1373 additions and 74 deletions
|
|
@ -5,15 +5,18 @@
|
|||
|
||||
#ifdef XTC_HAVE_JACK
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct xtc_backend {
|
||||
jack_client_t *client;
|
||||
jack_port_t *input, *output;
|
||||
jack_port_t *input, *output, *midi_input;
|
||||
xtc_realtime *engine;
|
||||
xtc_audio_plan *audio;
|
||||
xtc_midi_plan *midi;
|
||||
unsigned int panic_requested, panic_seen, midi_dropped;
|
||||
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
|
||||
float *timeline;
|
||||
float *timeline, *instrument;
|
||||
size_t max_frames;
|
||||
uint64_t sample_position;
|
||||
unsigned int expected_rate;
|
||||
|
|
@ -22,12 +25,46 @@ struct xtc_backend {
|
|||
int ready, running; /* Control thread only. */
|
||||
};
|
||||
|
||||
static xtc_result render_midi(xtc_backend *backend, uint64_t position, jack_nframes_t frames)
|
||||
{
|
||||
xtc_midi_event events[XTC_MIDI_MAX_EVENTS];
|
||||
size_t count = 0;
|
||||
uint32_t i, total;
|
||||
unsigned int requested = __atomic_load_n(&backend->panic_requested, __ATOMIC_ACQUIRE);
|
||||
void *buffer;
|
||||
if (!backend->midi) return XTC_OK;
|
||||
if (requested != backend->panic_seen) {
|
||||
xtc_midi_plan_panic(backend->midi, position); backend->panic_seen = requested;
|
||||
}
|
||||
buffer = jack_port_get_buffer(backend->midi_input, frames);
|
||||
total = buffer ? jack_midi_get_event_count(buffer) : 0;
|
||||
if (!buffer || total > XTC_MIDI_MAX_EVENTS) {
|
||||
(void)__atomic_fetch_add(&backend->midi_dropped, total ? total : 1U, __ATOMIC_RELAXED);
|
||||
xtc_midi_plan_panic(backend->midi, position);
|
||||
} else for (i = 0; i < total; ++i) {
|
||||
jack_midi_event_t raw;
|
||||
xtc_result result;
|
||||
if (jack_midi_event_get(&raw, buffer, i) != 0 || raw.time >= frames) {
|
||||
(void)__atomic_fetch_add(&backend->midi_dropped, 1U, __ATOMIC_RELAXED);
|
||||
xtc_midi_plan_panic(backend->midi, position); count = 0; break;
|
||||
}
|
||||
result = xtc_midi_decode(raw.buffer, raw.size, (unsigned int)raw.time, events + count);
|
||||
if (result == XTC_ERR_UNSUPPORTED) continue;
|
||||
if (result != XTC_OK || (count && events[count].offset < events[count - 1].offset)) {
|
||||
(void)__atomic_fetch_add(&backend->midi_dropped, 1U, __ATOMIC_RELAXED);
|
||||
xtc_midi_plan_panic(backend->midi, position); count = 0; break;
|
||||
}
|
||||
++count;
|
||||
}
|
||||
return xtc_midi_plan_render(backend->midi, position, events, count, backend->instrument, (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);
|
||||
xtc_result result = XTC_OK;
|
||||
if (output == NULL) {
|
||||
(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);
|
||||
return 0;
|
||||
|
|
@ -43,9 +80,11 @@ static int process(jack_nframes_t frames, void *context)
|
|||
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 = render_midi(backend, position, frames);
|
||||
if (result == XTC_OK)
|
||||
result = xtc_realtime_process_audio(backend->engine, input,
|
||||
backend->audio ? backend->timeline : NULL, output, (size_t)frames);
|
||||
result = xtc_realtime_process_sources(backend->engine, input,
|
||||
backend->audio ? backend->timeline : NULL,
|
||||
backend->midi ? backend->instrument : NULL, output, (size_t)frames);
|
||||
}
|
||||
if (result == XTC_OK) {
|
||||
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
|
||||
|
|
@ -53,6 +92,7 @@ static int process(jack_nframes_t frames, void *context)
|
|||
else __atomic_store_n(&backend->sample_position, position + frames, __ATOMIC_RELAXED);
|
||||
}
|
||||
if (result != XTC_OK) {
|
||||
(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));
|
||||
}
|
||||
|
|
@ -64,6 +104,13 @@ static void shutdown_callback(void *context)
|
|||
xtc_backend *backend = context;
|
||||
__atomic_store_n(&backend->lost, 1U, __ATOMIC_RELEASE);
|
||||
}
|
||||
static void port_connection(jack_port_id_t source, jack_port_id_t destination, int connected, void *context)
|
||||
{
|
||||
xtc_backend *backend = context;
|
||||
if (!connected && (jack_port_by_id(backend->client, source) == backend->midi_input ||
|
||||
jack_port_by_id(backend->client, destination) == backend->midi_input))
|
||||
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
|
||||
}
|
||||
static int rate_callback(jack_nframes_t rate, void *context)
|
||||
{
|
||||
xtc_backend *backend = context;
|
||||
|
|
@ -82,6 +129,7 @@ static int xrun_callback(void *context)
|
|||
{
|
||||
xtc_backend *backend = context;
|
||||
(void)__atomic_fetch_add(&backend->xruns, 1U, __ATOMIC_RELAXED);
|
||||
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -101,10 +149,13 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
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->instrument = calloc(config->max_frames, sizeof(float));
|
||||
if (!backend->timeline || !backend->instrument) {
|
||||
free(backend->timeline); free(backend->instrument); 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->timeline); free(backend); return XTC_ERR_IO; }
|
||||
if (backend->client == NULL) { free(backend->instrument); 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);
|
||||
|
|
@ -113,8 +164,10 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
if (result != XTC_OK) goto fail;
|
||||
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);
|
||||
if (backend->input == NULL || backend->output == NULL) { result = XTC_ERR_IO; goto fail; }
|
||||
if (jack_set_process_callback(backend->client, process, backend) != 0 ||
|
||||
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 (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 ||
|
||||
jack_set_buffer_size_callback(backend->client, buffer_callback, backend) != 0 ||
|
||||
jack_set_xrun_callback(backend->client, xrun_callback, backend) != 0) {
|
||||
|
|
@ -134,6 +187,8 @@ fail:
|
|||
}
|
||||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
xtc_midi_plan_destroy(backend->midi);
|
||||
free(backend->instrument);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
return result;
|
||||
|
|
@ -146,6 +201,8 @@ xtc_result xtc_backend_close(xtc_backend *backend)
|
|||
if (jack_client_close(backend->client) != 0) return XTC_ERR_IO;
|
||||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
xtc_midi_plan_destroy(backend->midi);
|
||||
free(backend->instrument);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
return XTC_OK;
|
||||
|
|
@ -157,6 +214,8 @@ xtc_result xtc_backend_start(xtc_backend *backend)
|
|||
if (__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
|
||||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE)) return XTC_ERR_INVALID_STATE;
|
||||
if (backend->running) return XTC_ERR_INVALID_STATE;
|
||||
xtc_midi_plan_reset(backend->midi);
|
||||
backend->panic_seen = __atomic_load_n(&backend->panic_requested, __ATOMIC_ACQUIRE);
|
||||
if (jack_activate(backend->client) != 0) return XTC_ERR_IO;
|
||||
backend->running = 1;
|
||||
return XTC_OK;
|
||||
|
|
@ -165,9 +224,10 @@ xtc_result xtc_backend_stop(xtc_backend *backend)
|
|||
{
|
||||
if (backend == NULL) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready) return XTC_ERR_INVALID_STATE;
|
||||
if (!backend->running) return XTC_OK;
|
||||
if (!backend->running) { xtc_midi_plan_reset(backend->midi); return XTC_OK; }
|
||||
if (jack_deactivate(backend->client) != 0) return XTC_ERR_IO;
|
||||
backend->running = 0;
|
||||
xtc_midi_plan_reset(backend->midi);
|
||||
xtc_realtime_collect(backend->engine);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -177,6 +237,7 @@ xtc_result xtc_backend_seek(xtc_backend *backend, uint64_t sample)
|
|||
if (!backend->ready || backend->running ||
|
||||
__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
|
||||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE)) return XTC_ERR_INVALID_STATE;
|
||||
xtc_midi_plan_reset(backend->midi);
|
||||
__atomic_store_n(&backend->sample_position, sample, __ATOMIC_RELAXED);
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -204,13 +265,16 @@ xtc_result xtc_backend_submit(xtc_backend *backend, const xtc_graph *graph)
|
|||
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project)
|
||||
{
|
||||
xtc_audio_plan *candidate = NULL;
|
||||
xtc_midi_plan *midi = 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_midi_plan_create(project, backend->expected_rate, &midi);
|
||||
if (result == XTC_OK) result = xtc_backend_submit(backend, xtc_project_graph(project));
|
||||
if (result != XTC_OK) { xtc_audio_plan_destroy(candidate); return result; }
|
||||
if (result != XTC_OK) { xtc_audio_plan_destroy(candidate); xtc_midi_plan_destroy(midi); return result; }
|
||||
xtc_audio_plan_destroy(backend->audio); backend->audio = candidate;
|
||||
xtc_midi_plan_destroy(backend->midi); backend->midi = midi;
|
||||
return XTC_OK;
|
||||
}
|
||||
const char *xtc_backend_asset_issue(const xtc_backend *backend)
|
||||
|
|
@ -228,6 +292,20 @@ xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder
|
|||
backend->recorder = recorder;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_backend_panic(xtc_backend *backend)
|
||||
{
|
||||
if (!backend) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready) return XTC_ERR_INVALID_STATE;
|
||||
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
|
||||
if (!backend->running) xtc_midi_plan_reset(backend->midi);
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_backend_midi_port_name(const xtc_backend *backend, const char **out)
|
||||
{
|
||||
if (!backend || !out) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready) return XTC_ERR_INVALID_STATE;
|
||||
*out = jack_port_name(backend->midi_input); 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)
|
||||
|
|
@ -236,6 +314,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->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);
|
||||
out_status->buffer_frames = __atomic_load_n(&backend->frames, __ATOMIC_RELAXED);
|
||||
|
|
@ -297,6 +376,10 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
|
|||
{ return route(backend, direction, remote, 1); }
|
||||
|
||||
#else
|
||||
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)
|
||||
{ (void)backend; (void)out; return XTC_ERR_UNSUPPORTED; }
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue