src/SDL12_compat.c
changeset 95 2fa2951e5c96
parent 94 ed05888ca3eb
child 96 4735fcd0fd8a
     1.1 --- a/src/SDL12_compat.c	Fri Mar 01 10:52:47 2019 -0500
     1.2 +++ b/src/SDL12_compat.c	Fri Mar 01 10:53:25 2019 -0500
     1.3 @@ -3638,4 +3638,60 @@
     1.4      return retval;
     1.5  }
     1.6  
     1.7 +typedef struct
     1.8 +{
     1.9 +    void (SDLCALL *app_callback)(void *userdata, Uint8 *stream, int len);
    1.10 +    void *app_userdata;
    1.11 +    Uint8 silence;
    1.12 +} AudioCallbackWrapperData;
    1.13 +
    1.14 +static void SDLCALL
    1.15 +AudioCallbackWrapper(void *userdata, Uint8 *stream, int len)
    1.16 +{
    1.17 +    AudioCallbackWrapperData *data = (AudioCallbackWrapperData *) userdata;
    1.18 +    SDL_memset(stream, data->silence, len);  // SDL2 doesn't clear the stream before calling in here, but 1.2 expects it.
    1.19 +    data->app_callback(data->app_userdata, stream, len);
    1.20 +}
    1.21 +
    1.22 +
    1.23 +DECLSPEC int SDLCALL
    1.24 +SDL_OpenAudio(SDL_AudioSpec *want, SDL_AudioSpec *obtained)
    1.25 +{
    1.26 +    // SDL2 uses a NULL callback to mean "we play to use SDL_QueueAudio()"
    1.27 +    if (want && (want->callback == NULL)) {
    1.28 +        return SDL20_SetError("Callback can't be NULL");
    1.29 +    }
    1.30 +
    1.31 +    AudioCallbackWrapperData *data = (AudioCallbackWrapperData *) SDL_calloc(1, sizeof (AudioCallbackWrapperData));
    1.32 +    if (!data) {
    1.33 +        return SDL20_SetError("Out of memory");
    1.34 +    }
    1.35 +    data->app_callback = want->callback;
    1.36 +    data->app_userdata = want->userdata;
    1.37 +    want->callback = AudioCallbackWrapper;
    1.38 +    want->userdata = data;
    1.39 +
    1.40 +    FIXME("Don't allow int32 or float32");
    1.41 +    FIXME("clamp output to mono/stereo");
    1.42 +    const int retval = SDL20_OpenAudio(want, obtained);
    1.43 +    want->callback = data->app_callback;
    1.44 +    want->userdata = data->app_userdata;
    1.45 +    if (retval == -1) {
    1.46 +        SDL_free(data);
    1.47 +    } else {
    1.48 +        FIXME("memory leak on callback data");
    1.49 +        if (!obtained) {
    1.50 +            data->silence = want->silence;
    1.51 +        } else {
    1.52 +            data->silence = obtained->silence;
    1.53 +            obtained->callback = data->app_callback;
    1.54 +            obtained->userdata = data->app_userdata;
    1.55 +        }
    1.56 +    }
    1.57 +
    1.58 +    return retval;
    1.59 +}
    1.60 +
    1.61 +
    1.62 +
    1.63  /* vi: set ts=4 sw=4 expandtab: */