1.1 --- a/test/loopwave.c Thu Jul 06 18:01:37 2006 +0000
1.2 +++ b/test/loopwave.c Mon Jul 10 21:04:37 2006 +0000
1.3 @@ -16,99 +16,117 @@
1.4 #include "SDL.h"
1.5 #include "SDL_audio.h"
1.6
1.7 -struct {
1.8 - SDL_AudioSpec spec;
1.9 - Uint8 *sound; /* Pointer to wave data */
1.10 - Uint32 soundlen; /* Length of wave data */
1.11 - int soundpos; /* Current play position */
1.12 +struct
1.13 +{
1.14 + SDL_AudioSpec spec;
1.15 + Uint8 *sound; /* Pointer to wave data */
1.16 + Uint32 soundlen; /* Length of wave data */
1.17 + int soundpos; /* Current play position */
1.18 } wave;
1.19
1.20
1.21 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
1.22 -static void quit(int rc)
1.23 +static void
1.24 +quit(int rc)
1.25 {
1.26 - SDL_Quit();
1.27 - exit(rc);
1.28 + SDL_Quit();
1.29 + exit(rc);
1.30 }
1.31
1.32
1.33 -void SDLCALL fillerup(void *unused, Uint8 *stream, int len)
1.34 +void SDLCALL
1.35 +fillerup(void *unused, Uint8 * stream, int len)
1.36 {
1.37 - Uint8 *waveptr;
1.38 - int waveleft;
1.39 + Uint8 *waveptr;
1.40 + int waveleft;
1.41
1.42 - /* Set up the pointers */
1.43 - waveptr = wave.sound + wave.soundpos;
1.44 - waveleft = wave.soundlen - wave.soundpos;
1.45 + /* Set up the pointers */
1.46 + waveptr = wave.sound + wave.soundpos;
1.47 + waveleft = wave.soundlen - wave.soundpos;
1.48
1.49 - /* Go! */
1.50 - while ( waveleft <= len ) {
1.51 - SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
1.52 - stream += waveleft;
1.53 - len -= waveleft;
1.54 - waveptr = wave.sound;
1.55 - waveleft = wave.soundlen;
1.56 - wave.soundpos = 0;
1.57 - }
1.58 - SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
1.59 - wave.soundpos += len;
1.60 + /* Go! */
1.61 + while (waveleft <= len) {
1.62 + SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
1.63 + stream += waveleft;
1.64 + len -= waveleft;
1.65 + waveptr = wave.sound;
1.66 + waveleft = wave.soundlen;
1.67 + wave.soundpos = 0;
1.68 + }
1.69 + SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
1.70 + wave.soundpos += len;
1.71 }
1.72
1.73 static int done = 0;
1.74 -void poked(int sig)
1.75 +void
1.76 +poked(int sig)
1.77 {
1.78 - done = 1;
1.79 + done = 1;
1.80 }
1.81
1.82 -int main(int argc, char *argv[])
1.83 +int
1.84 +main(int argc, char *argv[])
1.85 {
1.86 - char name[32];
1.87 + int i, n;
1.88 +
1.89 + /* Print available audio drivers */
1.90 + n = SDL_GetNumAudioDrivers();
1.91 + if (n == 0) {
1.92 + printf("No built-in audio drivers\n");
1.93 + } else {
1.94 + printf("Built-in audio drivers:");
1.95 + for (i = 0; i < n; ++i) {
1.96 + if (i > 0) {
1.97 + printf(",");
1.98 + }
1.99 + printf(" %s", SDL_GetAudioDriver(i));
1.100 + }
1.101 + printf("\n");
1.102 + }
1.103
1.104 - /* Load the SDL library */
1.105 - if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
1.106 - fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
1.107 - return(1);
1.108 - }
1.109 - if ( argv[1] == NULL ) {
1.110 - argv[1] = "sample.wav";
1.111 - }
1.112 - /* Load the wave file into memory */
1.113 - if ( SDL_LoadWAV(argv[1],
1.114 - &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
1.115 - fprintf(stderr, "Couldn't load %s: %s\n",
1.116 - argv[1], SDL_GetError());
1.117 - quit(1);
1.118 - }
1.119 + /* Load the SDL library */
1.120 + if (SDL_Init(SDL_INIT_AUDIO) < 0) {
1.121 + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
1.122 + return (1);
1.123 + }
1.124 + if (argv[1] == NULL) {
1.125 + argv[1] = "sample.wav";
1.126 + }
1.127 + /* Load the wave file into memory */
1.128 + if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
1.129 + fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
1.130 + quit(1);
1.131 + }
1.132
1.133 - wave.spec.callback = fillerup;
1.134 + wave.spec.callback = fillerup;
1.135 #if HAVE_SIGNAL_H
1.136 - /* Set the signals */
1.137 + /* Set the signals */
1.138 #ifdef SIGHUP
1.139 - signal(SIGHUP, poked);
1.140 + signal(SIGHUP, poked);
1.141 #endif
1.142 - signal(SIGINT, poked);
1.143 + signal(SIGINT, poked);
1.144 #ifdef SIGQUIT
1.145 - signal(SIGQUIT, poked);
1.146 + signal(SIGQUIT, poked);
1.147 #endif
1.148 - signal(SIGTERM, poked);
1.149 + signal(SIGTERM, poked);
1.150 #endif /* HAVE_SIGNAL_H */
1.151
1.152 - /* Initialize fillerup() variables */
1.153 - if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
1.154 - fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
1.155 - SDL_FreeWAV(wave.sound);
1.156 - quit(2);
1.157 - }
1.158 - SDL_PauseAudio(0);
1.159 + /* Initialize fillerup() variables */
1.160 + if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
1.161 + fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
1.162 + SDL_FreeWAV(wave.sound);
1.163 + quit(2);
1.164 + }
1.165 + SDL_PauseAudio(0);
1.166
1.167 - /* Let the audio run */
1.168 - printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
1.169 - while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
1.170 - SDL_Delay(1000);
1.171 + /* Let the audio run */
1.172 + printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
1.173 + while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
1.174 + SDL_Delay(1000);
1.175
1.176 - /* Clean up on signal */
1.177 - SDL_CloseAudio();
1.178 - SDL_FreeWAV(wave.sound);
1.179 - SDL_Quit();
1.180 - return(0);
1.181 + /* Clean up on signal */
1.182 + SDL_CloseAudio();
1.183 + SDL_FreeWAV(wave.sound);
1.184 + SDL_Quit();
1.185 + return (0);
1.186 }