/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/loopwave.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/loopwave.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (show annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 2563 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
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 */
7 #include "SDL_config.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #if HAVE_SIGNAL_H
13 #include <signal.h>
14 #endif
15
16 #include "SDL.h"
17 #include "SDL_audio.h"
18
19 struct
20 {
21 SDL_AudioSpec spec;
22 Uint8 *sound; /* Pointer to wave data */
23 Uint32 soundlen; /* Length of wave data */
24 int soundpos; /* Current play position */
25 } wave;
26
27
28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
29 static void
30 quit(int rc)
31 {
32 SDL_Quit();
33 exit(rc);
34 }
35
36
37 void SDLCALL
38 fillerup(void *unused, Uint8 * stream, int len)
39 {
40 Uint8 *waveptr;
41 int waveleft;
42
43 /* Set up the pointers */
44 waveptr = wave.sound + wave.soundpos;
45 waveleft = wave.soundlen - wave.soundpos;
46
47 /* Go! */
48 while (waveleft <= len) {
49 SDL_memcpy(stream, waveptr, waveleft);
50 stream += waveleft;
51 len -= waveleft;
52 waveptr = wave.sound;
53 waveleft = wave.soundlen;
54 wave.soundpos = 0;
55 }
56 SDL_memcpy(stream, waveptr, len);
57 wave.soundpos += len;
58 }
59
60 static int done = 0;
61 void
62 poked(int sig)
63 {
64 done = 1;
65 }
66
67 int
68 main(int argc, char *argv[])
69 {
70 /* Load the SDL library */
71 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
72 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
73 return (1);
74 }
75
76 if (argv[1] == NULL) {
77 argv[1] = "sample.wav";
78 }
79 /* Load the wave file into memory */
80 if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
81 fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
82 quit(1);
83 }
84
85 wave.spec.callback = fillerup;
86 #if HAVE_SIGNAL_H
87 /* Set the signals */
88 #ifdef SIGHUP
89 signal(SIGHUP, poked);
90 #endif
91 signal(SIGINT, poked);
92 #ifdef SIGQUIT
93 signal(SIGQUIT, poked);
94 #endif
95 signal(SIGTERM, poked);
96 #endif /* HAVE_SIGNAL_H */
97
98 /* Initialize fillerup() variables */
99 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
100 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
101 SDL_FreeWAV(wave.sound);
102 quit(2);
103 }
104
105 printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
106
107 /* Let the audio run */
108 SDL_PauseAudio(0);
109 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
110 SDL_Delay(1000);
111
112 /* Clean up on signal */
113 SDL_CloseAudio();
114 SDL_FreeWAV(wave.sound);
115 SDL_Quit();
116 return (0);
117 }

  ViewVC Help
Powered by ViewVC 1.1.22