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
6
src/mixer/README.md
Normal file
6
src/mixer/README.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# Mixer core
|
||||
|
||||
`model.c` validates track settings and stable track-relative automation references.
|
||||
`render.c` prepares sorted sample endpoints and evaluates linear gain/pan lanes
|
||||
without allocation. `mix_internal.h` is private snapshot storage for audio/MIDI
|
||||
source plans. See ../../docs/MIXER.md for the stereo pan law and ownership.
|
||||
20
src/mixer/mix_internal.h
Normal file
20
src/mixer/mix_internal.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef XTC_MIX_INTERNAL_H
|
||||
#define XTC_MIX_INTERNAL_H
|
||||
#include "../project/project_internal.h"
|
||||
typedef struct {
|
||||
uint64_t sample, tick;
|
||||
size_t track;
|
||||
unsigned int parameter;
|
||||
float value;
|
||||
} xtc_mix_point;
|
||||
typedef struct {
|
||||
xtc_track_info tracks[XTC_PROJECT_MAX_TRACKS];
|
||||
size_t count, offsets[XTC_PROJECT_MAX_TRACKS][2], lengths[XTC_PROJECT_MAX_TRACKS][2];
|
||||
xtc_mix_point points[XTC_MAX_AUTOMATION_POINTS];
|
||||
int any_solo;
|
||||
} xtc_mix_snapshot;
|
||||
xtc_result xtc_mix_prepare(const xtc_project *, unsigned int rate, xtc_mix_snapshot *);
|
||||
size_t xtc_mix_track(const xtc_mix_snapshot *, uint64_t id);
|
||||
/* side 0/1 stereo, 2 mono (gain only). */
|
||||
float xtc_mix_level(const xtc_mix_snapshot *, size_t track, uint64_t sample, unsigned int side);
|
||||
#endif
|
||||
81
src/mixer/model.c
Normal file
81
src/mixer/model.c
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include "../project/project_internal.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
static size_t find_track(const xtc_project *p, uint64_t id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < p->track_count; ++i)
|
||||
if (p->tracks[i].id == id)
|
||||
return i;
|
||||
return SIZE_MAX;
|
||||
}
|
||||
xtc_result xtc_project_set_mix(xtc_project *p, uint64_t id, float gain, float pan, int mute,
|
||||
int solo)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || !isfinite(gain) || gain < 0 || gain > 4 || !isfinite(pan) || pan < -1 || pan > 1 ||
|
||||
(mute != 0 && mute != 1) || (solo != 0 && solo != 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
i = find_track(p, id);
|
||||
if (i == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
p->tracks[i].gain = gain;
|
||||
p->tracks[i].pan = pan;
|
||||
p->tracks[i].mute = mute;
|
||||
p->tracks[i].solo = solo;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_set_automation(xtc_project *p, const xtc_automation_point *a)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || !a || !isfinite(a->value) || a->tick > XTC_PROJECT_MAX_TICK ||
|
||||
(a->parameter != XTC_AUTOMATION_GAIN && a->parameter != XTC_AUTOMATION_PAN) ||
|
||||
a->value < (a->parameter == XTC_AUTOMATION_GAIN ? 0 : -1) ||
|
||||
a->value > (a->parameter == XTC_AUTOMATION_GAIN ? 4 : 1))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (find_track(p, a->track_id) == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
for (i = 0; i < p->automation_count; ++i)
|
||||
if (p->automation[i].track_id == a->track_id &&
|
||||
p->automation[i].parameter == a->parameter && p->automation[i].tick == a->tick)
|
||||
break;
|
||||
if (i == XTC_MAX_AUTOMATION_POINTS)
|
||||
return XTC_ERR_CAPACITY;
|
||||
if (i == p->automation_count)
|
||||
++p->automation_count;
|
||||
p->automation[i] = *a;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_remove_automation(xtc_project *p, uint64_t id,
|
||||
xtc_automation_parameter param, uint64_t tick)
|
||||
{
|
||||
size_t i;
|
||||
if (!p || (param != XTC_AUTOMATION_GAIN && param != XTC_AUTOMATION_PAN) ||
|
||||
tick > XTC_PROJECT_MAX_TICK)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < p->automation_count; ++i)
|
||||
if (p->automation[i].track_id == id && p->automation[i].parameter == param &&
|
||||
p->automation[i].tick == tick)
|
||||
break;
|
||||
if (i == p->automation_count)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
memmove(p->automation + i, p->automation + i + 1,
|
||||
(--p->automation_count - i) * sizeof(*p->automation));
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_automation_count(const xtc_project *p, size_t *n)
|
||||
{
|
||||
if (!p || !n)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
*n = p->automation_count;
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_project_get_automation(const xtc_project *p, size_t i, xtc_automation_point *a)
|
||||
{
|
||||
if (!p || !a)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (i >= p->automation_count)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
*a = p->automation[i];
|
||||
return XTC_OK;
|
||||
}
|
||||
87
src/mixer/render.c
Normal file
87
src/mixer/render.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include "mix_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
size_t xtc_mix_track(const xtc_mix_snapshot *m, uint64_t id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < m->count; ++i)
|
||||
if (m->tracks[i].id == id)
|
||||
return i;
|
||||
return SIZE_MAX;
|
||||
}
|
||||
static int compare(const void *left, const void *right)
|
||||
{
|
||||
const xtc_mix_point *a = left, *b = right;
|
||||
if (a->track != b->track)
|
||||
return a->track < b->track ? -1 : 1;
|
||||
if (a->parameter != b->parameter)
|
||||
return a->parameter < b->parameter ? -1 : 1;
|
||||
return a->tick < b->tick ? -1 : a->tick != b->tick;
|
||||
}
|
||||
xtc_result xtc_mix_prepare(const xtc_project *p, unsigned int rate, xtc_mix_snapshot *m)
|
||||
{
|
||||
size_t i;
|
||||
xtc_result r;
|
||||
memset(m, 0, sizeof(*m));
|
||||
m->count = p->track_count;
|
||||
memcpy(m->tracks, p->tracks, p->track_count * sizeof(*m->tracks));
|
||||
for (i = 0; i < m->count; ++i)
|
||||
if (m->tracks[i].solo)
|
||||
m->any_solo = 1;
|
||||
for (i = 0; i < p->automation_count; ++i) {
|
||||
const xtc_automation_point *a = p->automation + i;
|
||||
xtc_mix_point *b = m->points + i;
|
||||
b->track = xtc_mix_track(m, a->track_id);
|
||||
b->parameter = (unsigned int)a->parameter;
|
||||
if (b->track == SIZE_MAX || b->parameter > 1)
|
||||
return XTC_ERR_INVALID_STATE;
|
||||
b->tick = a->tick;
|
||||
b->value = a->value;
|
||||
r = xtc_project_tick_to_sample(p, a->tick, rate, &b->sample);
|
||||
if (r != XTC_OK)
|
||||
return r;
|
||||
}
|
||||
qsort(m->points, p->automation_count, sizeof(*m->points), compare);
|
||||
for (i = 0; i < p->automation_count; ++i) {
|
||||
xtc_mix_point *a = m->points + i;
|
||||
size_t *n = &m->lengths[a->track][a->parameter];
|
||||
if (!*n)
|
||||
m->offsets[a->track][a->parameter] = i;
|
||||
++*n;
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
static float value(const xtc_mix_snapshot *m, size_t track, unsigned int param, uint64_t sample)
|
||||
{
|
||||
size_t low = 0, high = m->lengths[track][param], count = high;
|
||||
const xtc_mix_point *p = m->points + m->offsets[track][param];
|
||||
if (!count)
|
||||
return param ? m->tracks[track].pan : m->tracks[track].gain;
|
||||
while (low < high) {
|
||||
size_t mid = low + (high - low) / 2;
|
||||
if (p[mid].sample <= sample)
|
||||
low = mid + 1;
|
||||
else
|
||||
high = mid;
|
||||
}
|
||||
if (!low)
|
||||
return p[0].value;
|
||||
if (low == count)
|
||||
return p[count - 1].value;
|
||||
return (float)(p[low - 1].value + ((double)sample - (double)p[low - 1].sample) /
|
||||
(double)(p[low].sample - p[low - 1].sample) *
|
||||
(p[low].value - p[low - 1].value));
|
||||
}
|
||||
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
|
||||
{
|
||||
float gain, pan;
|
||||
if (track >= m->count)
|
||||
return 0;
|
||||
if (m->tracks[track].mute || (m->any_solo && !m->tracks[track].solo))
|
||||
return 0;
|
||||
gain = value(m, track, 0, sample);
|
||||
if (side == 2)
|
||||
return gain;
|
||||
pan = value(m, track, 1, sample);
|
||||
return gain * (side == 0 ? (pan > 0 ? 1 - pan : 1) : (pan < 0 ? 1 + pan : 1));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue