SDL  2.0
loopwave.c File Reference
#include "SDL_config.h"
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
+ Include dependency graph for loopwave.c:

Go to the source code of this file.

Functions

static void quit (int rc)
static void close_audio ()
static void open_audio ()
static void reopen_audio ()
void fillerup (void *unused, Uint8 *stream, int len)
int main (int argc, char *argv[])

Variables

struct {
   SDL_AudioSpec   spec
   Uint8 *   sound
   Uint32   soundlen
   int   soundpos
wave
static SDL_AudioDeviceID device
static int done = 0

Function Documentation

static void close_audio ( )
static

Definition at line 48 of file loopwave.c.

References device, and SDL_CloseAudioDevice.

Referenced by main(), and reopen_audio().

{
if (device != 0) {
device = 0;
}
}
void fillerup ( void unused,
Uint8 stream,
int  len 
)

Definition at line 80 of file loopwave.c.

References SDL_memcpy, and wave.

Referenced by iteration(), and main().

{
Uint8 *waveptr;
int waveleft;
/* Set up the pointers */
waveptr = wave.sound + wave.soundpos;
waveleft = wave.soundlen - wave.soundpos;
/* Go! */
while (waveleft <= len) {
SDL_memcpy(stream, waveptr, waveleft);
stream += waveleft;
len -= waveleft;
waveptr = wave.sound;
waveleft = wave.soundlen;
wave.soundpos = 0;
}
SDL_memcpy(stream, waveptr, len);
wave.soundpos += len;
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 114 of file loopwave.c.

References SDL_Event::adevice, close_audio(), device, done, sort_controllers::filename, fillerup(), i, SDL_AudioDeviceEvent::iscapture, loop(), NULL, open_audio(), quit(), reopen_audio(), SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED, SDL_Delay, SDL_FlushEvents, SDL_FreeWAV, SDL_GetAudioDriver, SDL_GetCurrentAudioDriver, SDL_GetError, SDL_GetNumAudioDrivers, SDL_Init, SDL_INIT_AUDIO, SDL_INIT_EVENTS, SDL_LoadWAV, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PollEvent, SDL_Quit, SDL_QUIT, SDL_strlcpy, SDL_Event::type, wave, and SDL_AudioDeviceEvent::which.

{
int i;
char filename[4096];
/* Enable standard application logging */
/* Load the SDL library */
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
if (argc > 1) {
SDL_strlcpy(filename, argv[1], sizeof(filename));
} else {
SDL_strlcpy(filename, "sample.wav", sizeof(filename));
}
/* 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);
}
wave.spec.callback = fillerup;
/* Show the list of available drivers */
SDL_Log("Available audio drivers:");
for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
}
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
while (SDL_PollEvent(&event) > 0) {
if (event.type == SDL_QUIT) {
done = 1;
}
if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
(event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
}
}
SDL_Delay(100);
}
#endif
/* Clean up on signal */
SDL_FreeWAV(wave.sound);
return (0);
}
static void open_audio ( )
static

Definition at line 57 of file loopwave.c.

References device, NULL, quit(), SDL_FALSE, SDL_FreeWAV, SDL_GetError, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_OpenAudioDevice, SDL_PauseAudioDevice, and wave.

Referenced by main(), and reopen_audio().

{
/* Initialize fillerup() variables */
if (!device) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
SDL_FreeWAV(wave.sound);
quit(2);
}
/* Let the audio run */
}
static void quit ( int  rc)
static

Definition at line 41 of file loopwave.c.

References SDL_Quit.

{
exit(rc);
}
static void reopen_audio ( )
static

Definition at line 72 of file loopwave.c.

References close_audio(), and open_audio().

Referenced by main().

Variable Documentation

int done = 0
static

Definition at line 102 of file loopwave.c.

Uint8* sound

Definition at line 32 of file loopwave.c.

Referenced by fillerup(), loop(), main(), and play_through_once().

Uint32 soundlen

Definition at line 33 of file loopwave.c.

Referenced by fillerup(), main(), and play_through_once().

int soundpos

Definition at line 34 of file loopwave.c.

Definition at line 31 of file loopwave.c.

Referenced by DUMMYAUDIO_CaptureFromDevice(), and main().

struct { ... } wave

Referenced by fillerup(), main(), and open_audio().