2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2022-01-03 10:40:00 -07:00
|
|
|
Copyright (C) 1997-2022 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Program to load a wave file and loop playing it using SDL sound queueing */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
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>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-12 06:07:18 -06:00
|
|
|
#include "testutils.h"
|
|
|
|
|
2017-02-19 13:05:42 -07:00
|
|
|
static struct
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
SDL_AudioSpec spec;
|
2022-11-30 13:51:59 -07:00
|
|
|
Uint8 *sound; /* Pointer to wave data */
|
|
|
|
Uint32 soundlen; /* Length of wave data */
|
2015-06-21 09:33:46 -06:00
|
|
|
} wave;
|
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int done = 0;
|
2022-11-30 13:51:59 -07:00
|
|
|
void poked(int sig)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
|
2023-01-05 01:57:14 -07:00
|
|
|
static SDL_AudioDeviceID g_audio_id = 0;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
void loop()
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
#ifdef __EMSCRIPTEN__
|
2023-01-05 01:57:14 -07:00
|
|
|
if (done || (SDL_GetAudioDeviceStatus(g_audio_id) != SDL_AUDIO_PLAYING)) {
|
2015-06-21 09:33:46 -06:00
|
|
|
emscripten_cancel_main_loop();
|
2022-11-27 09:38:43 -07:00
|
|
|
} else
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* The device from SDL_OpenAudio() is always device #1. */
|
2023-01-05 01:57:14 -07:00
|
|
|
const Uint32 queued = SDL_GetQueuedAudioSize(g_audio_id);
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Device has %u bytes queued.\n", (unsigned int)queued);
|
|
|
|
if (queued <= 8192) { /* time to requeue the whole thing? */
|
2023-01-05 01:57:14 -07:00
|
|
|
if (SDL_QueueAudio(g_audio_id, wave.sound, wave.soundlen) == 0) {
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Device queued %u more bytes.\n", (unsigned int)wave.soundlen);
|
2015-06-21 09:33:46 -06:00
|
|
|
} else {
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int)wave.soundlen, SDL_GetError());
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2022-04-12 06:07:18 -06:00
|
|
|
char *filename = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2015-11-25 13:39:28 -07:00
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
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
|
|
|
}
|
|
|
|
|
2022-04-12 06:07:18 -06:00
|
|
|
filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
|
|
|
|
|
|
|
|
if (filename == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
|
|
|
|
quit(1);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-04-12 06:07:18 -06:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Load the wave file into memory */
|
|
|
|
if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
wave.spec.callback = NULL; /* we'll push audio. */
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
/* Set the signals */
|
|
|
|
#ifdef SIGHUP
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGHUP, poked);
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGINT, poked);
|
2015-06-21 09:33:46 -06:00
|
|
|
#ifdef SIGQUIT
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGQUIT, poked);
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGTERM, poked);
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif /* HAVE_SIGNAL_H */
|
|
|
|
|
|
|
|
/* Initialize fillerup() variables */
|
2023-01-05 01:57:14 -07:00
|
|
|
g_audio_id = SDL_OpenAudioDevice(NULL, 0, &wave.spec, NULL, 0);
|
|
|
|
|
|
|
|
if (g_audio_id <= 0) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(wave.sound);
|
2015-06-21 09:33:46 -06:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
|
|
|
|
|
|
|
|
/* Let the audio run */
|
2023-01-06 05:59:45 -07:00
|
|
|
SDL_PlayAudioDevice(g_audio_id);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
done = 0;
|
|
|
|
|
|
|
|
/* Note that we stuff the entire audio buffer into the queue in one
|
|
|
|
shot. Most apps would want to feed it a little at a time, as it
|
|
|
|
plays, but we're going for simplicity here. */
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
2023-01-05 01:57:14 -07:00
|
|
|
while (!done && (SDL_GetAudioDeviceStatus(g_audio_id) == SDL_AUDIO_PLAYING)) {
|
2015-06-21 09:33:46 -06:00
|
|
|
loop();
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Delay(100); /* let it play for awhile. */
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clean up on signal */
|
2023-01-05 01:57:14 -07:00
|
|
|
SDL_CloseAudioDevice(g_audio_id);
|
2022-12-27 05:22:43 -07:00
|
|
|
SDL_free(wave.sound);
|
2022-04-12 06:07:18 -06:00
|
|
|
SDL_free(filename);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
|
|
}
|