audio: Don't ever use libsamplerate in the SDL_AudioCVT codepath.

It causes audio pops if you're converting in chunks (and needs to
allocate/initialize/free on each convert). We'll either adjust this interface
when we break ABI for 2.1 to make this usable, or publish the SDL_AudioStream
API for those that want a streaming solution.

In the meantime, the "simple" resampler produces "good enough" audio without
pops and doesn't have to be initialized, so that'll do for now on the
SDL_AudioCVT interface.
This commit is contained in:
Ryan C. Gordon 2017-01-09 16:31:57 -05:00
parent 97f19cc367
commit 23020f92fa

View file

@ -377,53 +377,16 @@ SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format
const int srclen = cvt->len_cvt;
float *dst = (float *) (cvt->buf + srclen);
const int dstlen = (cvt->len * cvt->len_mult) - srclen;
SDL_bool do_simple = SDL_TRUE;
float state[8];
int i;
SDL_assert(format == AUDIO_F32SYS);
#ifdef HAVE_LIBSAMPLERATE_H
if (SRC_available) {
int result = 0;
SRC_STATE *state = SRC_src_new(SRC_SINC_FASTEST, chans, &result);
if (state) {
const int framelen = sizeof(float) * chans;
SRC_DATA data;
data.data_in = (float *)src; /* Older versions of libsamplerate had a non-const pointer, but didn't write to it */
data.input_frames = srclen / framelen;
data.input_frames_used = 0;
data.data_out = dst;
data.output_frames = dstlen / framelen;
data.end_of_input = 0;
data.src_ratio = cvt->rate_incr;
result = SRC_src_process(state, &data);
SDL_assert(result == 0); /* what to do if this fails? Can it fail? */
/* What to do if this fails...? */
SDL_assert(data.input_frames_used == data.input_frames);
SRC_src_delete(state);
cvt->len_cvt = data.output_frames_gen * (sizeof(float) * chans);
do_simple = SDL_FALSE;
}
/* failed to create state? Fall back to simple method. */
for (i = 0; i < chans; i++) {
state[i] = src[i];
}
#endif
if (do_simple) {
float state[8];
int i;
for (i = 0; i < chans; i++) {
state[i] = src[i];
}
cvt->len_cvt = SDL_ResampleAudioSimple(chans, cvt->rate_incr, state, src, srclen, dst, dstlen);
}
cvt->len_cvt = SDL_ResampleAudioSimple(chans, cvt->rate_incr, state, src, srclen, dst, dstlen);
SDL_memcpy(cvt->buf, dst, cvt->len_cvt);
if (cvt->filters[++cvt->filter_index]) {