2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03: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-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 02:25:39 +03:00
|
|
|
#include <SDL3/SDL_test.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#include <stdio.h> /* for fflush() and stdout */
|
2022-04-12 15:07:18 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
static SDL_AudioSpec spec;
|
2022-11-30 23:51:59 +03:00
|
|
|
static Uint8 *sound = NULL; /* Pointer to wave data */
|
|
|
|
static Uint32 soundlen = 0; /* Length of wave data */
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-06-24 07:34:02 +03: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 18:33:46 +03:00
|
|
|
|
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2023-03-08 13:40:07 +03:00
|
|
|
static void loop(void)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2023-06-24 07:34:02 +03:00
|
|
|
if (SDL_GetAudioStreamAvailable(stream) == 0) {
|
|
|
|
SDL_Log("done.");
|
|
|
|
SDL_DestroyAudioStream(stream);
|
2022-12-27 15:22:43 +03:00
|
|
|
SDL_free(sound);
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Quit();
|
2023-06-24 07:34:02 +03:00
|
|
|
emscripten_cancel_main_loop();
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 13:40:07 +03:00
|
|
|
#endif
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
static void
|
2023-06-24 07:34:02 +03:00
|
|
|
test_multi_audio(SDL_AudioDeviceID *devices, int devcount)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
int keep_going = 1;
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_AudioStream **streams = NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
int i;
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_ANDROID /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Event event;
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
/* Create a Window to get fully initialized event processing for testing pause on Android. */
|
2023-03-06 01:44:38 +03:00
|
|
|
SDL_CreateWindow("testmultiaudio", 320, 240, 0);
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
char *devname = SDL_GetAudioDeviceName(devices[i]);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-08-27 20:32:33 +03:00
|
|
|
if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
|
2015-06-21 18:33:46 +03:00
|
|
|
} else {
|
2023-08-27 23:34:26 +03:00
|
|
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_PutAudioStreamData(stream, sound, soundlen);
|
|
|
|
SDL_FlushAudioStream(stream);
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
2023-06-24 07:34:02 +03:00
|
|
|
while (SDL_GetAudioStreamAvailable(stream) > 0) {
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_ANDROID
|
2015-06-21 18:33:46 +03:00
|
|
|
/* Empty queue, some application events would prevent pause. */
|
2022-11-30 23:51:59 +03:00
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Delay(100);
|
|
|
|
}
|
|
|
|
#endif
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_Log("done.");
|
|
|
|
SDL_DestroyAudioStream(stream);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_free(devname);
|
|
|
|
stream = NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:34:02 +03: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 20:32:33 +03: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-24 07:34:02 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
/* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
|
2015-06-21 18:33:46 +03:00
|
|
|
for (i = 0; i < devcount; i++) {
|
2023-08-27 20:32:33 +03:00
|
|
|
if (streams[i]) {
|
2023-08-27 23:34:26 +03:00
|
|
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(streams[i]));
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-24 07:34:02 +03:00
|
|
|
|
|
|
|
while (keep_going) {
|
|
|
|
keep_going = 0;
|
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
|
|
|
|
keep_going = 1;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_ANDROID
|
2023-06-24 07:34:02 +03:00
|
|
|
/* Empty queue, some application events would prevent pause. */
|
|
|
|
while (SDL_PollEvent(&event)) {}
|
2022-11-30 23:51:59 +03:00
|
|
|
#endif
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_Delay(100);
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
SDL_DestroyAudioStream(streams[i]);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2023-06-24 07:34:02 +03:00
|
|
|
|
|
|
|
SDL_free(streams);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("All done!\n");
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
int main(int argc, char **argv)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_AudioDeviceID *devices = NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
int devcount = 0;
|
2023-03-17 02:25:39 +03:00
|
|
|
int i;
|
|
|
|
char *filename = NULL;
|
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2023-03-17 02:25:39 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2015-11-25 23:39:28 +03:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2023-03-17 02:25:39 +03: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 18:33:46 +03: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 19:38:43 +03:00
|
|
|
return 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
filename = GetResourceFilename(filename, "sample.wav");
|
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
devices = SDL_GetAudioOutputDevices(&devcount);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!devices) {
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!");
|
2015-06-21 18:33:46 +03:00
|
|
|
} else {
|
|
|
|
/* Load the wave file into memory */
|
2023-06-24 07:34:02 +03:00
|
|
|
if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == -1) {
|
2023-03-17 02:25:39 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_GetError());
|
2015-06-21 18:33:46 +03:00
|
|
|
} else {
|
2023-06-24 07:34:02 +03:00
|
|
|
test_multi_audio(devices, devcount);
|
2022-12-27 15:22:43 +03:00
|
|
|
SDL_free(sound);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 07:34:02 +03:00
|
|
|
SDL_free(devices);
|
2023-03-17 02:25:39 +03:00
|
|
|
SDL_free(filename);
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Quit();
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2023-06-24 07:34:02 +03:00
|
|
|
|