test/loopwave.c
changeset 8582 c3e9a2b93517
parent 8535 e8ee0708ef5c
child 8583 fb2933ca805f
     1.1 --- a/test/loopwave.c	Tue Mar 04 19:49:11 2014 -0500
     1.2 +++ b/test/loopwave.c	Sun Mar 09 11:06:11 2014 -0700
     1.3 @@ -1,144 +1,144 @@
     1.4 -/*
     1.5 -  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.6 -
     1.7 -  This software is provided 'as-is', without any express or implied
     1.8 -  warranty.  In no event will the authors be held liable for any damages
     1.9 -  arising from the use of this software.
    1.10 -
    1.11 -  Permission is granted to anyone to use this software for any purpose,
    1.12 -  including commercial applications, and to alter it and redistribute it
    1.13 -  freely.
    1.14 -*/
    1.15 -
    1.16 -/* Program to load a wave file and loop playing it using SDL sound */
    1.17 -
    1.18 -/* loopwaves.c is much more robust in handling WAVE files --
    1.19 -    This is only for simple WAVEs
    1.20 -*/
    1.21 -#include "SDL_config.h"
    1.22 -
    1.23 -#include <stdio.h>
    1.24 -#include <stdlib.h>
    1.25 -
    1.26 -#if HAVE_SIGNAL_H
    1.27 -#include <signal.h>
    1.28 -#endif
    1.29 -
    1.30 -#include "SDL.h"
    1.31 -#include "SDL_audio.h"
    1.32 -
    1.33 -struct
    1.34 -{
    1.35 -    SDL_AudioSpec spec;
    1.36 -    Uint8 *sound;               /* Pointer to wave data */
    1.37 -    Uint32 soundlen;            /* Length of wave data */
    1.38 -    int soundpos;               /* Current play position */
    1.39 -} wave;
    1.40 -
    1.41 -
    1.42 -/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.43 -static void
    1.44 -quit(int rc)
    1.45 -{
    1.46 -    SDL_Quit();
    1.47 -    exit(rc);
    1.48 -}
    1.49 -
    1.50 -
    1.51 -void SDLCALL
    1.52 -fillerup(void *unused, Uint8 * stream, int len)
    1.53 -{
    1.54 -    Uint8 *waveptr;
    1.55 -    int waveleft;
    1.56 -
    1.57 -    /* Set up the pointers */
    1.58 -    waveptr = wave.sound + wave.soundpos;
    1.59 -    waveleft = wave.soundlen - wave.soundpos;
    1.60 -
    1.61 -    /* Go! */
    1.62 -    while (waveleft <= len) {
    1.63 -        SDL_memcpy(stream, waveptr, waveleft);
    1.64 -        stream += waveleft;
    1.65 -        len -= waveleft;
    1.66 -        waveptr = wave.sound;
    1.67 -        waveleft = wave.soundlen;
    1.68 -        wave.soundpos = 0;
    1.69 -    }
    1.70 -    SDL_memcpy(stream, waveptr, len);
    1.71 -    wave.soundpos += len;
    1.72 -}
    1.73 -
    1.74 -static int done = 0;
    1.75 -void
    1.76 -poked(int sig)
    1.77 -{
    1.78 -    done = 1;
    1.79 -}
    1.80 -
    1.81 -int
    1.82 -main(int argc, char *argv[])
    1.83 -{
    1.84 -    int i;
    1.85 -    char filename[4096];
    1.86 -
    1.87 -	/* Enable standard application logging */
    1.88 -	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    1.89 -
    1.90 -    /* Load the SDL library */
    1.91 -    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    1.92 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.93 -        return (1);
    1.94 -    }
    1.95 -
    1.96 -    if (argc >= 1) {
    1.97 -        SDL_strlcpy(filename, argv[1], sizeof(filename));
    1.98 -    } else {
    1.99 -        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
   1.100 -    }
   1.101 -    /* Load the wave file into memory */
   1.102 -    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   1.103 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   1.104 -        quit(1);
   1.105 -    }
   1.106 -
   1.107 -    wave.spec.callback = fillerup;
   1.108 -#if HAVE_SIGNAL_H
   1.109 -    /* Set the signals */
   1.110 -#ifdef SIGHUP
   1.111 -    signal(SIGHUP, poked);
   1.112 -#endif
   1.113 -    signal(SIGINT, poked);
   1.114 -#ifdef SIGQUIT
   1.115 -    signal(SIGQUIT, poked);
   1.116 -#endif
   1.117 -    signal(SIGTERM, poked);
   1.118 -#endif /* HAVE_SIGNAL_H */
   1.119 -
   1.120 -    /* Show the list of available drivers */
   1.121 -    SDL_Log("Available audio drivers:");
   1.122 -    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   1.123 -		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   1.124 -    }
   1.125 -
   1.126 -    /* Initialize fillerup() variables */
   1.127 -    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   1.128 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   1.129 -        SDL_FreeWAV(wave.sound);
   1.130 -        quit(2);
   1.131 -    }
   1.132 -
   1.133 -    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.134 -
   1.135 -    /* Let the audio run */
   1.136 -    SDL_PauseAudio(0);
   1.137 -    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.138 -        SDL_Delay(1000);
   1.139 -
   1.140 -    /* Clean up on signal */
   1.141 -    SDL_CloseAudio();
   1.142 -    SDL_FreeWAV(wave.sound);
   1.143 -    SDL_Quit();
   1.144 -    return (0);
   1.145 -}
   1.146 -
   1.147 -/* vi: set ts=4 sw=4 expandtab: */
   1.148 +/*
   1.149 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
   1.150 +
   1.151 +  This software is provided 'as-is', without any express or implied
   1.152 +  warranty.  In no event will the authors be held liable for any damages
   1.153 +  arising from the use of this software.
   1.154 +
   1.155 +  Permission is granted to anyone to use this software for any purpose,
   1.156 +  including commercial applications, and to alter it and redistribute it
   1.157 +  freely.
   1.158 +*/
   1.159 +
   1.160 +/* Program to load a wave file and loop playing it using SDL sound */
   1.161 +
   1.162 +/* loopwaves.c is much more robust in handling WAVE files --
   1.163 +    This is only for simple WAVEs
   1.164 +*/
   1.165 +#include "SDL_config.h"
   1.166 +
   1.167 +#include <stdio.h>
   1.168 +#include <stdlib.h>
   1.169 +
   1.170 +#if HAVE_SIGNAL_H
   1.171 +#include <signal.h>
   1.172 +#endif
   1.173 +
   1.174 +#include "SDL.h"
   1.175 +#include "SDL_audio.h"
   1.176 +
   1.177 +struct
   1.178 +{
   1.179 +    SDL_AudioSpec spec;
   1.180 +    Uint8 *sound;               /* Pointer to wave data */
   1.181 +    Uint32 soundlen;            /* Length of wave data */
   1.182 +    int soundpos;               /* Current play position */
   1.183 +} wave;
   1.184 +
   1.185 +
   1.186 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   1.187 +static void
   1.188 +quit(int rc)
   1.189 +{
   1.190 +    SDL_Quit();
   1.191 +    exit(rc);
   1.192 +}
   1.193 +
   1.194 +
   1.195 +void SDLCALL
   1.196 +fillerup(void *unused, Uint8 * stream, int len)
   1.197 +{
   1.198 +    Uint8 *waveptr;
   1.199 +    int waveleft;
   1.200 +
   1.201 +    /* Set up the pointers */
   1.202 +    waveptr = wave.sound + wave.soundpos;
   1.203 +    waveleft = wave.soundlen - wave.soundpos;
   1.204 +
   1.205 +    /* Go! */
   1.206 +    while (waveleft <= len) {
   1.207 +        SDL_memcpy(stream, waveptr, waveleft);
   1.208 +        stream += waveleft;
   1.209 +        len -= waveleft;
   1.210 +        waveptr = wave.sound;
   1.211 +        waveleft = wave.soundlen;
   1.212 +        wave.soundpos = 0;
   1.213 +    }
   1.214 +    SDL_memcpy(stream, waveptr, len);
   1.215 +    wave.soundpos += len;
   1.216 +}
   1.217 +
   1.218 +static int done = 0;
   1.219 +void
   1.220 +poked(int sig)
   1.221 +{
   1.222 +    done = 1;
   1.223 +}
   1.224 +
   1.225 +int
   1.226 +main(int argc, char *argv[])
   1.227 +{
   1.228 +    int i;
   1.229 +    char filename[4096];
   1.230 +
   1.231 +	/* Enable standard application logging */
   1.232 +	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.233 +
   1.234 +    /* Load the SDL library */
   1.235 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   1.236 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.237 +        return (1);
   1.238 +    }
   1.239 +
   1.240 +    if (argc >= 1) {
   1.241 +        SDL_strlcpy(filename, argv[1], sizeof(filename));
   1.242 +    } else {
   1.243 +        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
   1.244 +    }
   1.245 +    /* Load the wave file into memory */
   1.246 +    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   1.247 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   1.248 +        quit(1);
   1.249 +    }
   1.250 +
   1.251 +    wave.spec.callback = fillerup;
   1.252 +#if HAVE_SIGNAL_H
   1.253 +    /* Set the signals */
   1.254 +#ifdef SIGHUP
   1.255 +    signal(SIGHUP, poked);
   1.256 +#endif
   1.257 +    signal(SIGINT, poked);
   1.258 +#ifdef SIGQUIT
   1.259 +    signal(SIGQUIT, poked);
   1.260 +#endif
   1.261 +    signal(SIGTERM, poked);
   1.262 +#endif /* HAVE_SIGNAL_H */
   1.263 +
   1.264 +    /* Show the list of available drivers */
   1.265 +    SDL_Log("Available audio drivers:");
   1.266 +    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   1.267 +		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   1.268 +    }
   1.269 +
   1.270 +    /* Initialize fillerup() variables */
   1.271 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   1.272 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   1.273 +        SDL_FreeWAV(wave.sound);
   1.274 +        quit(2);
   1.275 +    }
   1.276 +
   1.277 +    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.278 +
   1.279 +    /* Let the audio run */
   1.280 +    SDL_PauseAudio(0);
   1.281 +    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.282 +        SDL_Delay(1000);
   1.283 +
   1.284 +    /* Clean up on signal */
   1.285 +    SDL_CloseAudio();
   1.286 +    SDL_FreeWAV(wave.sound);
   1.287 +    SDL_Quit();
   1.288 +    return (0);
   1.289 +}
   1.290 +
   1.291 +/* vi: set ts=4 sw=4 expandtab: */