Phase 7: Add MIDI clips, instrument and piano roll

This commit is contained in:
Johannes Findeisen 2026-10-27 00:53:41 +01:00
commit cacc086805
38 changed files with 1373 additions and 74 deletions

View file

@ -2,6 +2,7 @@
#define XTC_BACKEND_H
#include "xtc_engine.h"
#include "xtc_audio.h"
#include "xtc_midi.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -19,6 +20,7 @@ typedef struct {
unsigned int buffer_frames;
unsigned int xruns; /* Wraps modulo UINT_MAX + 1. */
unsigned int processed_blocks;
unsigned int midi_dropped; /* Malformed/overflowed input; unsigned wrap. */
unsigned int asset_issues; /* Unique missing/unreadable/unsupported assets. */
uint64_t sample_position; /* Successful callback frames; retained across stop/submit. */
unsigned int submitted_revision, active_revision;
@ -52,6 +54,10 @@ 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);
const char *xtc_backend_asset_issue(const xtc_backend *backend);
/* Borrowed recorder, set/detach only while stopped. Stop before finish/destroy. */
/* Control-thread panic is delivered at a block boundary via lock-free counter.
* Stops, seeks, xruns and event overflow also clear instrument voices. */
xtc_result xtc_backend_panic(xtc_backend *backend);
xtc_result xtc_backend_midi_port_name(const xtc_backend *backend, const char **out_name);
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder);
void xtc_backend_collect(xtc_backend *backend);
/* Snapshot fields are individually coherent, not a transactional snapshot. */

View file

@ -33,7 +33,8 @@ typedef enum {
XTC_NODE_OSCILLATOR,
XTC_NODE_METER,
XTC_NODE_OUTPUT,
XTC_NODE_AUDIO /* Timeline mix source, output only. */
XTC_NODE_AUDIO, /* Timeline mix source, output only. */
XTC_NODE_INSTRUMENT /* Internal MIDI instrument, output only. */
} xtc_node_type;
typedef struct {
@ -81,7 +82,7 @@ void xtc_graph_destroy(xtc_graph *graph);
xtc_result xtc_graph_add_gain(xtc_graph *graph, float gain, size_t *out_node_id);
xtc_result xtc_graph_connect(xtc_graph *graph, size_t source_node,
size_t destination_node);
/* Port 0 is input; port 1 is output. Oscillators and audio sources have only output 1;
/* Port 0 is input; port 1 is output. Oscillators, audio sources and instruments have only output 1;
* output nodes have only input 0. All ports carry mono audio. */
xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
size_t port_id, xtc_port_info *out_port);
@ -91,6 +92,7 @@ xtc_result xtc_graph_connect_ports(xtc_graph *graph, size_t source_node,
xtc_result xtc_graph_add_oscillator(xtc_graph *graph, float frequency,
size_t *out_node_id);
xtc_result xtc_graph_add_meter(xtc_graph *graph, size_t *out_node_id);
xtc_result xtc_graph_add_instrument(xtc_graph *graph, size_t *out_node_id);
xtc_result xtc_graph_add_audio(xtc_graph *graph, size_t *out_node_id);
xtc_result xtc_graph_add_output(xtc_graph *graph, size_t *out_node_id);
/* Compile copies graph state; caller owns the plan. Sample rate must be
@ -106,6 +108,9 @@ void xtc_plan_destroy(xtc_plan *plan);
* Sums explicit output nodes; if absent, sums terminal nodes for compatibility
* unless require_output_nodes is set. Empty graphs produce silence.
* Unconnected gain/meter/output inputs receive external input. */
/* Separate mono source buffers; NULL is silence. Existing wrappers omit MIDI. */
xtc_result xtc_plan_process_sources(xtc_plan *plan, const float *input, const float *timeline,
const float *instrument, float *output, size_t frames);
/* timeline is a separate mono input for XTC_NODE_AUDIO, NULL means silence. */
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline,
float *output, size_t frames);

57
include/xtc/xtc_midi.h Normal file
View file

