home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / SDL-Amiga / demo / loopwave.c < prev    next >
C/C++ Source or Header  |  2000-08-22  |  2KB  |  93 lines

  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.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #include "mydebug.h"
  12.  
  13. #include "SDL.h"
  14. #include "SDL_audio.h"
  15.  
  16. struct {
  17.     SDL_AudioSpec spec;
  18.     Uint8   *sound;            /* Pointer to wave data */
  19.     Uint32   soundlen;        /* Length of wave data */
  20.     int      soundpos;        /* Current play position */
  21. } wave;
  22.  
  23. int done;
  24.  
  25. void fillerup(void *unused, Uint8 *stream, int len)
  26. {
  27.     Uint8 *waveptr;
  28.     int    wavetoplay;
  29.  
  30.     if( (wave.soundlen-wave.soundpos)<=0 )
  31.     {
  32.         D(bug("Playing finito!\n"));
  33.         done=1;
  34.         return;
  35.     }
  36.  
  37.     /* Set up the pointers */
  38.     waveptr = wave.sound + wave.soundpos;
  39.  
  40.     wavetoplay = (wave.soundlen - wave.soundpos)>len ? len : (wave.soundlen - wave.soundpos);
  41.  
  42.     /* Go! */
  43.     D(bug("Mixxo %ld bytes, da %lx...\n",wavetoplay,waveptr));
  44.     SDL_MixAudio(stream, waveptr, wavetoplay, SDL_MIX_MAXVOLUME);
  45.  
  46.     wave.soundpos += wavetoplay;
  47. }
  48.  
  49. main(int argc, char *argv[])
  50. {
  51.     /* Load the SDL library */
  52.     if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
  53.         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  54.         exit(1);
  55.     }
  56.     atexit(SDL_Quit);
  57.  
  58.     if ( argv[1] == NULL ) {
  59.         fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
  60.         exit(1);
  61.     }
  62.  
  63.     /* Load the wave file into memory */
  64.     if ( SDL_LoadWAV(argv[1],
  65.             &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
  66.         fprintf(stderr, "Couldn't load %s: %s\n",
  67.                         argv[1], SDL_GetError());
  68.         exit(1);
  69.     }
  70.  
  71.     D(bug("Suono wav: len:%ld data:%lx T:%lx Fq:%ld C:%d\n",wave.soundlen,wave.sound,wave.spec.format,wave.spec.freq,wave.spec.channels));
  72.     wave.spec.callback = fillerup;
  73.  
  74.     /* Initialize fillerup() variables */
  75.     if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
  76.         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
  77.         SDL_FreeWAV(wave.sound);
  78.         exit(2);
  79.     }
  80.  
  81.     D(bug("Avvio il playback...\n"));
  82.     SDL_PauseAudio(0);
  83.  
  84.     /* Let the audio run */
  85.     while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
  86.         SDL_Delay(1000);
  87.  
  88.     /* Clean up on signal */
  89.     SDL_CloseAudio();
  90.     SDL_FreeWAV(wave.sound);
  91.     exit(0);
  92. }
  93.