Phase 16: persist sample-timed effect automation and add expressive MIDI controls
This commit is contained in:
parent
f72314b5a3
commit
7fac0a4f6a
23 changed files with 374 additions and 39 deletions
|
|
@ -19,13 +19,15 @@ typedef struct {
|
|||
} endpoint;
|
||||
|
||||
typedef struct {
|
||||
unsigned int active, channel, pitch, velocity;
|
||||
unsigned int active, channel, pitch, velocity, released;
|
||||
uint64_t age;
|
||||
double phase;
|
||||
} voice;
|
||||
|
||||
struct xtc_midi_plan {
|
||||
size_t fade_frames;
|
||||
size_t fade_frames, controller_frames;
|
||||
xtc_gain_ramp volume[16], pan[16];
|
||||
unsigned int sustain[16];
|
||||
xtc_mix_snapshot mix;
|
||||
span notes[XTC_PROJECT_MAX_NOTES];
|
||||
size_t count;
|
||||
|
|
@ -54,13 +56,13 @@ xtc_result xtc_midi_decode(const unsigned char *bytes, size_t size, unsigned int
|
|||
return XTC_ERR_UNSUPPORTED;
|
||||
if (size != 3 || bytes[1] > 127 || bytes[2] > 127)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (command == 176 && bytes[1] != 120 && bytes[1] != 123)
|
||||
if (command == 176 && bytes[1] != 120 && bytes[1] != 123 && bytes[1] != 7 && bytes[1] != 10 && bytes[1] != 64)
|
||||
return XTC_ERR_UNSUPPORTED;
|
||||
e.offset = offset;
|
||||
e.channel = bytes[0] & 15U;
|
||||
e.pitch = bytes[1];
|
||||
e.velocity = bytes[2];
|
||||
e.type = command == 176 ? XTC_MIDI_ALL_OFF : command == 128 || !e.velocity ? XTC_MIDI_NOTE_OFF : XTC_MIDI_NOTE_ON;
|
||||
e.type = command == 176 ? (bytes[1] == 120 || bytes[1] == 123 ? XTC_MIDI_ALL_OFF : XTC_MIDI_CONTROL) : command == 128 || !e.velocity ? XTC_MIDI_NOTE_OFF : XTC_MIDI_NOTE_ON;
|
||||
*out = e;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -93,6 +95,11 @@ void xtc_midi_plan_reset(xtc_midi_plan *p)
|
|||
memset(p->cutoff, 0, sizeof(p->cutoff));
|
||||
p->next_position = UINT64_MAX;
|
||||
p->age = 0;
|
||||
{ unsigned int c; for (c = 0; c < 16; ++c) {
|
||||
p->sustain[c] = 0;
|
||||
(void)xtc_gain_ramp_reset(p->volume+c,1);
|
||||
(void)xtc_gain_ramp_reset(p->pan+c,0);
|
||||
} }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -181,6 +188,8 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
}
|
||||
}
|
||||
free(points);
|
||||
p->controller_frames = rate / 200U;
|
||||
if (!p->controller_frames) p->controller_frames = 1;
|
||||
xtc_midi_plan_reset(p);
|
||||
*out = p;
|
||||
return XTC_OK;
|
||||
|
|
@ -209,6 +218,7 @@ static void channel_off(xtc_midi_plan *p, unsigned int channel, uint64_t positio
|
|||
for (i = 0; i < XTC_MIDI_VOICES; ++i)
|
||||
if (p->voices[i].channel == channel)
|
||||
p->voices[i].active = 0;
|
||||
p->sustain[channel] = 0;
|
||||
p->cutoff[channel] = position == UINT64_MAX ? position : position + 1;
|
||||
}
|
||||
|
||||
|
|
@ -242,6 +252,17 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
channel_off(p, e->channel, position);
|
||||
return;
|
||||
}
|
||||
if (e->type == XTC_MIDI_CONTROL) {
|
||||
if (e->pitch == 7) (void)xtc_gain_ramp_target(p->volume+e->channel,(float)e->velocity/127.0f,p->controller_frames);
|
||||
else if (e->pitch == 10) (void)xtc_gain_ramp_target(p->pan+e->channel,
|
||||
e->velocity >= 64 ? (float)(e->velocity-64)/63.0f : ((float)e->velocity-64)/64.0f,p->controller_frames);
|
||||
else if (e->pitch == 64) {
|
||||
p->sustain[e->channel] = e->velocity >= 64;
|
||||
if (!p->sustain[e->channel]) for (i = 0; i < XTC_MIDI_VOICES; ++i)
|
||||
if (p->voices[i].channel == e->channel && p->voices[i].released) p->voices[i].active = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < XTC_MIDI_VOICES; ++i) {
|
||||
if (p->voices[i].active && p->voices[i].channel == e->channel && p->voices[i].pitch == e->pitch) {
|
||||
slot = i;
|
||||
|
|
@ -249,8 +270,10 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
}
|
||||
}
|
||||
if (e->type == XTC_MIDI_NOTE_OFF) {
|
||||
if (slot != SIZE_MAX)
|
||||
p->voices[slot].active = 0;
|
||||
if (slot != SIZE_MAX) {
|
||||
if (p->sustain[e->channel]) p->voices[slot].released = 1;
|
||||
else p->voices[slot].active = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (slot == SIZE_MAX) {
|
||||
|
|
@ -267,6 +290,7 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
slot = i;
|
||||
}
|
||||
p->voices[slot].active = 1;
|
||||
p->voices[slot].released = 0;
|
||||
p->voices[slot].channel = e->channel;
|
||||
p->voices[slot].pitch = e->pitch;
|
||||
p->voices[slot].velocity = e->velocity;
|
||||
|
|
@ -315,19 +339,24 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
|
|||
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 == 0.0)
|
||||
continue;
|
||||
for (j = 0; j < frames; ++j) {
|
||||
float sample = (float)(sin(TAU * v->phase) * 0.1 * v->velocity / 127.0);
|
||||
out[j] += sample;
|
||||
if (right)
|
||||
right[j] += sample;
|
||||
for (j = 0; j < frames; ++j) {
|
||||
float levels[16], pans[16]; unsigned int c;
|
||||
for (c = 0; c < 16; ++c) {
|
||||
levels[c] = pans[c] = 1;
|
||||
(void)xtc_gain_ramp_process(p->volume+c,levels+c,NULL,1);
|
||||
(void)xtc_gain_ramp_process(p->pan+c,pans+c,NULL,1);
|
||||
}
|
||||
for (i = 0; i < XTC_MIDI_VOICES; ++i) {
|
||||
voice *v = p->voices+i;
|
||||
double step = p->step[v->pitch];
|
||||
float sample, pan;
|
||||
if (!v->active || step == 0) continue;
|
||||
sample = (float)(sin(TAU*v->phase)*.1*v->velocity/127.0) * levels[v->channel];
|
||||
pan = pans[v->channel];
|
||||
out[j] += sample * (right && pan > 0 ? 1-pan : 1);
|
||||
if (right) right[j] += sample * (pan < 0 ? 1+pan : 1);
|
||||
v->phase += step;
|
||||
if (v->phase >= 1)
|
||||
v->phase -= 1;
|
||||
if (v->phase >= 1) v->phase -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -354,7 +383,7 @@ xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position, cons
|
|||
for (i = 0; i < count; ++i) {
|
||||
const xtc_midi_event *e = events + i;
|
||||
if (e->offset >= frames || (i && e->offset < events[i - 1].offset) || e->type < XTC_MIDI_NOTE_ON ||
|
||||
e->type > XTC_MIDI_ALL_OFF || e->pitch > 127 || e->velocity > 127 || e->channel > 15 ||
|
||||
e->type > XTC_MIDI_CONTROL || e->pitch > 127 || e->velocity > 127 || e->channel > 15 ||
|
||||
(e->type == XTC_MIDI_NOTE_ON && !e->velocity))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue