test/loopwave.c
author David Ludwig <dludwig@pobox.com>
Mon, 12 Aug 2013 22:29:55 -0400
changeset 8477 ad08f0d710f3
parent 8377 3a7e8110201e
parent 7517 965d57022c01
child 8535 e8ee0708ef5c
permissions -rw-r--r--
WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
     1 /*
     2   Copyright (C) 1997-2013 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     char filename[4096];
    83 
    84     /* Load the SDL library */
    85     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    86         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    87         return (1);
    88     }
    89 
    90     if (argc >= 1) {
    91         SDL_strlcpy(filename, argv[1], sizeof(filename));
    92     } else {
    93         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    94     }
    95     /* Load the wave file into memory */
    96     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    97         fprintf(stderr, "Couldn't load %s: %s\n", filename, SDL_GetError());
    98         quit(1);
    99     }
   100 
   101     wave.spec.callback = fillerup;
   102 #if HAVE_SIGNAL_H
   103     /* Set the signals */
   104 #ifdef SIGHUP
   105     signal(SIGHUP, poked);
   106 #endif
   107     signal(SIGINT, poked);
   108 #ifdef SIGQUIT
   109     signal(SIGQUIT, poked);
   110 #endif
   111     signal(SIGTERM, poked);
   112 #endif /* HAVE_SIGNAL_H */
   113 
   114     /* Show the list of available drivers */
   115     printf("Available audio drivers: ");
   116     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   117         if (i == 0) {
   118             printf("%s", SDL_GetAudioDriver(i));
   119         } else {
   120             printf(", %s", SDL_GetAudioDriver(i));
   121         }
   122     }
   123     printf("\n");
   124 
   125     /* Initialize fillerup() variables */
   126     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   127         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   128         SDL_FreeWAV(wave.sound);
   129         quit(2);
   130     }
   131 
   132     printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   133 
   134     /* Let the audio run */
   135     SDL_PauseAudio(0);
   136     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   137         SDL_Delay(1000);
   138 
   139     /* Clean up on signal */
   140     SDL_CloseAudio();
   141     SDL_FreeWAV(wave.sound);
   142     SDL_Quit();
   143     return (0);
   144 }
   145 
   146 /* vi: set ts=4 sw=4 expandtab: */