Speedup rendering functions (#573)

This commit is contained in:
Carlo Bramini 2019-10-23 13:33:22 +02:00 committed by Tom M
parent eef361eb35
commit 8aef0930c6

View file

@ -3815,9 +3815,9 @@ fluid_synth_write_float_LOCAL(fluid_synth_t *synth, int len,
int (*block_render_func)(fluid_synth_t *, int) int (*block_render_func)(fluid_synth_t *, int)
) )
{ {
int i, j, k, l; int n, cur, size;
float *left_out = (float *) lout; float *left_out = (float *) lout + loff;
float *right_out = (float *) rout; float *right_out = (float *) rout + roff;
fluid_real_t *left_in; fluid_real_t *left_in;
fluid_real_t *right_in; fluid_real_t *right_in;
double time = fluid_utime(); double time = fluid_utime();
@ -3830,27 +3830,55 @@ fluid_synth_write_float_LOCAL(fluid_synth_t *synth, int len,
fluid_return_val_if_fail(rout != NULL, FLUID_FAILED); fluid_return_val_if_fail(rout != NULL, FLUID_FAILED);
fluid_rvoice_mixer_set_mix_fx(synth->eventhandler->mixer, 1); fluid_rvoice_mixer_set_mix_fx(synth->eventhandler->mixer, 1);
l = synth->cur;
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in); fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in);
for(i = 0, j = loff, k = roff; i < len; i++, l++, j += lincr, k += rincr) if (FLUID_LIKELY((size = len) > 0))
{ {
/* fill up the buffers as needed */ cur = synth->cur;
if(l >= synth->curmax)
do
{ {
int blocksleft = (len - i + FLUID_BUFSIZE - 1) / FLUID_BUFSIZE; /* fill up the buffers as needed */
synth->curmax = FLUID_BUFSIZE * block_render_func(synth, blocksleft); if(cur >= synth->curmax)
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in); {
int blocksleft = (size + FLUID_BUFSIZE - 1) / FLUID_BUFSIZE;
synth->curmax = FLUID_BUFSIZE * block_render_func(synth, blocksleft);
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in);
cur = 0;
}
l = 0; /* calculate amount of available samples */
n = synth->curmax - cur;
/* keep track of emitted samples */
if (n > size) n = size;
size -= n;
/* update pointers to current position */
left_in += cur+n;
right_in += cur+n;
/* set final cursor position */
cur += n;
/* reverse index */
n = 0 - n;
do
{
*left_out = (float) left_in[n];
*right_out = (float) right_in[n];
left_out += lincr;
right_out += rincr;
}
while (++n < 0);
} }
while (size);
left_out[j] = (float) left_in[0 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + l]; synth->cur = cur;
right_out[k] = (float) right_in[0 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + l];
} }
synth->cur = l;
time = fluid_utime() - time; time = fluid_utime() - time;
cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0); cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0);
fluid_atomic_float_set(&synth->cpu_load, cpu_load); fluid_atomic_float_set(&synth->cpu_load, cpu_load);
@ -3941,13 +3969,12 @@ fluid_synth_write_s16(fluid_synth_t *synth, int len,
void *lout, int loff, int lincr, void *lout, int loff, int lincr,
void *rout, int roff, int rincr) void *rout, int roff, int rincr)
{ {
int i, j, k, cur; int di, n, cur, size;
int16_t *left_out = lout; int16_t *left_out = (int16_t *)lout + loff;
int16_t *right_out = rout; int16_t *right_out = (int16_t *)rout + roff;
fluid_real_t *left_in; fluid_real_t *left_in;
fluid_real_t *right_in; fluid_real_t *right_in;
double time = fluid_utime(); double time = fluid_utime();
int di;
float cpu_load; float cpu_load;
fluid_profile_ref_var(prof_ref); fluid_profile_ref_var(prof_ref);
@ -3955,33 +3982,60 @@ fluid_synth_write_s16(fluid_synth_t *synth, int len,
fluid_rvoice_mixer_set_mix_fx(synth->eventhandler->mixer, 1); fluid_rvoice_mixer_set_mix_fx(synth->eventhandler->mixer, 1);
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in); fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in);
cur = synth->cur; if (FLUID_LIKELY((size = len) > 0))
di = synth->dither_index;
for(i = 0, j = loff, k = roff; i < len; i++, cur++, j += lincr, k += rincr)
{ {
cur = synth->cur;
di = synth->dither_index;
/* fill up the buffers as needed */ do
if(cur >= synth->curmax)
{ {
int blocksleft = (len - i + FLUID_BUFSIZE - 1) / FLUID_BUFSIZE; /* fill up the buffers as needed */
synth->curmax = FLUID_BUFSIZE * fluid_synth_render_blocks(synth, blocksleft); if(cur >= synth->curmax)
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in); {
cur = 0; int blocksleft = (size + FLUID_BUFSIZE - 1) / FLUID_BUFSIZE;
} synth->curmax = FLUID_BUFSIZE * fluid_synth_render_blocks(synth, blocksleft);
fluid_rvoice_mixer_get_bufs(synth->eventhandler->mixer, &left_in, &right_in);
cur = 0;
}
left_out[j] = round_clip_to_i16(left_in[0 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + cur] * 32766.0f + rand_table[0][di]); /* calculate amount of available samples */
right_out[k] = round_clip_to_i16(right_in[0 * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + cur] * 32766.0f + rand_table[1][di]); n = synth->curmax - cur;
if(++di >= DITHER_SIZE) /* keep track of emitted samples */
{ if (n > size) n = size;
di = 0; size -= n;
/* update pointers to current position */
left_in += cur+n;
right_in += cur+n;
/* set final cursor position */
cur += n;
/* reverse index */
n = 0 - n;
do
{
*left_out = round_clip_to_i16(left_in[n] * 32766.0f + rand_table[0][di]);
*right_out = round_clip_to_i16(right_in[n] * 32766.0f + rand_table[1][di]);
left_out += lincr;
right_out += rincr;
if(++di >= DITHER_SIZE)
{
di = 0;
}
}
while (++n < 0);
} }
while (size);
synth->cur = cur;
synth->dither_index = di; /* keep dither buffer continous */
} }
synth->cur = cur;
synth->dither_index = di; /* keep dither buffer continous */
time = fluid_utime() - time; time = fluid_utime() - time;
cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0); cpu_load = 0.5 * (fluid_atomic_float_get(&synth->cpu_load) + time * synth->sample_rate / len / 10000.0);
fluid_atomic_float_set(&synth->cpu_load, cpu_load); fluid_atomic_float_set(&synth->cpu_load, cpu_load);