Phase 32: add MIDI track CLAP, VST2 and VST3 instruments
This commit is contained in:
parent
9e0e4562d7
commit
fe484537f9
41 changed files with 1138 additions and 65 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#include "xtc/xtc_midi.h"
|
||||
#include "xtc/xtc_dsp.h"
|
||||
#include "xtc/xtc_plugin.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -33,6 +34,11 @@ struct xtc_midi_plan {
|
|||
unsigned char control_map[16][3];
|
||||
xtc_mix_snapshot mix;
|
||||
span notes[XTC_PROJECT_MAX_NOTES];
|
||||
xtc_plugin *plugins[XTC_PROJECT_MAX_TRACKS];
|
||||
size_t live_track;
|
||||
unsigned int plugin_reset;
|
||||
float scratch[256];
|
||||
xtc_midi_event queue[256];
|
||||
size_t count;
|
||||
voice voices[XTC_MIDI_VOICES];
|
||||
double step[128];
|
||||
|
|
@ -96,6 +102,7 @@ void xtc_midi_plan_reset(xtc_midi_plan *p)
|
|||
memset(p->cutoff, 0, sizeof(p->cutoff));
|
||||
p->next_position = UINT64_MAX;
|
||||
p->age = 0;
|
||||
p->plugin_reset = 1;
|
||||
{ unsigned int c; for (c = 0; c < 16; ++c) {
|
||||
p->sustain[c] = 0;
|
||||
(void)xtc_gain_ramp_reset(p->volume+c,1);
|
||||
|
|
@ -189,6 +196,15 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
}
|
||||
}
|
||||
free(points);
|
||||
p->live_track = SIZE_MAX;
|
||||
for (i = 0; i < p->mix.count; ++i) {
|
||||
xtc_plugin_spec spec;
|
||||
if (xtc_project_get_instrument(project,p->mix.tracks[i].id,&spec) == XTC_OK) {
|
||||
xtc_result r = xtc_plugin_create(&spec,rate,256,&p->plugins[i]);
|
||||
if (r != XTC_OK) { xtc_midi_plan_destroy(p); return r; }
|
||||
}
|
||||
if (project->live_instrument == p->mix.tracks[i].id) p->live_track = i;
|
||||
}
|
||||
memcpy(p->control_map,project->control_map,sizeof(p->control_map));
|
||||
p->controller_frames = rate / 200U;
|
||||
if (!p->controller_frames) p->controller_frames = 1;
|
||||
|
|
@ -204,6 +220,9 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
*/
|
||||
void xtc_midi_plan_destroy(xtc_midi_plan *p)
|
||||
{
|
||||
size_t i;
|
||||
if (!p) return;
|
||||
for (i = 0; i < XTC_PROJECT_MAX_TRACKS; ++i) xtc_plugin_destroy(p->plugins[i]);
|
||||
free(p);
|
||||
}
|
||||
|
||||
|
|
@ -238,6 +257,7 @@ void xtc_midi_plan_panic(xtc_midi_plan *p, uint64_t position)
|
|||
for (c = 0; c < 16; ++c)
|
||||
channel_off(p, c, position);
|
||||
p->next_position = position;
|
||||
p->plugin_reset = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -324,7 +344,7 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
|
|||
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 == 0.0 || s->start < p->cutoff[s->channel] || end <= begin)
|
||||
if ((s->track < p->mix.count && p->plugins[s->track]) || 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);
|
||||
|
|
@ -371,6 +391,64 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
|
|||
}
|
||||
}
|
||||
|
||||
/* Stable insertion keeps resets, scheduled offs/ons and live events ordered.
|
||||
* All storage belongs to the prepared plan; callback work is strictly bounded. */
|
||||
static int enqueue(xtc_midi_plan *p, size_t *count, xtc_midi_event e)
|
||||
{
|
||||
size_t i = *count;
|
||||
if (i == 256) return 0;
|
||||
while (i && p->queue[i-1].offset > e.offset) { p->queue[i] = p->queue[i-1]; --i; }
|
||||
p->queue[i] = e; ++*count; return 1;
|
||||
}
|
||||
static xtc_result instruments(xtc_midi_plan *p, uint64_t position, const xtc_midi_event *events,
|
||||
size_t count, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t track, offset, i, j;
|
||||
for (track = 0; track < p->mix.count; ++track) {
|
||||
if (!p->plugins[track]) continue;
|
||||
for (offset = 0; offset < frames; offset += 256) {
|
||||
size_t n = frames-offset < 256 ? frames-offset : 256, queued = 0;
|
||||
uint64_t begin = position+offset, end = begin+n;
|
||||
xtc_result r;
|
||||
if (!offset && p->plugin_reset) {
|
||||
unsigned int c;
|
||||
for (c = 0; c < 16; ++c)
|
||||
if (!enqueue(p,&queued,(xtc_midi_event){0,XTC_MIDI_ALL_OFF,c,0,0})) return XTC_ERR_CAPACITY;
|
||||
}
|
||||
/* End before start at equal offsets, including retriggered keys. */
|
||||
for (i = 0; i < p->count; ++i) {
|
||||
const span *s = p->notes+i;
|
||||
if (s->track == track && s->end >= begin && s->end < end)
|
||||
if (!enqueue(p,&queued,(xtc_midi_event){(unsigned int)(s->end-begin),XTC_MIDI_NOTE_OFF,s->channel,s->pitch,0})) return XTC_ERR_CAPACITY;
|
||||
}
|
||||
for (i = 0; i < p->count; ++i) {
|
||||
const span *s = p->notes+i;
|
||||
int chase = !offset && p->plugin_reset && s->start < begin && s->end > begin;
|
||||
if (s->track == track && s->start >= p->cutoff[s->channel] &&
|
||||
((s->start >= begin && s->start < end) || chase))
|
||||
if (!enqueue(p,&queued,(xtc_midi_event){chase ? 0U : (unsigned int)(s->start-begin),XTC_MIDI_NOTE_ON,s->channel,s->pitch,s->velocity})) return XTC_ERR_CAPACITY;
|
||||
}
|
||||
if (track == p->live_track) for (i = 0; i < count; ++i) {
|
||||
xtc_midi_event e = events[i];
|
||||
if (e.offset >= offset && e.offset < offset+n) {
|
||||
e.offset -= (unsigned int)offset;
|
||||
if (!enqueue(p,&queued,e)) return XTC_ERR_CAPACITY;
|
||||
}
|
||||
}
|
||||
r = xtc_plugin_process_midi(p->plugins[track],p->queue,queued,p->scratch,n);
|
||||
if (r != XTC_OK) return r;
|
||||
for (j = 0; j < n; ++j) {
|
||||
float sample = p->scratch[j];
|
||||
if (p->monitor) xtc_mix_monitor(&p->mix,p->monitor,p->monitor_stride,track,begin+j,offset+j,sample,right != NULL);
|
||||
out[offset+j] += sample*xtc_mix_level(&p->mix,track,begin+j,right ? 0U : 2U);
|
||||
if (right) right[offset+j] += sample*xtc_mix_level(&p->mix,track,begin+j,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
p->plugin_reset = 0;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Renders scheduled and live MIDI with sample-offset events and track gain and pan.
|
||||
*
|
||||
|
|
@ -408,10 +486,18 @@ xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position, cons
|
|||
for (i = 0; i < count; ++i) {
|
||||
size_t next = events[i].offset;
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, next - cursor);
|
||||
event(p, events + i, position + next);
|
||||
if (p->live_track == SIZE_MAX) event(p, events + i, position + next);
|
||||
cursor = next;
|
||||
}
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, frames - cursor);
|
||||
{
|
||||
xtc_result r = instruments(p,position,events,count,out,right,frames);
|
||||
if (r != XTC_OK) {
|
||||
memset(out,0,frames*sizeof(*out));
|
||||
if (right) memset(right,0,frames*sizeof(*right));
|
||||
xtc_midi_plan_reset(p); return r;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < frames; ++i) {
|
||||
out[i] = xtc_dsp_finite(out[i]);
|
||||
if (right) right[i] = xtc_dsp_finite(right[i]);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,27 @@
|
|||
#include "../graph/graph_internal.h"
|
||||
#include <string.h>
|
||||
|
||||
xtc_result xtc_project_ensure_midi_route(xtc_project *c)
|
||||
{
|
||||
size_t i, source = SIZE_MAX, output = SIZE_MAX;
|
||||
xtc_result r = XTC_OK;
|
||||
for (i = 0; i < c->graph->node_count; ++i) {
|
||||
xtc_node *n = c->graph->nodes + i;
|
||||
if (n->type == XTC_NODE_INSTRUMENT)
|
||||
source = n->id;
|
||||
if (n->type == XTC_NODE_OUTPUT)
|
||||
output = n->id;
|
||||
}
|
||||
if (source == SIZE_MAX) {
|
||||
r = xtc_graph_add_instrument(c->graph, &source);
|
||||
if (r == XTC_OK && output == SIZE_MAX)
|
||||
r = xtc_graph_add_output(c->graph, &output);
|
||||
if (r == XTC_OK)
|
||||
r = xtc_graph_connect(c->graph, source, output);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds a MIDI clip and ensures an instrument output route exists.
|
||||
*
|
||||
|
|
@ -16,7 +37,6 @@ xtc_result xtc_project_add_midi_clip(xtc_project *p, uint64_t track, uint64_t st
|
|||
{
|
||||
xtc_project *c = NULL;
|
||||
xtc_result r;
|
||||
size_t i, source = SIZE_MAX, output = SIZE_MAX;
|
||||
uint64_t id;
|
||||
if (!p || !out)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
|
|
@ -25,20 +45,7 @@ xtc_result xtc_project_add_midi_clip(xtc_project *p, uint64_t track, uint64_t st
|
|||
r = xtc_project_add_clip(c, track, "MIDI", start, length, &id);
|
||||
if (r == XTC_OK) {
|
||||
c->clips[c->clip_count - 1].midi = 1;
|
||||
for (i = 0; i < c->graph->node_count; ++i) {
|
||||
xtc_node *n = c->graph->nodes + i;
|
||||
if (n->type == XTC_NODE_INSTRUMENT)
|
||||
source = n->id;
|
||||
if (n->type == XTC_NODE_OUTPUT)
|
||||
output = n->id;
|
||||
}
|
||||
if (source == SIZE_MAX) {
|
||||
r = xtc_graph_add_instrument(c->graph, &source);
|
||||
if (r == XTC_OK && output == SIZE_MAX)
|
||||
r = xtc_graph_add_output(c->graph, &output);
|
||||
if (r == XTC_OK)
|
||||
r = xtc_graph_connect(c->graph, source, output);
|
||||
}
|
||||
r = xtc_project_ensure_midi_route(c);
|
||||
}
|
||||
if (r == XTC_OK) {
|
||||
xtc_graph *old = p->graph;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue