Phase 24: add mix buses, coherent peak meters and master clipping indicators

This commit is contained in:
Johannes Findeisen 2026-10-27 16:15:34 +01:00
commit 2fd7724065
32 changed files with 632 additions and 61 deletions

View file

@ -2,6 +2,7 @@
#define XTC_MIX_INTERNAL_H
#include "../project/project_internal.h"
#include "xtc/xtc_audio.h"
typedef struct {
uint64_t sample, tick;
@ -15,6 +16,7 @@ typedef struct {
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;
size_t bus[XTC_PROJECT_MAX_TRACKS];
} xtc_mix_snapshot;
/**
@ -45,4 +47,11 @@ size_t xtc_mix_track(const xtc_mix_snapshot * m, uint64_t id);
* @return Linear level for the chosen side, or zero for an absent or inaudible track.
*/
float xtc_mix_level(const xtc_mix_snapshot * m, size_t track, uint64_t sample, unsigned int side);
float xtc_mix_leaf_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side);
/* Borrowed control-installed scratch: 2*track_count channels, stride frames.
* Clear once per callback before audio+MIDI rendering. No pointer changes active. */
void xtc_audio_plan_monitor(xtc_audio_plan *plan, float *samples, size_t stride);
void xtc_midi_plan_monitor(xtc_midi_plan *plan, float *samples, size_t stride);
void xtc_mix_monitor(const xtc_mix_snapshot *mix, float *samples, size_t stride, size_t track,
uint64_t position, size_t frame, float sample, int stereo);
#endif

View file

@ -129,3 +129,38 @@ xtc_result xtc_project_get_automation(const xtc_project *p, size_t i, xtc_automa
*a = p->automation[i];
return XTC_OK;
}
xtc_result xtc_project_add_bus(xtc_project *p, const char *name, uint64_t *out)
{
xtc_result r = xtc_project_add_track(p, name, out);
if (r == XTC_OK) p->tracks[p->track_count-1].is_bus = 1;
return r;
}
xtc_result xtc_project_route_track(xtc_project *p, uint64_t id, uint64_t bus)
{
size_t i, destination;
if (!p) return XTC_ERR_INVALID_ARGUMENT;
i = find_track(p, id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
if (p->tracks[i].is_bus) return XTC_ERR_INVALID_STATE;
destination = find_track(p, bus);
if (bus != UINT64_MAX && (destination == SIZE_MAX || !p->tracks[destination].is_bus)) return XTC_ERR_INVALID_ARGUMENT;
p->tracks[i].bus_id = bus;
return XTC_OK;
}
void xtc_peak_meter_reset(xtc_peak_meter *m)
{
if (m) { m->db = -90; m->clipped = 0; }
}
xtc_result xtc_peak_meter_update(xtc_peak_meter *m, float peak, double seconds)
{
float db, released;
if (!m || !isfinite(peak) || peak < 0 || !isfinite(seconds) || seconds < 0 || seconds > 60)
return XTC_ERR_INVALID_ARGUMENT;
db = peak > 0 ? 20.0f * log10f(peak) : -90;
if (db < -90) db = -90;
released = m->db - (float)(20 * seconds);
m->db = db > released ? db : released;
if (peak >= 1) m->clipped = 1;
return XTC_OK;
}

View file

@ -50,9 +50,10 @@ xtc_result xtc_mix_prepare(const xtc_project *p, unsigned int rate, xtc_mix_snap
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 < m->count; ++i) {
if (m->tracks[i].solo) m->any_solo = 1;
m->bus[i] = xtc_mix_track(m, m->tracks[i].bus_id);
}
for (i = 0; i < p->automation_count; ++i) {
const xtc_automation_point *a = p->automation + i;
xtc_mix_point *b = m->points + i;
@ -117,12 +118,13 @@ static float value(const xtc_mix_snapshot *m, size_t track, unsigned int param,
* @param side Output side: 0 for left, 1 for right, 2 for mono gain only.
* @return Linear level for the chosen side, or zero for an absent or inaudible track.
*/
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
float xtc_mix_leaf_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))
if (m->tracks[track].mute || (m->any_solo && !m->tracks[track].solo &&
!(m->bus[track] < m->count && m->tracks[m->bus[track]].solo)))
return 0;
gain = value(m, track, 0, sample);
if (side == 2)
@ -130,3 +132,31 @@ float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, un
pan = value(m, track, 1, sample);
return gain * (side == 0 ? (pan > 0 ? 1 - pan : 1) : (pan < 0 ? 1 + pan : 1));
}
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
{
float level = xtc_mix_leaf_level(m, track, sample, side);
size_t bus;
if (track >= m->count) return 0;
bus = m->bus[track];
if (bus < m->count) {
float gain, pan;
if (m->tracks[bus].mute) return 0;
gain = value(m, bus, 0, sample);
pan = side == 2 ? 0 : value(m, bus, 1, sample);
level *= gain * (side == 0 ? (pan > 0 ? 1-pan : 1) : side == 1 ? (pan < 0 ? 1+pan : 1) : 1);
}
return level;
}
void xtc_mix_monitor(const xtc_mix_snapshot *m, float *samples, size_t stride, size_t track,
uint64_t position, size_t frame, float sample, int stereo)
{
unsigned int side;
if (!samples || track >= m->count || frame >= stride) return;
for (side = 0; side < 2; ++side) {
unsigned int channel = stereo ? side : 2;
samples[(track*2+side)*stride+frame] += sample * xtc_mix_leaf_level(m, track, position, channel);
if (m->bus[track] < m->count)
samples[(m->bus[track]*2+side)*stride+frame] += sample * xtc_mix_level(m, track, position, channel);
}
}