SDL  2.0
loopwave.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2018 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 audio */
14 
15 /* loopwaves.c is much more robust in handling WAVE files --
16  This is only for simple WAVEs
17 */
18 #include "SDL_config.h"
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #ifdef __EMSCRIPTEN__
24 #include <emscripten/emscripten.h>
25 #endif
26 
27 #include "SDL.h"
28 
29 static struct
30 {
32  Uint8 *sound; /* Pointer to wave data */
33  Uint32 soundlen; /* Length of wave data */
34  int soundpos; /* Current play position */
35 } wave;
36 
38 
39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
40 static void
41 quit(int rc)
42 {
43  SDL_Quit();
44  exit(rc);
45 }
46 
47 static void
49 {
50  if (device != 0) {
52  device = 0;
53  }
54 }
55 
56 static void
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 }
71 
72 static void reopen_audio()
73 {
74  close_audio();
75  open_audio();
76 }
77 
78 
79 void SDLCALL
80 fillerup(void *unused, Uint8 * stream, int len)
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 }
101 
102 static int done = 0;
103 
104 #ifdef __EMSCRIPTEN__
105 void
106 loop()
107 {
109  emscripten_cancel_main_loop();
110 }
111 #endif
112 
113 int
114 main(int argc, char *argv[])
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 }
178 
179 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_GetNumAudioDrivers
#define SDL_strlcpy
void loop()
Definition: checkkeys.c:152
#define SDL_INIT_EVENTS
Definition: SDL.h:83
#define SDL_PollEvent
#define SDL_GetError
#define SDL_CloseAudioDevice
#define SDL_OpenAudioDevice
Uint32 soundlen
Definition: loopwave.c:33
int main(int argc, char *argv[])
Definition: loopwave.c:114
Uint8 * sound
Definition: loopwave.c:32
#define SDL_FreeWAV
GLenum GLsizei len
#define SDL_LogError
SDL_AudioSpec spec
Definition: loopwave.c:31
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_Log
#define SDL_memcpy
int soundpos
Definition: loopwave.c:34
GLuint GLuint stream
void fillerup(void *unused, Uint8 *stream, int len)
Definition: loopwave.c:80
uint8_t Uint8
Definition: SDL_stdinc.h:179
struct _cl_event * event
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF base if bpp PF set rept prefetch_distance PF set OFFSET endr endif endm macro preload_leading_step2 base if bpp ifc DST PF PF else if bpp lsl PF PF lsl PF PF lsl PF PF PF else PF lsl PF lsl PF lsl PF endif SIZE macro preload_middle scratch_holds_offset if bpp if else PF PF endif endif endif endm macro preload_trailing base if bpp if bpp *pix_per_block PF PF lsl PF PF PF PF PF else PF lsl PF lsl PF PF PF PF PF base if bpp if narrow_case &&bpp<=dst_w_bpp) PF bic, WK0, base, #31 PF pld, [WK0] PF add, WK1, base, X, LSL #bpp_shift PF sub, WK1, WK1, #1 PF bic, WK1, WK1, #31 PF cmp, WK1, WK0 PF beq, 90f PF pld, [WK1]90:.else PF bic, WK0, base, #31 PF pld, [WK0] PF add, WK1, base, X, lsl #bpp_shift PF sub, WK1, WK1, #1 PF bic, WK1, WK1, #31 PF cmp, WK1, WK0 PF beq, 92f91:PF add, WK0, WK0, #32 PF cmp, WK0, WK1 PF pld, [WK0] PF bne, 91b92:.endif .endif.endm.macro conditional_process1_helper cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx process_head cond, numbytes, firstreg, unaligned_src, unaligned_mask, 0 .if decrementx sub &cond X, X, #8 *numbytes/dst_w_bpp .endif process_tail cond, numbytes, firstreg .if !((flags) &FLAG_PROCESS_DOES_STORE) pixst cond, numbytes, firstreg, DST .endif.endm.macro conditional_process1 cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx .if(flags) &FLAG_BRANCH_OVER .ifc cond, mi bpl 100f .endif .ifc cond, cs bcc 100f .endif .ifc cond, ne beq 100f .endif conditional_process1_helper, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx100:.else conditional_process1_helper cond, process_head, process_tail, numbytes, firstreg, unaligned_src, unaligned_mask, decrementx .endif.endm.macro conditional_process2 test, cond1, cond2, process_head, process_tail, numbytes1, numbytes2, firstreg1, firstreg2, unaligned_src, unaligned_mask, decrementx .if(flags) &(FLAG_DST_READWRITE|FLAG_BRANCH_OVER|FLAG_PROCESS_CORRUPTS_PSR|FLAG_PROCESS_DOES_STORE) test conditional_process1 cond1, process_head, process_tail, numbytes1, firstreg1, unaligned_src, unaligned_mask, decrementx .if(flags) &FLAG_PROCESS_CORRUPTS_PSR test .endif conditional_process1 cond2, process_head, process_tail, numbytes2, firstreg2, unaligned_src, unaligned_mask, decrementx .else test process_head cond1, numbytes1, firstreg1, unaligned_src, unaligned_mask, 0 process_head cond2, numbytes2, firstreg2, unaligned_src, unaligned_mask, 0 .if decrementx sub &cond1 X, X, #8 *numbytes1/dst_w_bpp sub &cond2 X, X, #8 *numbytes2/dst_w_bpp .endif process_tail cond1, numbytes1, firstreg1 process_tail cond2, numbytes2, firstreg2 pixst cond1, numbytes1, firstreg1, DST pixst cond2, numbytes2, firstreg2, DST .endif.endm.macro test_bits_1_0_ptr .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 movs SCRATCH, X, lsl #32-1 .else movs SCRATCH, WK0, lsl #32-1 .endif.endm.macro test_bits_3_2_ptr .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 movs SCRATCH, X, lsl #32-3 .else movs SCRATCH, WK0, lsl #32-3 .endif.endm.macro leading_15bytes process_head, process_tail .set DECREMENT_X, 1 .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 .set DECREMENT_X, 0 sub X, X, WK0, lsr #dst_bpp_shift str X, [sp, #LINE_SAVED_REG_COUNT *4] mov X, WK0 .endif .if dst_w_bpp==8 conditional_process2 test_bits_1_0_ptr, mi, cs, process_head, process_tail, 1, 2, 1, 2, 1, 1, DECREMENT_X .elseif dst_w_bpp==16 test_bits_1_0_ptr conditional_process1 cs, process_head, process_tail, 2, 2, 1, 1, DECREMENT_X .endif conditional_process2 test_bits_3_2_ptr, mi, cs, process_head, process_tail, 4, 8, 1, 2, 1, 1, DECREMENT_X .if(flags) &FLAG_PROCESS_CORRUPTS_WK0 ldr X, [sp, #LINE_SAVED_REG_COUNT *4] .endif.endm.macro test_bits_3_2_pix movs SCRATCH, X, lsl #dst_bpp_shift+32-3.endm.macro test_bits_1_0_pix .if dst_w_bpp==8 movs SCRATCH, X, lsl #dst_bpp_shift+32-1 .else movs SCRATCH, X, lsr #1 .endif.endm.macro trailing_15bytes process_head, process_tail, unaligned_src, unaligned_mask conditional_process2 test_bits_3_2_pix, cs, mi, process_head, process_tail, 8, 4, 0, 2, unaligned_src, unaligned_mask, 0 .if dst_w_bpp==16 test_bits_1_0_pix conditional_process1 cs, process_head, process_tail, 2, 0, unaligned_src, unaligned_mask, 0 .elseif dst_w_bpp==8 conditional_process2 test_bits_1_0_pix, cs, mi, process_head, process_tail, 2, 1, 0, 1, unaligned_src, unaligned_mask, 0 .endif.endm.macro wide_case_inner_loop process_head, process_tail, unaligned_src, unaligned_mask, dst_alignment110:.set SUBBLOCK, 0 .rept pix_per_block *dst_w_bpp/128 process_head, 16, 0, unaligned_src, unaligned_mask, 1 .if(src_bpp > 0) &&(mask_bpp==0) &&((flags) &FLAG_PROCESS_PRESERVES_SCRATCH) preload_middle src_bpp, SRC, 1 .elseif(src_bpp==0) &&(mask_bpp > 0) &&((flags) &FLAG_PROCESS_PRESERVES_SCRATCH) preload_middle mask_bpp, MASK, 1 .else preload_middle src_bpp, SRC, 0 preload_middle mask_bpp, MASK, 0 .endif .if(dst_r_bpp > 0) &&((SUBBLOCK % 2)==0) &&(((flags) &FLAG_NO_PRELOAD_DST)==0) PF pld, [DST, #32 *prefetch_distance - dst_alignment] .endif process_tail, 16, 0 .if !((flags) &FLAG_PROCESS_DOES_STORE) pixst, 16, 0, DST .endif .set SUBBLOCK, SUBBLOCK+1 .endr subs X, X, #pix_per_block bhs 110b.endm.macro wide_case_inner_loop_and_trailing_pixels process_head, process_tail, process_inner_loop, exit_label, unaligned_src, unaligned_mask .if dst_r_bpp > tst bne process_inner_loop DST_PRELOAD_BIAS endif preload_trailing SRC preload_trailing MASK DST endif add medium_case_inner_loop_and_trailing_pixels unaligned_mask endm macro medium_case_inner_loop_and_trailing_pixels unused
static struct @55 wave
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:451
#define SDL_GetAudioDeviceStatus
#define SDL_GetAudioDriver
static int done
Definition: loopwave.c:102
#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:164
SDL_AudioDeviceEvent adevice
Definition: SDL_events.h:577
#define SDL_INIT_AUDIO
Definition: SDL.h:78
static void close_audio()
Definition: loopwave.c:48
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:330
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
#define SDLCALL
Definition: SDL_internal.h:45
Uint32 type
Definition: SDL_events.h:559
static void open_audio()
Definition: loopwave.c:57
#define SDL_PauseAudioDevice