Phase 22: add non-destructive audio splitting, clip gain and fades, and cached waveforms

This commit is contained in:
Johannes Findeisen 2026-10-27 15:46:41 +01:00
commit 01d8c1d661
23 changed files with 650 additions and 18 deletions

View file

@ -8,7 +8,8 @@
#include <string.h>
typedef struct {
uint64_t start, end, offset;
uint64_t start, end, offset, fade_in, fade_out;
float gain;
size_t asset, track;
} region;
@ -185,6 +186,9 @@ xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
result = xtc_project_tick_to_sample(project, clip->start_tick + clip->length_tick, rate, &r->end);
if (result == XTC_OK)
result = xtc_project_tick_to_sample(project, clip->offset_tick, rate, &r->offset);
r->gain = clip->gain;
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->fade_in_tick, rate, &r->fade_in);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->fade_out_tick, rate, &r->fade_out);
if (result != XTC_OK)
break;
}
@ -219,7 +223,7 @@ xtc_result xtc_audio_plan_set_fade_frames(xtc_audio_plan *plan, size_t frames)
}
/**
* @brief Renders a block of the plan at unity gain with linear rate conversion.
* @brief Renders a block with clip/track gain, boundary fades and linear rate conversion.
*
* Both buffers are replaced, not mixed into. Rendering performs no allocation,
* file access, or locking. Non-finite output samples are replaced with zero.
@ -247,7 +251,7 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
uint64_t audible_end = r->end;
uint64_t begin = position > r->start ? position : r->start;
uint64_t end;
if (plan->fade_frames && wave->samples) {
if (wave->samples) {
uint64_t duration = ((uint64_t)wave->frames * plan->rate + wave->rate - 1U) / wave->rate;
if (duration <= r->offset) continue;
if (duration - r->offset < audible_end - r->start)
@ -274,6 +278,16 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
if (distance < plan->fade_frames)
sample *= (float)distance / (float)plan->fade_frames;
}
{
uint64_t age = position + j - r->start, tail = audible_end - 1U - (position + j);
float envelope = 1.0f;
if (r->fade_in && age < r->fade_in) envelope = (float)age / (float)r->fade_in;
if (r->fade_out && tail < r->fade_out) {
float fade = (float)tail / (float)r->fade_out;
if (fade < envelope) envelope = fade;
}
sample *= r->gain * envelope;
}
output[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, right ? 0U : 2U);
if (right)
right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);

53
src/audio/waveform.c Normal file
View file

@ -0,0 +1,53 @@
#include "wave_internal.h"
#include <stdlib.h>
struct xtc_waveform {
unsigned int rate;
size_t frames, bins;
float minimum[1024], maximum[1024];
};
xtc_result xtc_waveform_create(const char *path, xtc_waveform **out)
{
xtc_wave wave = {0};
xtc_waveform *summary;
xtc_result result;
size_t i, j;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!path) return XTC_ERR_INVALID_ARGUMENT;
result = xtc_wave_load(path, &wave);
if (result != XTC_OK) return result;
summary = calloc(1, sizeof(*summary));
if (!summary) { free(wave.samples); return XTC_ERR_OUT_OF_MEMORY; }
summary->rate = wave.rate;
summary->frames = wave.frames;
summary->bins = wave.frames < 1024 ? wave.frames : 1024;
for (i = 0; i < summary->bins; ++i) {
size_t first = (size_t)((uint64_t)i * wave.frames / summary->bins);
size_t end = (size_t)((uint64_t)(i+1) * wave.frames / summary->bins);
float low = wave.samples[first], high = low;
for (j = first+1; j < end; ++j) {
if (wave.samples[j] < low) low = wave.samples[j];
if (wave.samples[j] > high) high = wave.samples[j];
}
summary->minimum[i] = low;
summary->maximum[i] = high;
}
free(wave.samples);
*out = summary;
return XTC_OK;
}
void xtc_waveform_destroy(xtc_waveform *p) { free(p); }
xtc_result xtc_waveform_info(const xtc_waveform *p, unsigned int *rate, size_t *frames, size_t *bins)
{
if (!p || !rate || !frames || !bins) return XTC_ERR_INVALID_ARGUMENT;
*rate = p->rate; *frames = p->frames; *bins = p->bins;
return XTC_OK;
}
xtc_result xtc_waveform_peak(const xtc_waveform *p, size_t bin, float *low, float *high)
{
if (!p || !low || !high || bin >= p->bins) return XTC_ERR_INVALID_ARGUMENT;
*low = p->minimum[bin]; *high = p->maximum[bin];
return XTC_OK;
}