test/loopwave.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 2049 5f6550e5184f
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
    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 	char name[32];
    70     int i, n;
    66 
    71 
    67 	/* Load the SDL library */
    72     /* Print available audio drivers */
    68 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    73     n = SDL_GetNumAudioDrivers();
    69 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    74     if (n == 0) {
    70 		return(1);
    75         printf("No built-in audio drivers\n");
    71 	}
    76     } else {
    72 	if ( argv[1] == NULL ) {
    77         printf("Built-in audio drivers:");
    73 		argv[1] = "sample.wav";
    78         for (i = 0; i < n; ++i) {
    74 	}
    79             if (i > 0) {
    75 	/* Load the wave file into memory */
    80                 printf(",");
    76 	if ( SDL_LoadWAV(argv[1],
    81             }
    77 			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
    82             printf(" %s", SDL_GetAudioDriver(i));
    78 		fprintf(stderr, "Couldn't load %s: %s\n",
    83         }
    79 						argv[1], SDL_GetError());
    84         printf("\n");
    80 		quit(1);
    85     }
    81 	}
       
    82 
    86 
    83 	wave.spec.callback = fillerup;
    87     /* Load the SDL library */
       
    88     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
       
    89         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
       
    90         return (1);
       
    91     }
       
    92     if (argv[1] == NULL) {
       
    93         argv[1] = "sample.wav";
       
    94     }
       
    95     /* Load the wave file into memory */
       
    96     if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
       
    97         fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
       
    98         quit(1);
       
    99     }
       
   100 
       
   101     wave.spec.callback = fillerup;
    84 #if HAVE_SIGNAL_H
   102 #if HAVE_SIGNAL_H
    85 	/* Set the signals */
   103     /* Set the signals */
    86 #ifdef SIGHUP
   104 #ifdef SIGHUP
    87 	signal(SIGHUP, poked);
   105     signal(SIGHUP, poked);
    88 #endif
   106 #endif
    89 	signal(SIGINT, poked);
   107     signal(SIGINT, poked);
    90 #ifdef SIGQUIT
   108 #ifdef SIGQUIT
    91 	signal(SIGQUIT, poked);
   109     signal(SIGQUIT, poked);
    92 #endif
   110 #endif
    93 	signal(SIGTERM, poked);
   111     signal(SIGTERM, poked);
    94 #endif /* HAVE_SIGNAL_H */
   112 #endif /* HAVE_SIGNAL_H */
    95 
   113 
    96 	/* Initialize fillerup() variables */
   114     /* Initialize fillerup() variables */
    97 	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
   115     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
    98 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   116         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
    99 		SDL_FreeWAV(wave.sound);
   117         SDL_FreeWAV(wave.sound);
   100 		quit(2);
   118         quit(2);
   101 	}
   119     }
   102 	SDL_PauseAudio(0);
   120     SDL_PauseAudio(0);
   103 
   121 
   104 	/* Let the audio run */
   122     /* Let the audio run */
   105 	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
   123     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   106 	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
   124     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   107 		SDL_Delay(1000);
   125         SDL_Delay(1000);
   108 
   126 
   109 	/* Clean up on signal */
   127     /* Clean up on signal */
   110 	SDL_CloseAudio();
   128     SDL_CloseAudio();
   111 	SDL_FreeWAV(wave.sound);
   129     SDL_FreeWAV(wave.sound);
   112 	SDL_Quit();
   130     SDL_Quit();
   113 	return(0);
   131     return (0);
   114 }
   132 }