audio: libsamplerate loading now happens once at init time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 5773a4f..730a240 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -107,6 +107,72 @@ static const AudioBootStrap *const bootstrap[] = {
NULL
};
+
+#ifdef HAVE_LIBSAMPLERATE_H
+#ifdef SDL_LIBSAMPLERATE_DYNAMIC
+static void *SRC_lib = NULL;
+#endif
+SDL_bool SRC_available = SDL_FALSE;
+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;
+SRC_STATE* (*SRC_src_delete)(SRC_STATE *state) = NULL;
+const char* (*SRC_src_strerror)(int error) = NULL;
+
+static SDL_bool
+LoadLibSampleRate(void)
+{
+ SRC_available = SDL_FALSE;
+
+ if (!SDL_GetHintBoolean("SDL_AUDIO_ALLOW_LIBRESAMPLE", SDL_TRUE)) {
+ return SDL_FALSE;
+ }
+
+#ifdef SDL_LIBSAMPLERATE_DYNAMIC
+ SDL_assert(SRC_lib == NULL);
+ SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC);
+ if (!SRC_lib) {
+ return SDL_FALSE;
+ }
+#endif
+
+ SRC_src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(state->SRC_lib, "src_new");
+ SRC_src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(state->SRC_lib, "src_process");
+ SRC_src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_reset");
+ SRC_src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_delete");
+ SRC_src_strerror = (const char* (*)(int error))SDL_LoadFunction(state->SRC_lib, "src_strerror");
+
+ if (!SRC_src_new || !SRC_src_process || !SRC_src_reset || !SRC_src_delete || !SRC_src_strerror) {
+ #ifdef SDL_LIBSAMPLERATE_DYNAMIC
+ SDL_UnloadObject(SRC_lib);
+ SRC_lib = NULL;
+ #endif
+ return SDL_FALSE;
+ }
+
+ SRC_available = SDL_TRUE;
+ return SDL_TRUE;
+}
+
+static void
+UnloadLibSampleRate(void)
+{
+#ifdef SDL_LIBSAMPLERATE_DYNAMIC
+ if (SRC_lib != NULL) {
+ SDL_UnloadObject(SRC_lib);
+ }
+ SRC_lib = NULL;
+#endif
+
+ SRC_available = SDL_FALSE;
+ SRC_src_new = NULL;
+ SRC_src_process = NULL;
+ SRC_src_reset = NULL;
+ SRC_src_delete = NULL;
+ SRC_src_strerror = NULL;
+}
+#endif
+
static SDL_AudioDevice *
get_audio_device(SDL_AudioDeviceID id)
{
@@ -828,6 +894,10 @@ SDL_AudioInit(const char *driver_name)
/* Make sure we have a list of devices available at startup. */
current_audio.impl.DetectDevices();
+#ifdef HAVE_LIBSAMPLERATE_H
+ LoadLibSampleRate();
+#endif
+
return 0;
}
@@ -1427,6 +1497,10 @@ SDL_AudioQuit(void)
SDL_zero(current_audio);
SDL_zero(open_devices);
+
+#ifdef HAVE_LIBSAMPLERATE_H
+ UnloadLibSampleRate();
+#endif
}
#define NUM_FORMATS 10
diff --git a/src/audio/SDL_audio_c.h b/src/audio/SDL_audio_c.h
index 62467cf..2736fe4 100644
--- a/src/audio/SDL_audio_c.h
+++ b/src/audio/SDL_audio_c.h
@@ -36,6 +36,16 @@
/* Functions and variables exported from SDL_audio.c for SDL_sysaudio.c */
+#ifdef HAVE_LIBSAMPLERATE_H
+extern SDL_bool SRC_available;
+typedef struct SRC_STATE SRC_STATE;
+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);
+extern SRC_STATE* (*SRC_src_delete)(SRC_STATE *state);
+extern const char* (*SRC_src_strerror)(int error);
+#endif
+
/* Functions to get a list of "close" audio formats */
extern SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format);
extern SDL_AudioFormat SDL_NextAudioFormat(void);
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index a9d4fce..1d9f027 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -634,45 +634,10 @@ struct SDL_AudioStream
};
#ifdef HAVE_LIBSAMPLERATE_H
-
-typedef struct
-{
- void *SRC_lib;
-
- SRC_STATE* (*src_new)(int converter_type, int channels, int *error);
- int (*src_process)(SRC_STATE *state, SRC_DATA *data);
- int (*src_reset)(SRC_STATE *state);
- SRC_STATE* (*src_delete)(SRC_STATE *state);
- const char* (*src_strerror)(int error);
-
- SRC_STATE *SRC_state;
-} SDL_AudioStreamResamplerState_SRC;
-
-static SDL_bool
-LoadLibSampleRate(SDL_AudioStreamResamplerState_SRC *state)
-{
-#ifdef SDL_LIBSAMPLERATE_DYNAMIC
- state->SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC);
- if (!state->SRC_lib) {
- return SDL_FALSE;
- }
-#endif
-
- state->src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(state->SRC_lib, "src_new");
- state->src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(state->SRC_lib, "src_process");
- state->src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_reset");
- state->src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_delete");
- state->src_strerror = (const char* (*)(int error))SDL_LoadFunction(state->SRC_lib, "src_strerror");
- if (!state->src_new || !state->src_process || !state->src_reset || !state->src_delete || !state->src_strerror) {
- return SDL_FALSE;
- }
- return SDL_TRUE;
-}
-
static int
SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen)
{
- SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
+ SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
SRC_DATA data;
int result;
@@ -686,9 +651,9 @@ SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const float *inbuf, const i
data.end_of_input = 0;
data.src_ratio = stream->rate_incr;
- result = state->src_process(state->SRC_state, &data);
+ result = SRC_src_process(state, &data);
if (result != 0) {
- SDL_SetError("src_process() failed: %s", state->src_strerror(result));
+ SDL_SetError("src_process() failed: %s", SRC_src_strerror(result));
return 0;
}
@@ -701,20 +666,15 @@ SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const float *inbuf, const i
static void
SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *stream)
{
- SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
- state->src_reset(state->SRC_state);
+ SRC_src_reset((SRC_STATE *)stream->resampler_state);
}
static void
SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream)
{
- SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
+ SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
if (state) {
- if (state->SRC_lib) {
- SDL_UnloadObject(state->SRC_lib);
- }
- state->src_delete(state->SRC_state);
- SDL_free(state);
+ SRC_src_delete(state);
}
stream->resampler_state = NULL;
@@ -726,15 +686,18 @@ SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream)
static SDL_bool
SetupLibSampleRateResampling(SDL_AudioStream *stream)
{
- int result;
+ int result = 0;
+ SRC_STATE *state = NULL;
- SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC *)SDL_calloc(1, sizeof(*state));
- if (!state) {
- return SDL_FALSE;
+ if (SRC_available) {
+ state = SRC_src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result);
+ if (!state) {
+ SDL_SetError("src_new() failed: %s", SRC_src_strerror(result));
+ }
}
- if (!LoadLibSampleRate(state)) {
- SDL_free(state);
+ if (!state) {
+ SDL_CleanupAudioStreamResampler_SRC(stream);
return SDL_FALSE;
}
@@ -743,17 +706,11 @@ SetupLibSampleRateResampling(SDL_AudioStream *stream)
stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC;
stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC;
- state->SRC_state = state->src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result);
- if (!state->SRC_state) {
- SDL_SetError("src_new() failed: %s", state->src_strerror(result));
- SDL_CleanupAudioStreamResampler_SRC(stream);
- return SDL_FALSE;
- }
return SDL_TRUE;
}
-
#endif /* HAVE_LIBSAMPLERATE_H */
+
typedef struct
{
SDL_bool resampler_seeded;