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
|
|
@ -1,13 +1,14 @@
|
|||
#include "xtc/xtc_midi.h"
|
||||
#include "../project/project_internal.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define TAU 6.28318530717958647692
|
||||
typedef struct { uint64_t start, end, age; unsigned int pitch, velocity, channel; } span;
|
||||
typedef struct { uint64_t start, end, age; unsigned int pitch, velocity, channel; size_t track; } span;
|
||||
typedef struct { uint64_t time; int change; } endpoint;
|
||||
typedef struct { unsigned int active, channel, pitch, velocity; uint64_t age; double phase; } voice;
|
||||
struct xtc_midi_plan {
|
||||
xtc_mix_snapshot mix;
|
||||
span notes[XTC_PROJECT_MAX_NOTES];
|
||||
size_t count;
|
||||
voice voices[XTC_MIDI_VOICES];
|
||||
|
|
@ -51,6 +52,10 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
if (!project || !rate || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
|
||||
p = calloc(1, sizeof(*p)); points = calloc(XTC_PROJECT_MAX_NOTES * 2U, sizeof(*points));
|
||||
if (!p || !points) { free(p); free(points); return XTC_ERR_OUT_OF_MEMORY; }
|
||||
{
|
||||
xtc_result r = xtc_mix_prepare(project, rate, &p->mix);
|
||||
if (r != XTC_OK) { free(points); free(p); return r; }
|
||||
}
|
||||
for (i = 0; i < 128; ++i) {
|
||||
double frequency = 440.0 * pow(2.0, ((double)i - 69.0) / 12.0);
|
||||
p->step[i] = frequency < rate * 0.5 ? frequency / rate : 0;
|
||||
|
|
@ -71,6 +76,7 @@ xtc_result xtc_midi_plan_create(const xtc_project *project, unsigned int rate, x
|
|||
if (r == XTC_OK) r = xtc_project_tick_to_sample(project, begin - n->start_tick, rate, &s->age);
|
||||
if (r != XTC_OK) { free(points); free(p); return r; }
|
||||
if (s->end <= s->start) continue;
|
||||
s->track = xtc_mix_track(&p->mix, c.track_id);
|
||||
s->pitch = n->pitch; s->velocity = n->velocity; s->channel = n->channel;
|
||||
points[p->count * 2].time = s->start; points[p->count * 2].change = 1;
|
||||
points[p->count * 2 + 1].time = s->end; points[p->count * 2 + 1].change = -1;
|
||||
|
|
@ -122,7 +128,7 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
p->voices[slot].pitch = e->pitch; p->voices[slot].velocity = e->velocity;
|
||||
p->voices[slot].phase = 0; p->voices[slot].age = p->age++;
|
||||
}
|
||||
static void segment(xtc_midi_plan *p, uint64_t position, float *out, size_t frames)
|
||||
static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t i, j;
|
||||
for (i = 0; i < p->count; ++i) {
|
||||
|
|
@ -130,27 +136,30 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, size_t fram
|
|||
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 || s->start < p->cutoff[s->channel] || end <= begin) continue;
|
||||
if (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);
|
||||
out[j] += (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
|
||||
float sample = (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
|
||||
out[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, right ? 0U : 2U);
|
||||
if (right) 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) continue;
|
||||
if (!v->active || step == 0.0) continue;
|
||||
for (j = 0; j < frames; ++j) {
|
||||
out[j] += (float)(sin(TAU * v->phase) * 0.1 * v->velocity / 127.0);
|
||||
float sample = (float)(sin(TAU * v->phase) * 0.1 * v->velocity / 127.0);
|
||||
out[j] += sample; if (right) right[j] += sample;
|
||||
v->phase += step; if (v->phase >= 1) v->phase -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position,
|
||||
const xtc_midi_event *events, size_t count, float *out, size_t frames)
|
||||
xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position,
|
||||
const xtc_midi_event *events, size_t count, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t i, cursor = 0;
|
||||
if (!p || !out || count > XTC_MIDI_MAX_EVENTS || (count && !events) ||
|
||||
if (!p || !out || (right && right == out) || count > XTC_MIDI_MAX_EVENTS || (count && !events) ||
|
||||
frames > 65536U || position > UINT64_MAX - frames) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < count; ++i) {
|
||||
const xtc_midi_event *e = events + i;
|
||||
|
|
@ -162,12 +171,16 @@ xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position,
|
|||
if (!frames) return XTC_OK;
|
||||
if (p->next_position != position) xtc_midi_plan_reset(p);
|
||||
memset(out, 0, frames * sizeof(*out));
|
||||
if (right) memset(right, 0, frames * sizeof(*right));
|
||||
for (i = 0; i < count; ++i) {
|
||||
size_t next = events[i].offset;
|
||||
segment(p, position + cursor, out + cursor, next - cursor);
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, next - cursor);
|
||||
event(p, events + i, position + next); cursor = next;
|
||||
}
|
||||
segment(p, position + cursor, out + cursor, frames - cursor);
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, frames - cursor);
|
||||
p->next_position = position + frames;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position, const xtc_midi_event *events, size_t count, float *out, size_t frames)
|
||||
{ return xtc_midi_plan_render_stereo(p, position, events, count, out, NULL, frames); }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue