Phase 12: Improve audio transitions and verify sample-accurate timing
This commit is contained in:
parent
461689dd44
commit
05140c2e93
29 changed files with 795 additions and 33 deletions
|
|
@ -2,6 +2,7 @@
|
|||
#include "wave_internal.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include "../graph/graph_internal.h"
|
||||
#include "xtc/xtc_dsp.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -12,6 +13,7 @@ typedef struct {
|
|||
} region;
|
||||
|
||||
struct xtc_audio_plan {
|
||||
size_t fade_frames;
|
||||
xtc_mix_snapshot mix;
|
||||
xtc_wave waves[64];
|
||||
char paths[64][XTC_ASSET_PATH_BYTES];
|
||||
|
|
@ -209,6 +211,13 @@ size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
|
|||
return plan ? plan->issues : 0;
|
||||
}
|
||||
|
||||
xtc_result xtc_audio_plan_set_fade_frames(xtc_audio_plan *plan, size_t frames)
|
||||
{
|
||||
if (!plan || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
|
||||
plan->fade_frames = frames;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Renders a block of the plan at unity gain with linear rate conversion.
|
||||
*
|
||||
|
|
@ -235,8 +244,16 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
|
|||
for (i = 0; i < plan->count; ++i) {
|
||||
const region *r = plan->regions + i;
|
||||
const xtc_wave *wave = plan->waves + r->asset;
|
||||
uint64_t audible_end = r->end;
|
||||
uint64_t begin = position > r->start ? position : r->start;
|
||||
uint64_t end = position + frames < r->end ? position + frames : r->end;
|
||||
uint64_t end;
|
||||
if (plan->fade_frames && 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)
|
||||
audible_end = r->start + duration - r->offset;
|
||||
}
|
||||
end = position + frames < audible_end ? position + frames : audible_end;
|
||||
if (!wave->samples || end <= begin)
|
||||
continue;
|
||||
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
|
||||
|
|
@ -250,16 +267,21 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
|
|||
float sample =
|
||||
(float)(wave->samples[index] * (1.0 - fraction) +
|
||||
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
|
||||
if (plan->fade_frames) {
|
||||
uint64_t age = position + j - r->start;
|
||||
uint64_t tail = audible_end - 1U - (position + j);
|
||||
uint64_t distance = age < tail ? age : tail;
|
||||
if (distance < plan->fade_frames)
|
||||
sample *= (float)distance / (float)plan->fade_frames;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
for (j = 0; j < frames; ++j) {
|
||||
if (!isfinite(output[j]))
|
||||
output[j] = 0;
|
||||
if (right && !isfinite(right[j]))
|
||||
right[j] = 0;
|
||||
output[j] = xtc_dsp_finite(output[j]);
|
||||
if (right) right[j] = xtc_dsp_finite(right[j]);
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue