2
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
13
#include <SDL3/SDL_main.h>
14
#include <SDL3/SDL_test.h>
18
#ifdef SDL_PLATFORM_EMSCRIPTEN
19
#include <emscripten/emscripten.h>
22
#include <stdio.h> /* for fflush() and stdout */
24
static SDL_AudioSpec spec;
25
static Uint8 *sound = NULL; /* Pointer to wave data */
26
static Uint32 soundlen = 0; /* Length of wave data */
28
/* these have to be in globals so the Emscripten port can see them in the mainloop. :/ */
29
static SDL_AudioStream *stream = NULL;
32
#ifdef SDL_PLATFORM_EMSCRIPTEN
35
if (SDL_GetAudioStreamAvailable(stream) == 0) {
37
SDL_DestroyAudioStream(stream);
40
emscripten_cancel_main_loop();
46
test_multi_audio(const SDL_AudioDeviceID *devices, int devcount)
49
SDL_AudioStream **streams = NULL;
52
#ifdef SDL_PLATFORM_ANDROID /* !!! FIXME: maybe always create a window, in the SDLTest layer, so these #ifdefs don't have to be here? */
55
/* Create a Window to get fully initialized event processing for testing pause on Android. */
56
SDL_CreateWindow("testmultiaudio", 320, 240, 0);
59
for (i = 0; i < devcount; i++) {
60
const char *devname = SDL_GetAudioDeviceName(devices[i]);
62
SDL_Log("Playing on device #%d of %d: id=%u, name='%s'...", i, devcount, (unsigned int) devices[i], devname);
64
if ((stream = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL)) == NULL) {
65
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed: %s", SDL_GetError());
67
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
68
SDL_PutAudioStreamData(stream, sound, soundlen);
69
SDL_FlushAudioStream(stream);
70
#ifdef SDL_PLATFORM_EMSCRIPTEN
71
emscripten_set_main_loop(loop, 0, 1);
73
while (SDL_GetAudioStreamAvailable(stream) > 0) {
74
#ifdef SDL_PLATFORM_ANDROID
75
/* Empty queue, some application events would prevent pause. */
76
while (SDL_PollEvent(&event)) {
83
SDL_DestroyAudioStream(stream);
88
/* note that Emscripten currently doesn't run this part (but maybe only has a single audio device anyhow?) */
89
SDL_Log("Playing on all devices...\n");
90
streams = (SDL_AudioStream **) SDL_calloc(devcount, sizeof (SDL_AudioStream *));
92
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
94
for (i = 0; i < devcount; i++) {
95
streams[i] = SDL_OpenAudioDeviceStream(devices[i], &spec, NULL, NULL);
96
if (streams[i] == NULL) {
97
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Audio stream creation failed for device %d of %d: %s", i, devcount, SDL_GetError());
99
SDL_PutAudioStreamData(streams[i], sound, soundlen);
100
SDL_FlushAudioStream(streams[i]);
104
/* try to start all the devices about the same time. SDL does not guarantee sync across physical devices. */
105
for (i = 0; i < devcount; i++) {
107
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(streams[i]));
113
for (i = 0; i < devcount; i++) {
114
if (streams[i] && (SDL_GetAudioStreamAvailable(streams[i]) > 0)) {
118
#ifdef SDL_PLATFORM_ANDROID
119
/* Empty queue, some application events would prevent pause. */
120
while (SDL_PollEvent(&event)) {}
126
for (i = 0; i < devcount; i++) {
127
SDL_DestroyAudioStream(streams[i]);
133
SDL_Log("All done!\n");
136
int main(int argc, char **argv)
138
SDL_AudioDeviceID *devices;
141
char *filename = NULL;
142
SDLTest_CommonState *state;
144
/* Initialize test framework */
145
state = SDLTest_CommonCreateState(argv, 0);
150
/* Enable standard application logging */
151
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
153
/* Parse commandline */
154
for (i = 1; i < argc;) {
157
consumed = SDLTest_CommonArg(state, i);
165
static const char *options[] = { "[sample.wav]", NULL };
166
SDLTest_CommonLogUsage(state, argv[0], options);
173
/* Load the SDL library */
174
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
175
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
179
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
181
filename = GetResourceFilename(filename, "sample.wav");
183
devices = SDL_GetAudioPlaybackDevices(&devcount);
185
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio playback devices!");
187
/* Load the wave file into memory */
188
if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) < 0) {
189
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename,
192
test_multi_audio(devices, devcount);
201
SDLTest_CommonDestroyState(state);