SDL  2.0
loopwavequeue.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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 sound queueing */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #ifdef __EMSCRIPTEN__
19 #include <emscripten/emscripten.h>
20 #endif
21 
22 #include "SDL.h"
23 
24 #if HAVE_SIGNAL_H
25 #include <signal.h>
26 #endif
27 
28 static struct
29 {
31  Uint8 *sound; /* Pointer to wave data */
32  Uint32 soundlen; /* Length of wave data */
33 } wave;
34 
35 
36 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
37 static void
38 quit(int rc)
39 {
40  SDL_Quit();
41  exit(rc);
42 }
43 
44 static int done = 0;
45 void
46 poked(int sig)
47 {
48  done = 1;
49 }
50 
51 void
53 {
54 #ifdef __EMSCRIPTEN__
56  emscripten_cancel_main_loop();
57  }
58  else
59 #endif
60  {
61  /* The device from SDL_OpenAudio() is always device #1. */
62  const Uint32 queued = SDL_GetQueuedAudioSize(1);
63  SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
64  if (queued <= 8192) { /* time to requeue the whole thing? */
65  if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
66  SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
67  } else {
68  SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
69  }
70  }
71  }
72 }
73 
74 int
75 main(int argc, char *argv[])
76 {
77  char filename[4096];
78 
79  /* Enable standard application logging */
81 
82  /* Load the SDL library */
83  if (SDL_Init(SDL_INIT_AUDIO) < 0) {
84  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
85  return (1);
86  }
87 
88  if (argc > 1) {
89  SDL_strlcpy(filename, argv[1], sizeof(filename));
90  } else {
91  SDL_strlcpy(filename, "sample.wav", sizeof(filename));
92  }
93  /* Load the wave file into memory */
94  if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
95  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
96  quit(1);
97  }
98 
99  wave.spec.callback = NULL; /* we'll push audio. */
100 
101 #if HAVE_SIGNAL_H
102  /* Set the signals */
103 #ifdef SIGHUP
104  signal(SIGHUP, poked);
105 #endif
106  signal(SIGINT, poked);
107 #ifdef SIGQUIT
108  signal(SIGQUIT, poked);
109 #endif
110  signal(SIGTERM, poked);
111 #endif /* HAVE_SIGNAL_H */
112 
113  /* Initialize fillerup() variables */
114  if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
115  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
116  SDL_FreeWAV(wave.sound);
117  quit(2);
118  }
119 
120  /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
121 
122  /* Let the audio run */
123  SDL_PauseAudio(0);
124 
125  done = 0;
126 
127  /* Note that we stuff the entire audio buffer into the queue in one
128  shot. Most apps would want to feed it a little at a time, as it
129  plays, but we're going for simplicity here. */
130 
131 #ifdef __EMSCRIPTEN__
132  emscripten_set_main_loop(loop, 0, 1);
133 #else
134  while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
135  {
136  loop();
137 
138  SDL_Delay(100); /* let it play for awhile. */
139  }
140 #endif
141 
142  /* Clean up on signal */
143  SDL_CloseAudio();
144  SDL_FreeWAV(wave.sound);
145  SDL_Quit();
146  return 0;
147 }
148 
149 /* vi: set ts=4 sw=4 expandtab: */
SDL.h
SDL_OpenAudio
#define SDL_OpenAudio
Definition: SDL_dynapi_overrides.h:78
SDL_GetAudioStatus
#define SDL_GetAudioStatus
Definition: SDL_dynapi_overrides.h:82
sort_controllers.filename
string filename
Definition: sort_controllers.py:8
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
NULL
#define NULL
Definition: begin_code.h:167
main
int main(int argc, char *argv[])
Definition: loopwavequeue.c:75
soundlen
Uint32 soundlen
Definition: loopwavequeue.c:32
SDL_AudioSpec
Definition: SDL_audio.h:178
SDL_FreeWAV
#define SDL_FreeWAV
Definition: SDL_dynapi_overrides.h:87
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
sound
Uint8 * sound
Definition: loopwavequeue.c:31
SDL_PauseAudio
#define SDL_PauseAudio
Definition: SDL_dynapi_overrides.h:84
SDL_GetQueuedAudioSize
#define SDL_GetQueuedAudioSize
Definition: SDL_dynapi_overrides.h:589
quit
static void quit(int rc)
Definition: loopwavequeue.c:38
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_QueueAudio
#define SDL_QueueAudio
Definition: SDL_dynapi_overrides.h:588
SDL_LoadWAV
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)
Definition: SDL_audio.h:484
poked
void poked(int sig)
Definition: loopwavequeue.c:46
loop
void loop()
Definition: loopwavequeue.c:52
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
spec
SDL_AudioSpec spec
Definition: loopwavequeue.c:30
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
done
static int done
Definition: loopwavequeue.c:44
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
wave
static struct @285 wave
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_AUDIO_PLAYING
@ SDL_AUDIO_PLAYING
Definition: SDL_audio.h:398
SDL_CloseAudio
#define SDL_CloseAudio
Definition: SDL_dynapi_overrides.h:96
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_INIT_AUDIO
#define SDL_INIT_AUDIO
Definition: SDL.h:78
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179