comparison test/loopwave.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 8b9d79e7eacf
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
14 #endif 14 #endif
15 15
16 #include "SDL.h" 16 #include "SDL.h"
17 #include "SDL_audio.h" 17 #include "SDL_audio.h"
18 18
19 struct { 19 struct
20 SDL_AudioSpec spec; 20 {
21 Uint8 *sound; /* Pointer to wave data */ 21 SDL_AudioSpec spec;
22 Uint32 soundlen; /* Length of wave data */ 22 Uint8 *sound; /* Pointer to wave data */
23 int soundpos; /* Current play position */ 23 Uint32 soundlen; /* Length of wave data */
24 int soundpos; /* Current play position */
24 } wave; 25 } wave;
25 26
26 27
27 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 28 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
28 static void quit(int rc) 29 static void
30 quit (int rc)
29 { 31 {
30 SDL_Quit(); 32 SDL_Quit ();
31 exit(rc); 33 exit (rc);
32 } 34 }
33 35
34 36
35 void SDLCALL fillerup(void *unused, Uint8 *stream, int len) 37 void SDLCALL
38 fillerup (void *unused, Uint8 * stream, int len)
36 { 39 {
37 Uint8 *waveptr; 40 Uint8 *waveptr;
38 int waveleft; 41 int waveleft;
39 42
40 /* Set up the pointers */ 43 /* Set up the pointers */
41 waveptr = wave.sound + wave.soundpos; 44 waveptr = wave.sound + wave.soundpos;
42 waveleft = wave.soundlen - wave.soundpos; 45 waveleft = wave.soundlen - wave.soundpos;
43 46
44 /* Go! */ 47 /* Go! */
45 while ( waveleft <= len ) { 48 while (waveleft <= len) {
46 SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME); 49 SDL_MixAudio (stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
47 stream += waveleft; 50 stream += waveleft;
48 len -= waveleft; 51 len -= waveleft;
49 waveptr = wave.sound; 52 waveptr = wave.sound;
50 waveleft = wave.soundlen; 53 waveleft = wave.soundlen;
51 wave.soundpos = 0; 54 wave.soundpos = 0;
52 } 55 }
53 SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME); 56 SDL_MixAudio (stream, waveptr, len, SDL_MIX_MAXVOLUME);
54 wave.soundpos += len; 57 wave.soundpos += len;
55 } 58 }
56 59
57 static int done = 0; 60 static int done = 0;
58 void poked(int sig) 61 void
62 poked (int sig)
59 { 63 {
60 done = 1; 64 done = 1;
61 } 65 }
62 66
63 int main(int argc, char *argv[]) 67 int
68 main (int argc, char *argv[])
64 { 69 {
65 int i, n; 70 int i, n;
66 71
67 /* Print available audio drivers */ 72 /* Print available audio drivers */
68 n = SDL_GetNumAudioDrivers(); 73 n = SDL_GetNumAudioDrivers ();
69 if ( n == 0 ) { 74 if (n == 0) {
70 printf("No built-in audio drivers\n"); 75 printf ("No built-in audio drivers\n");
71 } else { 76 } else {
72 printf("Built-in audio drivers:"); 77 printf ("Built-in audio drivers:");
73 for ( i = 0; i < n; ++i ) { 78 for (i = 0; i < n; ++i) {
74 if ( i > 0 ) { 79 if (i > 0) {
75 printf(","); 80 printf (",");
76 } 81 }
77 printf(" %s", SDL_GetAudioDriver(i)); 82 printf (" %s", SDL_GetAudioDriver (i));
78 } 83 }
79 printf("\n"); 84 printf ("\n");
80 } 85 }
81 86
82 /* Load the SDL library */ 87 /* Load the SDL library */
83 if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { 88 if (SDL_Init (SDL_INIT_AUDIO) < 0) {
84 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 89 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
85 return(1); 90 return (1);
86 } 91 }
87 if ( argv[1] == NULL ) { 92 if (argv[1] == NULL) {
88 argv[1] = "sample.wav"; 93 argv[1] = "sample.wav";
89 } 94 }
90 /* Load the wave file into memory */ 95 /* Load the wave file into memory */
91 if ( SDL_LoadWAV(argv[1], 96 if (SDL_LoadWAV (argv[1],
92 &wave.spec, &wave.sound, &wave.soundlen) == NULL ) { 97 &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
93 fprintf(stderr, "Couldn't load %s: %s\n", 98 fprintf (stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError ());
94 argv[1], SDL_GetError()); 99 quit (1);
95 quit(1); 100 }
96 }
97 101
98 wave.spec.callback = fillerup; 102 wave.spec.callback = fillerup;
99 #if HAVE_SIGNAL_H 103 #if HAVE_SIGNAL_H
100 /* Set the signals */ 104 /* Set the signals */
101 #ifdef SIGHUP 105 #ifdef SIGHUP
102 signal(SIGHUP, poked); 106 signal (SIGHUP, poked);
103 #endif 107 #endif
104 signal(SIGINT, poked); 108 signal (SIGINT, poked);
105 #ifdef SIGQUIT 109 #ifdef SIGQUIT
106 signal(SIGQUIT, poked); 110 signal (SIGQUIT, poked);
107 #endif 111 #endif
108 signal(SIGTERM, poked); 112 signal (SIGTERM, poked);
109 #endif /* HAVE_SIGNAL_H */ 113 #endif /* HAVE_SIGNAL_H */
110 114
111 /* Initialize fillerup() variables */ 115 /* Initialize fillerup() variables */
112 if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) { 116 if (SDL_OpenAudio (&wave.spec, NULL) < 0) {
113 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); 117 fprintf (stderr, "Couldn't open audio: %s\n", SDL_GetError ());
114 SDL_FreeWAV(wave.sound); 118 SDL_FreeWAV (wave.sound);
115 quit(2); 119 quit (2);
116 } 120 }
117 SDL_PauseAudio(0); 121 SDL_PauseAudio (0);
118 122
119 /* Let the audio run */ 123 /* Let the audio run */
120 printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); 124 printf ("Using audio driver: %s\n", SDL_GetCurrentAudioDriver ());
121 while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) ) 125 while (!done && (SDL_GetAudioStatus () == SDL_AUDIO_PLAYING))
122 SDL_Delay(1000); 126 SDL_Delay (1000);
123 127
124 /* Clean up on signal */ 128 /* Clean up on signal */
125 SDL_CloseAudio(); 129 SDL_CloseAudio ();
126 SDL_FreeWAV(wave.sound); 130 SDL_FreeWAV (wave.sound);
127 SDL_Quit(); 131 SDL_Quit ();
128 return(0); 132 return (0);
129 } 133 }