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().

49 {
50  if (device != 0) {
52  device = 0;
53  }
54 }
#define SDL_CloseAudioDevice
static SDL_AudioDeviceID device
Definition: loopwave.c:37
void fillerup ( void unused,
Uint8 stream,
int  len 
)

Definition at line 80 of file loopwave.c.

References SDL_memcpy, and wave.

Referenced by main().

81 {
82  Uint8 *waveptr;
83  int waveleft;
84 
85  /* Set up the pointers */
86  waveptr = wave.sound + wave.soundpos;
87  waveleft = wave.soundlen - wave.soundpos;
88 
89  /* Go! */
90  while (waveleft <= len) {
91  SDL_memcpy(stream, waveptr, waveleft);
92  stream += waveleft;
93  len -= waveleft;
94  waveptr = wave.sound;
95  waveleft = wave.soundlen;
96  wave.soundpos = 0;
97  }
98  SDL_memcpy(stream, waveptr, len);
99  wave.soundpos += len;
100 }
GLenum GLsizei len
static struct @45 wave
#define SDL_memcpy
GLuint GLuint stream
uint8_t Uint8
Definition: SDL_stdinc.h:157
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.

115 {
116  int i;
117  char filename[4096];
118 
119  /* Enable standard application logging */
121 
122  /* Load the SDL library */
124  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
125  return (1);
126  }
127 
128  if (argc > 1) {
129  SDL_strlcpy(filename, argv[1], sizeof(filename));
130  } else {
131  SDL_strlcpy(filename, "sample.wav", sizeof(filename));
132  }
133  /* Load the wave file into memory */
134  if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
135  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
136  quit(1);
137  }
138 
139  wave.spec.callback = fillerup;
140 
141  /* Show the list of available drivers */
142  SDL_Log("Available audio drivers:");
143  for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
144  SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
145  }
146 
147  SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
148 
149  open_audio();
150 
152 
153 #ifdef __EMSCRIPTEN__
154  emscripten_set_main_loop(loop, 0, 1);
155 #else
156  while (!done) {
158 
159  while (SDL_PollEvent(&event) > 0) {
160  if (event.type == SDL_QUIT) {
161  done = 1;
162  }
163  if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
164  (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
165  reopen_audio();
166  }
167  }
168  SDL_Delay(100);
169  }
170 #endif
171 
172  /* Clean up on signal */
173  close_audio();
174  SDL_FreeWAV(wave.sound);
175  SDL_Quit();
176  return (0);
177 }
#define SDL_GetNumAudioDrivers
#define SDL_strlcpy
void loop()
Definition: checkkeys.c:152
#define SDL_INIT_EVENTS
Definition: SDL.h:82
#define SDL_PollEvent
#define SDL_GetError
#define SDL_FreeWAV
static struct @45 wave
#define SDL_LogError
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_Log
void fillerup(void *unused, Uint8 *stream, int len)
Definition: loopwave.c:80
struct _cl_event * event
static void quit(int rc)
Definition: loopwave.c:41
#define SDL_FlushEvents
#define SDL_Quit
static void reopen_audio()
Definition: loopwave.c:72
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:441
#define SDL_GetAudioDriver
static int done
Definition: loopwave.c:102
#define SDL_Delay
#define SDL_LogSetPriority
#define SDL_GetCurrentAudioDriver
#define NULL
Definition: begin_code.h:164
SDL_AudioDeviceEvent adevice
Definition: SDL_events.h:544
#define SDL_INIT_AUDIO
Definition: SDL.h:77
static void close_audio()
Definition: loopwave.c:48
#define SDL_Init
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527
static void open_audio()
Definition: loopwave.c:57
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().

58 {
59  /* Initialize fillerup() variables */
61  if (!device) {
62  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
63  SDL_FreeWAV(wave.sound);
64  quit(2);
65  }
66 
67 
68  /* Let the audio run */
70 }
#define SDL_GetError
#define SDL_OpenAudioDevice
#define SDL_FreeWAV
static struct @45 wave
#define SDL_LogError
static SDL_AudioDeviceID device
Definition: loopwave.c:37
static void quit(int rc)
Definition: loopwave.c:41
#define NULL
Definition: begin_code.h:164
#define SDL_PauseAudioDevice
static void quit ( int  rc)
static

Definition at line 41 of file loopwave.c.

References SDL_Quit.

Referenced by main(), and open_audio().

42 {
43  SDL_Quit();
44  exit(rc);
45 }
#define SDL_Quit
static void reopen_audio ( )
static

Definition at line 72 of file loopwave.c.

References close_audio(), and open_audio().

Referenced by main().

73 {
74  close_audio();
75  open_audio();
76 }
static void close_audio()
Definition: loopwave.c:48
static void open_audio()
Definition: loopwave.c:57

Variable Documentation

int done = 0
static

Definition at line 102 of file loopwave.c.

Referenced by main().

Uint8* sound

Definition at line 32 of file loopwave.c.

Uint32 soundlen

Definition at line 33 of file loopwave.c.

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().