# HG changeset patch # User Ryan C. Gordon # Date 1485291142 18000 # Node ID bc671e6906ae6bc3e8b89cb96a0fbf5abe7a942b # Parent 7764aba2f2114fc05d8da9b9f5472f8066faf240 audio: Offer a hint for libsamplerate quality/speed tradeoff. This defaults to the internal SDL resampler, since that's the likely default without a system-wide install of libsamplerate, but those that need more can tweak this. diff -r 7764aba2f211 -r bc671e6906ae include/SDL_hints.h --- a/include/SDL_hints.h Tue Jan 24 12:20:41 2017 -0500 +++ b/include/SDL_hints.h Tue Jan 24 15:52:22 2017 -0500 @@ -776,6 +776,42 @@ #define SDL_HINT_OPENGL_ES_DRIVER "SDL_OPENGL_ES_DRIVER" /** + * \brief A variable controlling speed/quality tradeoff of audio resampling. + * + * If available, SDL can use libsamplerate ( http://www.mega-nerd.com/SRC/ ) + * to handle audio resampling. There are different resampling modes available + * that produce different levels of quality, possibly using more CPU. + * + * If this hint isn't specified to a valid setting, or libsamplerate isn't + * available, SDL will act as if this hint was set to "fast". + * + * Note that this is currently only applicable to resampling audio that is + * being written to a device for playback or audio being read from a device + * for capture. SDL_AudioCVT always uses the "fast" resampler (although this + * might change for SDL 2.1). + * + * Most things can probably live with the "fast" resampler, but if quality + * is important or you can spare some CPU cycles, the other options are + * worth exploring! + * + * libsamplerate's interpolators, that these hints map to, are explained here: + * http://www.mega-nerd.com/SRC/api_misc.html#Converters + * + * This hint is only checked at audio subsystem init time and changes to it + * at other times are ignored. + * + * This variable can be set to the following values: + * + * "default" - Use SDL's internal, resampler. (Default when not set. low quality, fast.) + * "linear" - Use libsamplerate's Linear interpolator (low quality, fast). + * "zero_order_hold" - Use libsamplerate's Zero Order Hold interpolator (low quality, fast). + * "sinc_fastest" - Use libsamplerate's fastest (lowest quality) sinc interpolator. + * "sinc_medium" - Use libsamplerate's medium quality sinc interpolator. + * "sinc_best" - Use libsamplerate's best quality sinc interpolator. + */ +#define SDL_HINT_AUDIO_RESAMPLER_MODE "SDL_AUDIO_RESAMPLER_MODE" + +/** * \brief An enumeration of hint priorities */ typedef enum diff -r 7764aba2f211 -r bc671e6906ae src/audio/SDL_audio.c --- a/src/audio/SDL_audio.c Tue Jan 24 12:20:41 2017 -0500 +++ b/src/audio/SDL_audio.c Tue Jan 24 15:52:22 2017 -0500 @@ -113,6 +113,7 @@ static void *SRC_lib = NULL; #endif SDL_bool SRC_available = SDL_FALSE; +int SRC_converter = 0; SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error) = NULL; int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data) = NULL; int (*SRC_src_reset)(SRC_STATE *state) = NULL; @@ -122,10 +123,25 @@ static SDL_bool LoadLibSampleRate(void) { + const char *hint = SDL_GetHint(SDL_HINT_AUDIO_RESAMPLER_MODE); + SRC_available = SDL_FALSE; + SRC_converter = 0; - if (!SDL_GetHintBoolean("SDL_AUDIO_ALLOW_LIBRESAMPLE", SDL_TRUE)) { - return SDL_FALSE; + if (!hint || (SDL_strcasecmp(hint, "default") == 0)) { + return SDL_FALSE; /* don't load anything. */ + } else if (SDL_strcasecmp(hint, "linear") == 0) { + SRC_converter = SRC_LINEAR; + } else if (SDL_strcasecmp(hint, "zero_order_hold") == 0) { + SRC_converter = SRC_ZERO_ORDER_HOLD; + } else if (SDL_strcasecmp(hint, "sinc_fastest") == 0) { + SRC_converter = SRC_SINC_FASTEST; + } else if (SDL_strcasecmp(hint, "sinc_medium") == 0) { + SRC_converter = SRC_SINC_MEDIUM_QUALITY; + } else if (SDL_strcasecmp(hint, "sinc_best") == 0) { + SRC_converter = SRC_SINC_BEST_QUALITY; + } else { + return SDL_FALSE; /* treat it like "default", don't load anything. */ } #ifdef SDL_LIBSAMPLERATE_DYNAMIC diff -r 7764aba2f211 -r bc671e6906ae src/audio/SDL_audio_c.h --- a/src/audio/SDL_audio_c.h Tue Jan 24 12:20:41 2017 -0500 +++ b/src/audio/SDL_audio_c.h Tue Jan 24 15:52:22 2017 -0500 @@ -39,6 +39,7 @@ #ifdef HAVE_LIBSAMPLERATE_H #include "samplerate.h" extern SDL_bool SRC_available; +extern int SRC_converter; extern SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error); extern int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data); extern int (*SRC_src_reset)(SRC_STATE *state); diff -r 7764aba2f211 -r bc671e6906ae src/audio/SDL_audiocvt.c --- a/src/audio/SDL_audiocvt.c Tue Jan 24 12:20:41 2017 -0500 +++ b/src/audio/SDL_audiocvt.c Tue Jan 24 15:52:22 2017 -0500 @@ -947,7 +947,7 @@ SRC_STATE *state = NULL; if (SRC_available) { - state = SRC_src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result); + state = SRC_src_new(SRC_converter, stream->pre_resample_channels, &result); if (!state) { SDL_SetError("src_new() failed: %s", SRC_src_strerror(result)); }