src/audio/SDL_audiocvt.c
changeset 10790 92726dd20acd
parent 10789 e3eb7a266ec2
child 10791 64c46e09df9d
     1.1 --- a/src/audio/SDL_audiocvt.c	Sun Jan 08 14:17:09 2017 -0500
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sun Jan 08 14:18:03 2017 -0500
     1.3 @@ -634,45 +634,10 @@
     1.4  };
     1.5  
     1.6  #ifdef HAVE_LIBSAMPLERATE_H
     1.7 -
     1.8 -typedef struct
     1.9 -{
    1.10 -    void *SRC_lib;
    1.11 -
    1.12 -    SRC_STATE* (*src_new)(int converter_type, int channels, int *error);
    1.13 -    int (*src_process)(SRC_STATE *state, SRC_DATA *data);
    1.14 -    int (*src_reset)(SRC_STATE *state);
    1.15 -    SRC_STATE* (*src_delete)(SRC_STATE *state);
    1.16 -    const char* (*src_strerror)(int error);
    1.17 -
    1.18 -    SRC_STATE *SRC_state;
    1.19 -} SDL_AudioStreamResamplerState_SRC;
    1.20 -
    1.21 -static SDL_bool
    1.22 -LoadLibSampleRate(SDL_AudioStreamResamplerState_SRC *state)
    1.23 -{
    1.24 -#ifdef SDL_LIBSAMPLERATE_DYNAMIC
    1.25 -    state->SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC);
    1.26 -    if (!state->SRC_lib) {
    1.27 -        return SDL_FALSE;
    1.28 -    }
    1.29 -#endif
    1.30 -
    1.31 -    state->src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(state->SRC_lib, "src_new");
    1.32 -    state->src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(state->SRC_lib, "src_process");
    1.33 -    state->src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_reset");
    1.34 -    state->src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(state->SRC_lib, "src_delete");
    1.35 -    state->src_strerror = (const char* (*)(int error))SDL_LoadFunction(state->SRC_lib, "src_strerror");
    1.36 -    if (!state->src_new || !state->src_process || !state->src_reset || !state->src_delete || !state->src_strerror) {
    1.37 -        return SDL_FALSE;
    1.38 -    }
    1.39 -    return SDL_TRUE;
    1.40 -}
    1.41 -
    1.42  static int
    1.43  SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen)
    1.44  {
    1.45 -    SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
    1.46 +    SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
    1.47      SRC_DATA data;
    1.48      int result;
    1.49  
    1.50 @@ -686,9 +651,9 @@
    1.51      data.end_of_input = 0;
    1.52      data.src_ratio = stream->rate_incr;
    1.53  
    1.54 -    result = state->src_process(state->SRC_state, &data);
    1.55 +    result = SRC_src_process(state, &data);
    1.56      if (result != 0) {
    1.57 -        SDL_SetError("src_process() failed: %s", state->src_strerror(result));
    1.58 +        SDL_SetError("src_process() failed: %s", SRC_src_strerror(result));
    1.59          return 0;
    1.60      }
    1.61  
    1.62 @@ -701,20 +666,15 @@
    1.63  static void
    1.64  SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *stream)
    1.65  {
    1.66 -    SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
    1.67 -    state->src_reset(state->SRC_state);
    1.68 +    SRC_src_reset((SRC_STATE *)stream->resampler_state);
    1.69  }
    1.70  
    1.71  static void
    1.72  SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream)
    1.73  {
    1.74 -    SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC*)stream->resampler_state;
    1.75 +    SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
    1.76      if (state) {
    1.77 -        if (state->SRC_lib) {
    1.78 -            SDL_UnloadObject(state->SRC_lib);
    1.79 -        }
    1.80 -        state->src_delete(state->SRC_state);
    1.81 -        SDL_free(state);
    1.82 +        SRC_src_delete(state);
    1.83      }
    1.84  
    1.85      stream->resampler_state = NULL;
    1.86 @@ -726,15 +686,18 @@
    1.87  static SDL_bool
    1.88  SetupLibSampleRateResampling(SDL_AudioStream *stream)
    1.89  {
    1.90 -    int result;
    1.91 +    int result = 0;
    1.92 +    SRC_STATE *state = NULL;
    1.93  
    1.94 -    SDL_AudioStreamResamplerState_SRC *state = (SDL_AudioStreamResamplerState_SRC *)SDL_calloc(1, sizeof(*state));
    1.95 -    if (!state) {
    1.96 -        return SDL_FALSE;
    1.97 +    if (SRC_available) {
    1.98 +        state = SRC_src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result);
    1.99 +        if (!state) {
   1.100 +            SDL_SetError("src_new() failed: %s", SRC_src_strerror(result));
   1.101 +        }
   1.102      }
   1.103  
   1.104 -    if (!LoadLibSampleRate(state)) {
   1.105 -        SDL_free(state);
   1.106 +    if (!state) {
   1.107 +        SDL_CleanupAudioStreamResampler_SRC(stream);
   1.108          return SDL_FALSE;
   1.109      }
   1.110  
   1.111 @@ -743,16 +706,10 @@
   1.112      stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC;
   1.113      stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC;
   1.114  
   1.115 -    state->SRC_state = state->src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result);
   1.116 -    if (!state->SRC_state) {
   1.117 -        SDL_SetError("src_new() failed: %s", state->src_strerror(result));
   1.118 -        SDL_CleanupAudioStreamResampler_SRC(stream);
   1.119 -        return SDL_FALSE;
   1.120 -    }
   1.121      return SDL_TRUE;
   1.122  }
   1.123 +#endif /* HAVE_LIBSAMPLERATE_H */
   1.124  
   1.125 -#endif /* HAVE_LIBSAMPLERATE_H */
   1.126  
   1.127  typedef struct
   1.128  {