mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-19 11:18:27 +00:00
audio: SDL_AudioStream now has callbacks for Get and Put operations.
This allows code to feed a stream (or feed from a stream) on-demand, which is to say: it can efficiently simulate the SDL2 audio callback.
This commit is contained in:
parent
905c4fff5b
commit
56b1bc2198
8 changed files with 286 additions and 10 deletions
|
@ -33,11 +33,36 @@ static struct
|
|||
int freq;
|
||||
Uint8 *sound; /* Pointer to wave data */
|
||||
Uint32 soundlen; /* Length of wave data */
|
||||
Uint32 soundpos;
|
||||
} wave;
|
||||
|
||||
static SDL_AudioDeviceID device;
|
||||
static SDL_AudioStream *stream;
|
||||
|
||||
static void SDLCALL
|
||||
fillerup(SDL_AudioStream *stream, int len, void *unused)
|
||||
{
|
||||
Uint8 *waveptr;
|
||||
int waveleft;
|
||||
|
||||
/*SDL_Log("CALLBACK WANTS %d MORE BYTES!", len);*/
|
||||
|
||||
/* Set up the pointers */
|
||||
waveptr = wave.sound + wave.soundpos;
|
||||
waveleft = wave.soundlen - wave.soundpos;
|
||||
|
||||
/* Go! */
|
||||
while (waveleft <= len) {
|
||||
SDL_PutAudioStreamData(stream, waveptr, waveleft);
|
||||
len -= waveleft;
|
||||
waveptr = wave.sound;
|
||||
waveleft = wave.soundlen;
|
||||
wave.soundpos = 0;
|
||||
}
|
||||
SDL_PutAudioStreamData(stream, waveptr, len);
|
||||
wave.soundpos += len;
|
||||
}
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void
|
||||
quit(int rc)
|
||||
|
@ -78,6 +103,8 @@ open_audio(void)
|
|||
SDL_free(wave.sound);
|
||||
quit(2);
|
||||
}
|
||||
|
||||
SDL_SetAudioStreamGetCallback(stream, fillerup, NULL);
|
||||
}
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
|
@ -91,12 +118,6 @@ static void reopen_audio(void)
|
|||
|
||||
static int done = 0;
|
||||
|
||||
static void fillerup(void)
|
||||
{
|
||||
if (SDL_GetAudioStreamAvailable(stream) < (wave.soundlen / 2)) {
|
||||
SDL_PutAudioStreamData(stream, wave.sound, wave.soundlen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
|
@ -192,7 +213,6 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
fillerup();
|
||||
SDL_Delay(100);
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue