test/loopwave.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 14 Aug 2013 23:30:10 -0700
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 8149 681eb46b8ac4
child 8535 e8ee0708ef5c
permissions -rw-r--r--
Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
     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 
    83 	/* Enable standard application logging */
    84 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    85 
    86     /* Load the SDL library */
    87     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    88         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    89         return (1);
    90     }
    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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], 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     SDL_Log("Available audio drivers:");
   116     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   117 		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   118     }
   119 
   120     /* Initialize fillerup() variables */
   121     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   122         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   123         SDL_FreeWAV(wave.sound);
   124         quit(2);
   125     }
   126 
   127     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   128 
   129     /* Let the audio run */
   130     SDL_PauseAudio(0);
   131     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   132         SDL_Delay(1000);
   133 
   134     /* Clean up on signal */
   135     SDL_CloseAudio();
   136     SDL_FreeWAV(wave.sound);
   137     SDL_Quit();
   138     return (0);
   139 }
   140 
   141 /* vi: set ts=4 sw=4 expandtab: */