src/audio/haiku/SDL_haikuaudio.cc
changeset 10762 4b1b1918a246
parent 10737 3406a0f8b041
child 10764 f9bf759e9dd1
     1.1 --- a/src/audio/haiku/SDL_haikuaudio.cc	Fri Jan 06 00:49:35 2017 -0500
     1.2 +++ b/src/audio/haiku/SDL_haikuaudio.cc	Fri Jan 06 00:50:01 2017 -0500
     1.3 @@ -36,6 +36,7 @@
     1.4  #include "../SDL_audio_c.h"
     1.5  #include "../SDL_sysaudio.h"
     1.6  #include "SDL_haikuaudio.h"
     1.7 +#include "SDL_assert.h"
     1.8  
     1.9  }
    1.10  
    1.11 @@ -47,26 +48,39 @@
    1.12            const media_raw_audio_format & format)
    1.13  {
    1.14      SDL_AudioDevice *audio = (SDL_AudioDevice *) device;
    1.15 +    SDL_AudioCallback callback = audio->spec.callback;
    1.16  
    1.17 -    /* Only do soemthing if audio is enabled */
    1.18 -    if (!SDL_AtomicGet(&audio->enabled)) {
    1.19 +    /* Only do something if audio is enabled */
    1.20 +    if (!SDL_AtomicGet(&audio->enabled) || SDL_AtomicGet(&audio->paused)) {
    1.21 +        if (audio->stream) {
    1.22 +            SDL_AudioStreamClear(audio->stream);
    1.23 +        }
    1.24 +        SDL_memset(stream, audio->spec.silence, len);
    1.25          return;
    1.26      }
    1.27  
    1.28 -    if (!SDL_AtomicGet(&audio->paused)) {
    1.29 -        if (audio->convert.needed) {
    1.30 -            SDL_LockMutex(audio->mixer_lock);
    1.31 -            (*audio->spec.callback) (audio->spec.userdata,
    1.32 -                                     (Uint8 *) audio->convert.buf,
    1.33 -                                     audio->convert.len);
    1.34 -            SDL_UnlockMutex(audio->mixer_lock);
    1.35 -            SDL_ConvertAudio(&audio->convert);
    1.36 -            SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
    1.37 -        } else {
    1.38 -            SDL_LockMutex(audio->mixer_lock);
    1.39 -            (*audio->spec.callback) (audio->spec.userdata,
    1.40 -                                     (Uint8 *) stream, len);
    1.41 -            SDL_UnlockMutex(audio->mixer_lock);
    1.42 +    SDL_assert(audio->spec.size == len);
    1.43 +
    1.44 +    if (audio->stream == NULL) {  /* no conversion necessary. */
    1.45 +        SDL_LockMutex(audio->mixer_lock);
    1.46 +        callback(audio->spec.userdata, stream, len);
    1.47 +        SDL_UnlockMutex(audio->mixer_lock);
    1.48 +    } else {  /* streaming/converting */
    1.49 +        const int stream_len = audio->callbackspec.size;
    1.50 +        const int ilen = (int) len;
    1.51 +        while (SDL_AudioStreamAvailable(audio->stream) < ilen) {
    1.52 +            callback(audio->spec.userdata, audio->fake_stream, stream_len);
    1.53 +            if (SDL_AudioStreamPut(audio->stream, audio->fake_stream, stream_len) == -1) {
    1.54 +                SDL_AudioStreamClear(audio->stream);
    1.55 +                SDL_AtomicSet(&audio->enabled, 0);
    1.56 +                break;
    1.57 +            }
    1.58 +        }
    1.59 +
    1.60 +        const int got = SDL_AudioStreamGet(audio->stream, ilen, stream, ilen);
    1.61 +        SDL_assert((got < 0) || (got == ilen));
    1.62 +        if (got != ilen) {
    1.63 +            SDL_memset(stream, audio->spec.silence, len);
    1.64          }
    1.65      }
    1.66  }