2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2024-01-01 14:15:26 -07:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#include <stdio.h> /* for fflush() and stdout */
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-12 06:07:18 -06:00
|
|
|
#include "testutils.h"
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
static SDL_AudioSpec spec;
|
2022-11-30 13:51:59 -07:00
|
|
|
static Uint8 *sound = NULL; /* Pointer to wave data */
|
|
|
|
static Uint32 soundlen = 0; /* Length of wave data */
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
/* these have to be in globals so the Emscripten port can see them in the mainloop. :/ */
|
|
|
|
static SDL_AudioStream *stream = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
static void loop(void)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2023-06-23 22:34:02 -06:00
|
|
|
if (SDL_GetAudioStreamAvailable(stream) == 0) {
|
|
|
|
SDL_Log("done.");
|
|
|
|
SDL_DestroyAudioStream(stream);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(sound);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
2023-06-23 22:34:02 -06:00
|
|
|
emscripten_cancel_main_loop();
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 03:40:07 -07:00
|
|
|
#endif
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
static void
|
2023-06-23 22:34:02 -06:00
|
|
|
test_multi_audio(SDL_AudioDeviceID *devices, int devcount)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
int keep_going = 1;
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_AudioStream **streams = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
int i;
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
#ifdef __ANDROID__ /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Event event;
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Create a Window to get fully initialized event processing for testing pause on Android. */
|
2023-03-05 15:44:38 -07:00
|
|
|
SDL_CreateWindow("testmultiaudio", 320, 240, 0);
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif
|
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
char *devname = SDL_GetAudioDeviceName(devices[i]);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-08-27 11:32:33 -06:00
|
|
|
if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
|
2015-06-21 09:33:46 -06:00
|
|
|
} else {
|
2023-08-27 14:34:26 -06:00
|
|
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_PutAudioStreamData(stream, sound, soundlen);
|
|
|
|
SDL_FlushAudioStream(stream);
|
2015-06-21 09:33:46 -06:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
2023-06-23 22:34:02 -06:00
|
|
|
while (SDL_GetAudioStreamAvailable(stream) > 0) {
|
2022-11-30 13:51:59 -07:00
|
|
|
#ifdef __ANDROID__
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Empty queue, some application events would prevent pause. */
|
2022-11-30 13:51:59 -07:00
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Delay(100);
|
|
|
|
}
|
|
|
|
#endif
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_Log("done.");
|
|
|
|
SDL_DestroyAudioStream(stream);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_free(devname);
|
|
|
|
stream = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
/* note that Emscripten currently doesn't run this part (but maybe only has a single audio device anyhow?) */
|
|
|
|
SDL_Log("Playing on all devices...\n");
|
|
|
|
streams = (SDL_AudioStream **) SDL_calloc(devcount, sizeof (SDL_AudioStream *));
|
|
|
|
if (!streams) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < devcount; i++) {
|
2023-08-27 11:32:33 -06:00
|
|
|
streams[i] = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL);
|
|
|
|
if (streams[i] == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed for device %d of %d: %s", i, devcount, SDL_GetError());
|
|
|
|
} else {
|
|
|
|
SDL_PutAudioStreamData(streams[i], sound, soundlen);
|
|
|
|
SDL_FlushAudioStream(streams[i]);
|
2023-06-23 22:34:02 -06:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
/* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
|
2015-06-21 09:33:46 -06:00
|
|
|
for (i = 0; i < devcount; i++) {
|
2023-08-27 11:32:33 -06:00
|
|
|
if (streams[i]) {
|
2023-08-27 14:34:26 -06:00
|
|
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(streams[i]));
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
2023-06-23 22:34:02 -06:00
|
|
|
|
|
|
|
while (keep_going) {
|
|
|
|
keep_going = 0;
|
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
|
|
|
|
keep_going = 1;
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
#ifdef __ANDROID__
|
2023-06-23 22:34:02 -06:00
|
|
|
/* Empty queue, some application events would prevent pause. */
|
|
|
|
while (SDL_PollEvent(&event)) {}
|
2022-11-30 13:51:59 -07:00
|
|
|
#endif
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_Delay(100);
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
SDL_DestroyAudioStream(streams[i]);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2023-06-23 22:34:02 -06:00
|
|
|
|
|
|
|
SDL_free(streams);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("All done!\n");
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char **argv)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_AudioDeviceID *devices = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
int devcount = 0;
|
2023-03-16 17:25:39 -06:00
|
|
|
int i;
|
|
|
|
char *filename = NULL;
|
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!state) {
|
2023-03-16 17:25:39 -06:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2015-11-25 13:39:28 -07:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (!filename) {
|
|
|
|
filename = argv[i];
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
|
|
|
static const char *options[] = { "[sample.wav]", NULL };
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Load the SDL library */
|
|
|
|
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-27 09:38:43 -07:00
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
filename = GetResourceFilename(filename, "sample.wav");
|
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
devices = SDL_GetAudioOutputDevices(&devcount);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!devices) {
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!");
|
2015-06-21 09:33:46 -06:00
|
|
|
} else {
|
|
|
|
/* Load the wave file into memory */
|
2023-06-23 22:34:02 -06:00
|
|
|
if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == -1) {
|
2023-03-16 17:25:39 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_GetError());
|
2015-06-21 09:33:46 -06:00
|
|
|
} else {
|
2023-06-23 22:34:02 -06:00
|
|
|
test_multi_audio(devices, devcount);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(sound);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 22:34:02 -06:00
|
|
|
SDL_free(devices);
|
2023-03-16 17:25:39 -06:00
|
|
|
SDL_free(filename);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2023-06-23 22:34:02 -06:00
|
|
|
|