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

Go to the source code of this file.

Functions

static void quit (int rc)
 
void poked (int sig)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

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

Function Documentation

◆ loop()

void loop ( )

Definition at line 53 of file loopwavequeue.c.

References done, SDL_AUDIO_PLAYING, SDL_GetAudioStatus, SDL_GetError, SDL_GetQueuedAudioSize, SDL_Log, SDL_QueueAudio, and wave.

Referenced by main().

54 {
55 #ifdef __EMSCRIPTEN__
57  emscripten_cancel_main_loop();
58  }
59  else
60 #endif
61  {
62  /* The device from SDL_OpenAudio() is always device #1. */
63  const Uint32 queued = SDL_GetQueuedAudioSize(1);
64  SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
65  if (queued <= 8192) { /* time to requeue the whole thing? */
66  if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
67  SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
68  } else {
69  SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
70  }
71  }
72  }
73 }
#define SDL_GetError
#define SDL_GetAudioStatus
struct @39 wave
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
#define SDL_GetQueuedAudioSize
#define SDL_Log
#define SDL_QueueAudio
static int done
Definition: loopwavequeue.c:45

◆ main()

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

Definition at line 76 of file loopwavequeue.c.

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

77 {
78  char filename[4096];
79 
80  /* Enable standard application logging */
82 
83  /* Load the SDL library */
84  if (SDL_Init(SDL_INIT_AUDIO) < 0) {
85  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
86  return (1);
87  }
88 
89  if (argc > 1) {
90  SDL_strlcpy(filename, argv[1], sizeof(filename));
91  } else {
92  SDL_strlcpy(filename, "sample.wav", sizeof(filename));
93  }
94  /* Load the wave file into memory */
95  if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
96  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
97  quit(1);
98  }
99 
100  wave.spec.callback = NULL; /* we'll push audio. */
101 
102 #if HAVE_SIGNAL_H
103  /* Set the signals */
104 #ifdef SIGHUP
105  signal(SIGHUP, poked);
106 #endif
107  signal(SIGINT, poked);
108 #ifdef SIGQUIT
109  signal(SIGQUIT, poked);
110 #endif
111  signal(SIGTERM, poked);
112 #endif /* HAVE_SIGNAL_H */
113 
114  /* Initialize fillerup() variables */
115  if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
116  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
117  SDL_FreeWAV(wave.sound);
118  quit(2);
119  }
120 
121  /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
122 
123  /* Let the audio run */
124  SDL_PauseAudio(0);
125 
126  done = 0;
127 
128  /* Note that we stuff the entire audio buffer into the queue in one
129  shot. Most apps would want to feed it a little at a time, as it
130  plays, but we're going for simplicity here. */
131 
132 #ifdef __EMSCRIPTEN__
133  emscripten_set_main_loop(loop, 0, 1);
134 #else
135  while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
136  {
137  loop();
138 
139  SDL_Delay(100); /* let it play for awhile. */
140  }
141 #endif
142 
143  /* Clean up on signal */
144  SDL_CloseAudio();
145  SDL_FreeWAV(wave.sound);
146  SDL_Quit();
147  return 0;
148 }
#define SDL_strlcpy
#define SDL_GetError
#define SDL_GetAudioStatus
#define SDL_OpenAudio
struct @39 wave
#define SDL_FreeWAV
static void quit(int rc)
Definition: loopwavequeue.c:39
#define SDL_LogError
#define SDL_PauseAudio
void poked(int sig)
Definition: loopwavequeue.c:47
#define SDL_Quit
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:425
void loop()
Definition: loopwavequeue.c:53
#define SDL_Delay
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define SDL_INIT_AUDIO
Definition: SDL.h:76
#define SDL_CloseAudio
static int done
Definition: loopwavequeue.c:45
#define SDL_Init

◆ poked()

void poked ( int  sig)

Definition at line 47 of file loopwavequeue.c.

References done.

Referenced by main().

48 {
49  done = 1;
50 }
static int done
Definition: loopwavequeue.c:45

◆ quit()

static void quit ( int  rc)
static

Definition at line 39 of file loopwavequeue.c.

References SDL_Quit.

Referenced by main().

40 {
41  SDL_Quit();
42  exit(rc);
43 }
#define SDL_Quit

Variable Documentation

◆ done

int done = 0
static

Definition at line 45 of file loopwavequeue.c.

Referenced by loop(), main(), and poked().

◆ sound

Uint8* sound

Definition at line 31 of file loopwavequeue.c.

◆ soundlen

Uint32 soundlen

Definition at line 32 of file loopwavequeue.c.

◆ soundpos

int soundpos

Definition at line 33 of file loopwavequeue.c.

◆ spec

Definition at line 30 of file loopwavequeue.c.

◆ wave

struct { ... } wave

Referenced by loop(), and main().