@ -0,0 +1,57 @@
#ifndef XTC_MIDI_H
#define XTC_MIDI_H
#include "xtc_project.h"
#ifdef __cplusplus
extern "C" {
#endif
#define XTC_PROJECT_MAX_NOTES 4096U
#define XTC_MIDI_MAX_EVENTS 256U
#define XTC_MIDI_VOICES 32U
typedef struct {
uint64_t id, clip_id, start_tick, length_tick; /* Positions in clip source. */
unsigned int pitch, velocity, channel; /* 0..127, 1..127, 0..15. */
} xtc_note_info;
typedef enum { XTC_MIDI_NOTE_ON = 1, XTC_MIDI_NOTE_OFF, XTC_MIDI_ALL_OFF } xtc_midi_type;
typedef struct {
unsigned int offset; /* Sample offset within THIS block, strictly < frames. */
xtc_midi_type type;
unsigned int channel, pitch, velocity;
} xtc_midi_event;
typedef struct xtc_midi_plan xtc_midi_plan;
/* Control-thread model APIs, stable note IDs; deletion cascades from clips/tracks.
* MIDI clips cannot reference audio. Creation transactionally adds an instrument
* source -> output route if no instrument exists. Other clip edits stay shared. */
xtc_result xtc_project_add_midi_clip(xtc_project *project, uint64_t track,
uint64_t start, uint64_t length, uint64_t *out_id);
xtc_result xtc_project_note_count(const xtc_project *project, size_t *out_count);
xtc_result xtc_project_get_note(const xtc_project *project, size_t index, xtc_note_info *out_note);
xtc_result xtc_project_add_note(xtc_project *project, uint64_t clip, uint64_t start,
uint64_t length, unsigned int pitch, unsigned int velocity, unsigned int channel, uint64_t *out_id);
xtc_result xtc_project_edit_note(xtc_project *project, uint64_t id, uint64_t start,
uint64_t length, unsigned int pitch, unsigned int velocity, unsigned int channel);
xtc_result xtc_project_remove_note(xtc_project *project, uint64_t id);
/* Complete 3-byte note on/off and CC120/123 only. Velocity-zero on -> off.
* Unsupported messages return UNSUPPORTED; malformed supported messages INVALID.
* Output unchanged on error. No running-status/SysEx/pedal/program/bend support. */
xtc_result xtc_midi_decode(const unsigned char *bytes, size_t size, unsigned int offset,
xtc_midi_event *out_event);
/* Control-thread compile; immutable ranges plus exclusively audio-owned runtime.
* Rejects >32 simultaneous scheduled notes. Separate 32 live voices steal oldest.
* Constant-tempo sine instrument, velocity *0.1, above-Nyquist notes silent. */
xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int sample_rate, xtc_midi_plan **out_plan);
void xtc_midi_plan_destroy(xtc_midi_plan *plan);
/* Audio owner or stopped only. Reset clears live notes and panic suppression.
* Panic silences live notes and scheduled notes already started at position;
* later scheduled notes can start. MIDI CC120/123 affects its own channel. */
void xtc_midi_plan_reset(xtc_midi_plan *plan);
void xtc_midi_plan_panic(xtc_midi_plan *plan, uint64_t position);
/* No allocation, I/O, locks or GUI. Events sorted by offset, equal offsets retain
* caller order, count<=256, frames<=65536. Invalid input leaves state/output alone.
* Renders [position,position+frames); discontinuity clears live voices, chases
* scheduled notes at their correct phase. Note-off is immediate (no envelope). */
xtc_result xtc_midi_plan_render(xtc_midi_plan *plan, uint64_t position,
const xtc_midi_event *events, size_t count, float *output, size_t frames);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -18,6 +18,7 @@ typedef struct {
uint64_t id, track_id, start_tick, length_tick, offset_tick, source_length_tick;
char name[XTC_PROJECT_NAME_BYTES];
char asset_path[XTC_ASSET_PATH_BYTES];
int midi; /* 0 silent/audio region, 1 MIDI clip; no asset path in MIDI. */
} xtc_clip_info;
/* Single control-thread ownership. Names: nonempty UTF-8, <=63 bytes, no ASCII controls.
* IDs are monotonic, never reused, UINT64_MAX reserved. Snapshots are copies. */

View file

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