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)
 
void fillerup (void *unused, Uint8 *stream, int len)
 
void poked (int sig)
 
int main (int argc, char *argv[])
 

Variables

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

Function Documentation

◆ fillerup()

void fillerup ( void unused,
Uint8 stream,
int  len 
)

Definition at line 52 of file loopwave.c.

References SDL_memcpy, and wave.

Referenced by main().

53 {
54  Uint8 *waveptr;
55  int waveleft;
56 
57  /* Set up the pointers */
58  waveptr = wave.sound + wave.soundpos;
59  waveleft = wave.soundlen - wave.soundpos;
60 
61  /* Go! */
62  while (waveleft <= len) {
63  SDL_memcpy(stream, waveptr, waveleft);
64  stream += waveleft;
65  len -= waveleft;
66  waveptr = wave.sound;
67  waveleft = wave.soundlen;
68  wave.soundpos = 0;
69  }
70  SDL_memcpy(stream, waveptr, len);
71  wave.soundpos += len;
72 }
GLuint GLuint stream
struct @38 wave
GLenum GLsizei len
#define SDL_memcpy
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 91 of file loopwave.c.

References done, sort_controllers::filename, fillerup(), i, loop(), NULL, poked(), quit(), SDL_AUDIO_PLAYING, SDL_CloseAudio, SDL_Delay, SDL_FreeWAV, SDL_GetAudioDriver, SDL_GetAudioStatus, SDL_GetCurrentAudioDriver, SDL_GetError, SDL_GetNumAudioDrivers, SDL_Init, SDL_INIT_AUDIO, SDL_LoadWAV, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_OpenAudio, SDL_PauseAudio, SDL_Quit, SDL_strlcpy, and wave.

92 {
93  int i;
94  char filename[4096];
95 
96  /* Enable standard application logging */
98 
99  /* Load the SDL library */
100  if (SDL_Init(SDL_INIT_AUDIO) < 0) {
101  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
102  return (1);
103  }
104 
105  if (argc > 1) {
106  SDL_strlcpy(filename, argv[1], sizeof(filename));
107  } else {
108  SDL_strlcpy(filename, "sample.wav", sizeof(filename));
109  }
110  /* Load the wave file into memory */
111  if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
112  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
113  quit(1);
114  }
115 
116  wave.spec.callback = fillerup;
117 #if HAVE_SIGNAL_H
118  /* Set the signals */
119 #ifdef SIGHUP
120  signal(SIGHUP, poked);
121 #endif
122  signal(SIGINT, poked);
123 #ifdef SIGQUIT
124  signal(SIGQUIT, poked);
125 #endif
126  signal(SIGTERM, poked);
127 #endif /* HAVE_SIGNAL_H */
128 
129  /* Show the list of available drivers */
130  SDL_Log("Available audio drivers:");
131  for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
132  SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
133  }
134 
135  /* Initialize fillerup() variables */
136  if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
137  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
138  SDL_FreeWAV(wave.sound);
139  quit(2);
140  }
141 
142  SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
143 
144  /* Let the audio run */
145  SDL_PauseAudio(0);
146 
147 #ifdef __EMSCRIPTEN__
148  emscripten_set_main_loop(loop, 0, 1);
149 #else
150  while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
151  SDL_Delay(1000);
152 #endif
153 
154  /* Clean up on signal */
155  SDL_CloseAudio();
156  SDL_FreeWAV(wave.sound);
157  SDL_Quit();
158  return (0);
159 }
#define SDL_GetNumAudioDrivers
#define SDL_strlcpy
void loop()
Definition: checkkeys.c:152
#define SDL_GetError
#define SDL_GetAudioStatus
void poked(int sig)
Definition: loopwave.c:76
#define SDL_OpenAudio
struct @38 wave
#define SDL_FreeWAV
#define SDL_LogError
#define SDL_Log
#define SDL_PauseAudio
void fillerup(void *unused, Uint8 *stream, int len)
Definition: loopwave.c:52
static void quit(int rc)
Definition: loopwave.c:44
#define SDL_Quit
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:425
#define SDL_GetAudioDriver
static int done
Definition: loopwave.c:74
#define SDL_Delay
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
#define SDL_LogSetPriority
#define SDL_GetCurrentAudioDriver
#define NULL
Definition: begin_code.h:143
#define SDL_INIT_AUDIO
Definition: SDL.h:76
#define SDL_CloseAudio
#define SDL_Init

◆ poked()

void poked ( int  sig)

Definition at line 76 of file loopwave.c.

References done, loop(), SDL_AUDIO_PLAYING, and SDL_GetAudioStatus.

Referenced by main().

77 {
78  done = 1;
79 }
static int done
Definition: loopwave.c:74

◆ quit()

static void quit ( int  rc)
static

Definition at line 44 of file loopwave.c.

References SDL_Quit, and SDLCALL.

Referenced by main().

45 {
46  SDL_Quit();
47  exit(rc);
48 }
#define SDL_Quit

Variable Documentation

◆ done

int done = 0
static

Definition at line 74 of file loopwave.c.

Referenced by main(), and poked().

◆ sound

Uint8* sound

Definition at line 36 of file loopwave.c.

◆ soundlen

Uint32 soundlen

Definition at line 37 of file loopwave.c.

◆ soundpos

int soundpos

Definition at line 38 of file loopwave.c.

◆ spec

Definition at line 35 of file loopwave.c.

Referenced by DUMMYAUDIO_CaptureFromDevice(), and main().

◆ wave

struct { ... } wave

Referenced by fillerup(), and main().