| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 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. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Program to load a wave file and loop playing it using SDL audio */ |
| 14 |
|
| 15 |
/* loopwaves.c is much more robust in handling WAVE files -- |
| 16 |
This is only for simple WAVEs |
| 17 |
*/ |
| 18 |
#include "SDL_config.h" |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#ifdef __EMSCRIPTEN__ |
| 24 |
#include <emscripten/emscripten.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "SDL.h" |
| 28 |
#include "testutils.h" |
| 29 |
|
| 30 |
static struct |
| 31 |
{ |
| 32 |
SDL_AudioSpec spec; |
| 33 |
Uint8 *sound; /* Pointer to wave data */ |
| 34 |
Uint32 soundlen; /* Length of wave data */ |
| 35 |
int soundpos; /* Current play position */ |
| 36 |
} wave; |
| 37 |
|
| 38 |
static SDL_AudioDeviceID device; |
| 39 |
|
| 40 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 41 |
static void |
| 42 |
quit(int rc) |
| 43 |
{ |
| 44 |
SDL_Quit(); |
| 45 |
exit(rc); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
close_audio(void) |
| 50 |
{ |
| 51 |
if (device != 0) { |
| 52 |
SDL_CloseAudioDevice(device); |
| 53 |
device = 0; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
open_audio(void) |
| 59 |
{ |
| 60 |
/* Initialize fillerup() variables */ |
| 61 |
device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0); |
| 62 |
if (!device) { |
| 63 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError()); |
| 64 |
SDL_FreeWAV(wave.sound); |
| 65 |
quit(2); |
| 66 |
} |
| 67 |
|
| 68 |
/* Let the audio run */ |
| 69 |
SDL_PauseAudioDevice(device, SDL_FALSE); |
| 70 |
} |
| 71 |
|
| 72 |
#ifndef __EMSCRIPTEN__ |
| 73 |
static void reopen_audio(void) |
| 74 |
{ |
| 75 |
close_audio(); |
| 76 |
open_audio(); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
void SDLCALL |
| 81 |
fillerup(void *unused, Uint8 *stream, int len) |
| 82 |
{ |
| 83 |
Uint8 *waveptr; |
| 84 |
int waveleft; |
| 85 |
|
| 86 |
/* Set up the pointers */ |
| 87 |
waveptr = wave.sound + wave.soundpos; |
| 88 |
waveleft = wave.soundlen - wave.soundpos; |
| 89 |
|
| 90 |
/* Go! */ |
| 91 |
while (waveleft <= len) { |
| 92 |
SDL_memcpy(stream, waveptr, waveleft); |
| 93 |
stream += waveleft; |
| 94 |
len -= waveleft; |
| 95 |
waveptr = wave.sound; |
| 96 |
waveleft = wave.soundlen; |
| 97 |
wave.soundpos = 0; |
| 98 |
} |
| 99 |
SDL_memcpy(stream, waveptr, len); |
| 100 |
wave.soundpos += len; |
| 101 |
} |
| 102 |
|
| 103 |
static int done = 0; |
| 104 |
|
| 105 |
#ifdef __EMSCRIPTEN__ |
| 106 |
void loop(void) |
| 107 |
{ |
| 108 |
if (done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING)) { |
| 109 |
emscripten_cancel_main_loop(); |
| 110 |
} |
| 111 |
} |
| 112 |
#endif |
| 113 |
|
| 114 |
int main(int argc, char *argv[]) |
| 115 |
{ |
| 116 |
int i; |
| 117 |
char *filename = NULL; |
| 118 |
|
| 119 |
/* Enable standard application logging */ |
| 120 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 121 |
|
| 122 |
/* Load the SDL library */ |
| 123 |
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) { |
| 124 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav"); |
| 129 |
|
| 130 |
if (!filename) { |
| 131 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError()); |
| 132 |
quit(1); |
| 133 |
} |
| 134 |
|
| 135 |
/* Load the wave file into memory */ |
| 136 |
if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) { |
| 137 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError()); |
| 138 |
quit(1); |
| 139 |
} |
| 140 |
|
| 141 |
wave.spec.callback = fillerup; |
| 142 |
|
| 143 |
/* Show the list of available drivers */ |
| 144 |
SDL_Log("Available audio drivers:"); |
| 145 |
for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) { |
| 146 |
SDL_Log("%i: %s", i, SDL_GetAudioDriver(i)); |
| 147 |
} |
| 148 |
|
| 149 |
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); |
| 150 |
|
| 151 |
open_audio(); |
| 152 |
|
| 153 |
SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED); |
| 154 |
|
| 155 |
#ifdef __EMSCRIPTEN__ |
| 156 |
emscripten_set_main_loop(loop, 0, 1); |
| 157 |
#else |
| 158 |
while (!done) { |
| 159 |
SDL_Event event; |
| 160 |
|
| 161 |
while (SDL_PollEvent(&event) > 0) { |
| 162 |
if (event.type == SDL_QUIT) { |
| 163 |
done = 1; |
| 164 |
} |
| 165 |
if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) || |
| 166 |
(event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) { |
| 167 |
reopen_audio(); |
| 168 |
} |
| 169 |
} |
| 170 |
SDL_Delay(100); |
| 171 |
} |
| 172 |
#endif |
| 173 |
|
| 174 |
/* Clean up on signal */ |
| 175 |
close_audio(); |
| 176 |
SDL_FreeWAV(wave.sound); |
| 177 |
SDL_free(filename); |
| 178 |
SDL_Quit(); |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
/* vi: set ts=4 sw=4 expandtab: */ |