mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-12-11 17:12:31 +00:00
Clarify documentation for audio callback migration
This commit is contained in:
@@ -62,7 +62,7 @@ In SDL2, you might have done something like this to play audio...
|
|||||||
```c
|
```c
|
||||||
void SDLCALL MyAudioCallback(void *userdata, Uint8 * stream, int len)
|
void SDLCALL MyAudioCallback(void *userdata, Uint8 * stream, int len)
|
||||||
{
|
{
|
||||||
/* calculate a little more audio here, maybe using `userdata`, write it to `stream` */
|
/* Calculate a little more audio here, maybe using `userdata`, write it to `stream` */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ...somewhere near startup... */
|
/* ...somewhere near startup... */
|
||||||
@@ -81,15 +81,25 @@ In SDL2, you might have done something like this to play audio...
|
|||||||
...in SDL3, you can do this...
|
...in SDL3, you can do this...
|
||||||
|
|
||||||
```c
|
```c
|
||||||
void SDLCALL MyAudioCallback(SDL_AudioStream *stream, int len, void *userdata)
|
void SDLCALL MyAudioCallback3(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
|
||||||
{
|
{
|
||||||
/* calculate a little more audio here, maybe using `userdata`, write it to `stream` */
|
/* Calculate a little more audio here, maybe using `userdata`, write it to `stream`
|
||||||
SDL_PutAudioStreamData(stream, newdata, len);
|
*
|
||||||
|
* If you want to reuse the original callback, you could do something like this:
|
||||||
|
*/
|
||||||
|
if (additional_amount > 0) {
|
||||||
|
Uint16 *data = SDL_stack_alloc(Uint16, additional_amount / sizeof(Uint16));
|
||||||
|
if (data) {
|
||||||
|
MyAudioCallback(userdata, data, additional_amount);
|
||||||
|
SDL_PutAudioStreamData(stream, data, additional_amount);
|
||||||
|
SDL_stack_free(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ...somewhere near startup... */
|
/* ...somewhere near startup... */
|
||||||
const SDL_AudioSpec spec = { SDL_AUDIO_S16, 2, 44100 };
|
const SDL_AudioSpec spec = { SDL_AUDIO_S16, 2, 44100 };
|
||||||
SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &spec, MyAudioCallback, &my_audio_callback_user_data);
|
SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &spec, MyAudioCallback3, &my_audio_callback_user_data);
|
||||||
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user