Mercurial > sdl-ios-xcode
annotate test/loopwave.c @ 3824:25052dd25810 SDL-ryan-multiple-audio-device
Minor ALSA dynamic loading fix.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Sat, 07 Oct 2006 07:23:52 +0000 |
parents | e2f68b579a01 |
children | 5c14978b1e97 |
rev | line source |
---|---|
0 | 1 |
2 /* Program to load a wave file and loop playing it using SDL sound */ | |
3 | |
4 /* loopwaves.c is much more robust in handling WAVE files -- | |
5 This is only for simple WAVEs | |
6 */ | |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
7 #include "SDL_config.h" |
0 | 8 |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
11 |
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
12 #if HAVE_SIGNAL_H |
0 | 13 #include <signal.h> |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
14 #endif |
0 | 15 |
16 #include "SDL.h" | |
17 #include "SDL_audio.h" | |
18 | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
19 struct |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
20 { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
21 SDL_AudioSpec spec; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
22 Uint8 *sound; /* Pointer to wave data */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
23 Uint32 soundlen; /* Length of wave data */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
24 int soundpos; /* Current play position */ |
0 | 25 } wave; |
26 | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
27 |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
29 static void |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
30 quit(int rc) |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
31 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
32 SDL_Quit(); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
33 exit(rc); |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
34 } |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
35 |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
36 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
37 void SDLCALL |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
38 fillerup(void *unused, Uint8 * stream, int len) |
0 | 39 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
40 Uint8 *waveptr; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
41 int waveleft; |
0 | 42 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
43 /* Set up the pointers */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
44 waveptr = wave.sound + wave.soundpos; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
45 waveleft = wave.soundlen - wave.soundpos; |
0 | 46 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
47 /* Go! */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
48 while (waveleft <= len) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
49 SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
50 stream += waveleft; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
51 len -= waveleft; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
52 waveptr = wave.sound; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
53 waveleft = wave.soundlen; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
54 wave.soundpos = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
55 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
56 SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
57 wave.soundpos += len; |
0 | 58 } |
59 | |
60 static int done = 0; | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
61 void |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
62 poked(int sig) |
0 | 63 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
64 done = 1; |
0 | 65 } |
66 | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
67 int |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
68 main(int argc, char *argv[]) |
0 | 69 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
70 int i, n; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
71 |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
72 /* Load the SDL library */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
73 if (SDL_Init(SDL_INIT_AUDIO) < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
74 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
75 return (1); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
76 } |
3789
e2f68b579a01
Added testaudioinfo.c ...
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
77 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
78 if (argv[1] == NULL) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
79 argv[1] = "sample.wav"; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
80 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
81 /* Load the wave file into memory */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
82 if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
83 fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
84 quit(1); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
85 } |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
86 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
87 wave.spec.callback = fillerup; |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
88 #if HAVE_SIGNAL_H |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
89 /* Set the signals */ |
0 | 90 #ifdef SIGHUP |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
91 signal(SIGHUP, poked); |
0 | 92 #endif |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
93 signal(SIGINT, poked); |
0 | 94 #ifdef SIGQUIT |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
95 signal(SIGQUIT, poked); |
0 | 96 #endif |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
97 signal(SIGTERM, poked); |
1463
0394f8ebc42d
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
1151
diff
changeset
|
98 #endif /* HAVE_SIGNAL_H */ |
0 | 99 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
100 /* Initialize fillerup() variables */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
101 if (SDL_OpenAudio(&wave.spec, NULL) < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
102 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
103 SDL_FreeWAV(wave.sound); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
104 quit(2); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
105 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
106 SDL_PauseAudio(0); |
0 | 107 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
108 /* Let the audio run */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
109 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING)) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
110 SDL_Delay(1000); |
0 | 111 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
112 /* Clean up on signal */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
113 SDL_CloseAudio(); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
114 SDL_FreeWAV(wave.sound); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
115 SDL_Quit(); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
116 return (0); |
0 | 117 } |