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
|
|
@ -1,4 +1,5 @@
|
|||
#include "xtc/xtc_midi.h"
|
||||
#include "xtc/xtc_dsp.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -24,6 +25,7 @@ typedef struct {
|
|||
} voice;
|
||||
|
||||
struct xtc_midi_plan {
|
||||
size_t fade_frames;
|
||||
xtc_mix_snapshot mix;
|
||||
span notes[XTC_PROJECT_MAX_NOTES];
|
||||
size_t count;
|
||||
|
|
@ -281,6 +283,13 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
|
|||
* @param right Destination right-channel buffer; NULL selects mono rendering.
|
||||
* @param frames Number of audio frames.
|
||||
*/
|
||||
xtc_result xtc_midi_plan_set_fade_frames(xtc_midi_plan *plan, size_t frames)
|
||||
{
|
||||
if (!plan || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
|
||||
plan->fade_frames = frames;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *right, size_t frames)
|
||||
{
|
||||
size_t i, j;
|
||||
|
|
@ -294,6 +303,13 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
|
|||
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);
|
||||
float sample = (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
|
||||
if (p->fade_frames) {
|
||||
uint64_t age = position + j - s->start;
|
||||
uint64_t tail = s->end - 1U - (position + j);
|
||||
uint64_t distance = age < tail ? age : tail;
|
||||
if (distance < p->fade_frames)
|
||||
sample *= (float)distance / (float)p->fade_frames;
|
||||
}
|
||||
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);
|
||||
|
|
@ -356,6 +372,10 @@ xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position, cons
|
|||
cursor = next;
|
||||
}
|
||||
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, frames - cursor);
|
||||
for (i = 0; i < frames; ++i) {
|
||||
out[i] = xtc_dsp_finite(out[i]);
|
||||
if (right) right[i] = xtc_dsp_finite(right[i]);
|
||||
}
|
||||
p->next_position = position + frames;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue