test: Update testsurround for SDL3 audio API.

This commit is contained in:
Ryan C. Gordon 2023-06-24 15:41:20 -04:00
parent e1c78718d4
commit 3a02eecced
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 43 additions and 16 deletions

View File

@ -96,20 +96,23 @@ static SDL_bool is_lfe_channel(int channel_index, int channel_count)
return (channel_count == 3 && channel_index == 2) || (channel_count >= 6 && channel_index == 3); return (channel_count == 3 && channel_index == 2) || (channel_count >= 6 && channel_index == 3);
} }
static void SDLCALL fill_buffer(void *unused, Uint8 *stream, int len) static void SDLCALL fill_buffer(SDL_AudioStream *stream, int len, void *unused)
{ {
Sint16 *buffer = (Sint16 *)stream; const int samples = len / sizeof(Sint16);
int samples = len / sizeof(Sint16); Sint16 *buffer = NULL;
static int total_samples = 0; static int total_samples = 0;
int i; int i;
SDL_memset(stream, 0, len);
/* This can happen for a short time when switching devices */ /* This can happen for a short time when switching devices */
if (active_channel == total_channels) { if (active_channel == total_channels) {
return; return;
} }
buffer = (Sint16 *) SDL_calloc(samples, sizeof(Sint16));
if (!buffer) {
return; /* oh well. */
}
/* Play a sine wave on the active channel only */ /* Play a sine wave on the active channel only */
for (i = active_channel; i < samples; i += total_channels) { for (i = active_channel; i < samples; i += total_channels) {
float time = (float)total_samples++ / SAMPLE_RATE_HZ; float time = (float)total_samples++ / SAMPLE_RATE_HZ;
@ -134,12 +137,18 @@ static void SDLCALL fill_buffer(void *unused, Uint8 *stream, int len)
break; break;
} }
} }
SDL_PutAudioStreamData(stream, buffer, samples * sizeof (Sint16));
SDL_free(buffer);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i; SDL_AudioDeviceID *devices = NULL;
SDLTest_CommonState *state; SDLTest_CommonState *state;
int devcount = 0;
int i;
/* Initialize test framework */ /* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0); state = SDLTest_CommonCreateState(argv, 0);
@ -168,38 +177,53 @@ int main(int argc, char *argv[])
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
for (i = 0; i < SDL_GetNumAudioDevices(0); i++) { devices = SDL_GetAudioOutputDevices(&devcount);
const char *devname = SDL_GetAudioDeviceName(i, 0); if (!devices) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioOutputDevices() failed: %s\n", SDL_GetError());
devcount = 0;
}
for (i = 0; i < devcount; i++) {
SDL_AudioStream *stream = NULL;
char *devname = SDL_GetAudioDeviceName(devices[i]);
int j; int j;
SDL_AudioSpec spec; SDL_AudioSpec spec;
SDL_AudioDeviceID dev; SDL_AudioDeviceID dev;
if (SDL_GetAudioDeviceSpec(i, 0, &spec) != 0) { SDL_Log("Testing audio device: %s\n", devname);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioSpec() failed: %s\n", SDL_GetError()); SDL_free(devname);
if (SDL_GetAudioDeviceFormat(devices[i], &spec) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioDeviceFormat() failed: %s\n", SDL_GetError());
continue; continue;
} }
SDL_Log(" (%d channels)\n", spec.channels);
spec.freq = SAMPLE_RATE_HZ; spec.freq = SAMPLE_RATE_HZ;
spec.format = SDL_AUDIO_S16SYS; spec.format = SDL_AUDIO_S16SYS;
spec.samples = 4096;
spec.callback = fill_buffer;
dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0); dev = SDL_OpenAudioDevice(devices[i], &spec);
if (dev == 0) { if (dev == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice() failed: %s\n", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice() failed: %s\n", SDL_GetError());
continue; continue;
} }
SDL_Log("Testing audio device: %s (%d channels)\n", devname, spec.channels); stream = SDL_CreateAndBindAudioStream(dev, &spec);
if (stream == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateAndBindAudioStream() failed: %s\n", SDL_GetError());
SDL_CloseAudioDevice(dev);
continue;
}
/* These are used by the fill_buffer callback */ /* These are used by the fill_buffer callback */
total_channels = spec.channels; total_channels = spec.channels;
active_channel = 0; active_channel = 0;
SDL_PlayAudioDevice(dev); SDL_SetAudioStreamGetCallback(stream, fill_buffer, NULL);
for (j = 0; j < total_channels; j++) { for (j = 0; j < total_channels; j++) {
int sine_freq = is_lfe_channel(j, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ; const int sine_freq = is_lfe_channel(j, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
SDL_Log("Playing %d Hz test tone on channel: %s\n", sine_freq, get_channel_name(j, total_channels)); SDL_Log("Playing %d Hz test tone on channel: %s\n", sine_freq, get_channel_name(j, total_channels));
@ -212,8 +236,11 @@ int main(int argc, char *argv[])
} }
SDL_CloseAudioDevice(dev); SDL_CloseAudioDevice(dev);
SDL_DestroyAudioStream(stream);
} }
SDL_free(devices);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }