test: Updated testaudiostreamdynamicresample to SDL3 audio API.

This commit is contained in:
Ryan C. Gordon
2023-06-24 01:44:12 -04:00
parent f883b9fc64
commit 5d4e9e5f80

View File

@@ -16,13 +16,6 @@
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h> #include <SDL3/SDL_test.h>
static void SDLCALL audio_callback(void *userdata, Uint8 * stream, int len)
{
SDL_AudioStream *audiostream = (SDL_AudioStream *) userdata;
SDL_memset(stream, 0, len);
SDL_GetAudioStreamData(audiostream, stream, len);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
SDL_Window *window; SDL_Window *window;
@@ -42,18 +35,17 @@ int main(int argc, char *argv[])
renderer = SDL_CreateRenderer(window, NULL, 0); renderer = SDL_CreateRenderer(window, NULL, 0);
SDL_LoadWAV("sample.wav", &spec, &audio_buf, &audio_len); SDL_LoadWAV("sample.wav", &spec, &audio_buf, &audio_len);
stream = SDL_CreateAudioStream(spec.format, spec.channels, spec.freq, spec.format, spec.channels, spec.freq); stream = SDL_CreateAudioStream(&spec, &spec);
SDL_PutAudioStreamData(stream, audio_buf, audio_len); SDL_PutAudioStreamData(stream, audio_buf, audio_len);
spec.callback = audio_callback; device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &spec);
spec.userdata = stream; SDL_BindAudioStream(device, stream);
device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, NULL, 0);
SDL_PlayAudioDevice(device);
slider_fill_area.w /= 2; slider_fill_area.w /= 2;
while (!done) { while (!done) {
SDL_Event e; SDL_Event e;
int newmultiplier = multiplier; int newmultiplier = multiplier;
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) { if (e.type == SDL_EVENT_QUIT) {
done = 1; done = 1;
@@ -74,6 +66,7 @@ int main(int argc, char *argv[])
} }
if (multiplier != newmultiplier) { if (multiplier != newmultiplier) {
SDL_AudioSpec newspec;
char title[64]; char title[64];
int newfreq = spec.freq; int newfreq = spec.freq;
@@ -94,13 +87,13 @@ int main(int argc, char *argv[])
newfreq = spec.freq + (int) (spec.freq * (multiplier / 100.0f)); newfreq = spec.freq + (int) (spec.freq * (multiplier / 100.0f));
} }
/* SDL_Log("newfreq=%d multiplier=%d\n", newfreq, multiplier); */ /* SDL_Log("newfreq=%d multiplier=%d\n", newfreq, multiplier); */
SDL_LockAudioDevice(device); SDL_memcpy(&newspec, &spec, sizeof (spec));
SDL_SetAudioStreamFormat(stream, spec.format, spec.channels, newfreq, spec.format, spec.channels, spec.freq); newspec.freq = newfreq;
SDL_UnlockAudioDevice(device); SDL_SetAudioStreamFormat(stream, &newspec, NULL);
} }
/* keep it looping. */ /* keep it looping. */
if (SDL_GetAudioStreamAvailable(stream) < (1024 * 100)) { if (SDL_GetAudioStreamAvailable(stream) < (audio_len / 2)) {
SDL_PutAudioStreamData(stream, audio_buf, audio_len); SDL_PutAudioStreamData(stream, audio_buf, audio_len);
} }
@@ -116,6 +109,7 @@ int main(int argc, char *argv[])
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_CloseAudioDevice(device); SDL_CloseAudioDevice(device);
SDL_DestroyAudioStream(stream);
SDL_free(audio_buf); SDL_free(audio_buf);
SDL_Quit(); SDL_Quit();
return 0; return 0;