test/loopwave.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Program to load a wave file and loop playing it using SDL sound */
    14 
    15 /* loopwaves.c is much more robust in handling WAVE files --
    16     This is only for simple WAVEs
    17 */
    18 #include "SDL_config.h"
    19 
    20 #include <stdio.h>
    21 #include <stdlib.h>
    22 
    23 #if HAVE_SIGNAL_H
    24 #include <signal.h>
    25 #endif
    26 
    27 #include "SDL.h"
    28 #include "SDL_audio.h"
    29 
    30 struct
    31 {
    32     SDL_AudioSpec spec;
    33     Uint8 *sound;               /* Pointer to wave data */
    34     Uint32 soundlen;            /* Length of wave data */
    35     int soundpos;               /* Current play position */
    36 } wave;
    37 
    38 
    39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    40 static void
    41 quit(int rc)
    42 {
    43     SDL_Quit();
    44     exit(rc);
    45 }
    46 
    47 
    48 void SDLCALL
    49 fillerup(void *unused, Uint8 * stream, int len)
    50 {
    51     Uint8 *waveptr;
    52     int waveleft;
    53 
    54     /* Set up the pointers */
    55     waveptr = wave.sound + wave.soundpos;
    56     waveleft = wave.soundlen - wave.soundpos;
    57 
    58     /* Go! */
    59     while (waveleft <= len) {
    60         SDL_memcpy(stream, waveptr, waveleft);
    61         stream += waveleft;
    62         len -= waveleft;
    63         waveptr = wave.sound;
    64         waveleft = wave.soundlen;
    65         wave.soundpos = 0;
    66     }
    67     SDL_memcpy(stream, waveptr, len);
    68     wave.soundpos += len;
    69 }
    70 
    71 static int done = 0;
    72 void
    73 poked(int sig)
    74 {
    75     done = 1;
    76 }
    77 
    78 int
    79 main(int argc, char *argv[])
    80 {
    81     int i;
    82 
    83     /* Load the SDL library */
    84     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    85         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    86         return (1);
    87     }
    88 
    89     if (argv[1] == NULL) {
    90         argv[1] = "sample.wav";
    91     }
    92     /* Load the wave file into memory */
    93     if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    94         fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
    95         quit(1);
    96     }
    97 
    98     wave.spec.callback = fillerup;
    99 #if HAVE_SIGNAL_H
   100     /* Set the signals */
   101 #ifdef SIGHUP
   102     signal(SIGHUP, poked);
   103 #endif
   104     signal(SIGINT, poked);
   105 #ifdef SIGQUIT
   106     signal(SIGQUIT, poked);
   107 #endif
   108     signal(SIGTERM, poked);
   109 #endif /* HAVE_SIGNAL_H */
   110 
   111     /* Show the list of available drivers */
   112     printf("Available audio drivers: ");
   113     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   114         if (i == 0) {
   115             printf("%s", SDL_GetAudioDriver(i));
   116         } else {
   117             printf(", %s", SDL_GetAudioDriver(i));
   118         }
   119     }
   120     printf("\n");
   121 
   122     /* Initialize fillerup() variables */
   123     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   124         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   125         SDL_FreeWAV(wave.sound);
   126         quit(2);
   127     }
   128 
   129     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   130 
   131     /* Let the audio run */
   132     SDL_PauseAudio(0);
   133     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   134         SDL_Delay(1000);
   135 
   136     /* Clean up on signal */
   137     SDL_CloseAudio();
   138     SDL_FreeWAV(wave.sound);
   139     SDL_Quit();
   140     return (0);
   141 }
   142 
   143 /* vi: set ts=4 sw=4 expandtab: */