test/loopwave.c
changeset 8535 e8ee0708ef5c
parent 8477 ad08f0d710f3
parent 7639 9406b7dd2f2d
child 8582 c3e9a2b93517
     1.1 --- a/test/loopwave.c	Sun Oct 27 14:31:57 2013 -0400
     1.2 +++ b/test/loopwave.c	Sun Oct 27 21:26:46 2013 -0400
     1.3 @@ -1,146 +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 -    /* Load the SDL library */
    1.88 -    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    1.89 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.90 -        return (1);
    1.91 -    }
    1.92 -
    1.93 -    if (argc >= 1) {
    1.94 -        SDL_strlcpy(filename, argv[1], sizeof(filename));
    1.95 -    } else {
    1.96 -        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    1.97 -    }
    1.98 -    /* Load the wave file into memory */
    1.99 -    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   1.100 -        fprintf(stderr, "Couldn't load %s: %s\n", filename, SDL_GetError());
   1.101 -        quit(1);
   1.102 -    }
   1.103 -
   1.104 -    wave.spec.callback = fillerup;
   1.105 -#if HAVE_SIGNAL_H
   1.106 -    /* Set the signals */
   1.107 -#ifdef SIGHUP
   1.108 -    signal(SIGHUP, poked);
   1.109 -#endif
   1.110 -    signal(SIGINT, poked);
   1.111 -#ifdef SIGQUIT
   1.112 -    signal(SIGQUIT, poked);
   1.113 -#endif
   1.114 -    signal(SIGTERM, poked);
   1.115 -#endif /* HAVE_SIGNAL_H */
   1.116 -
   1.117 -    /* Show the list of available drivers */
   1.118 -    printf("Available audio drivers: ");
   1.119 -    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   1.120 -        if (i == 0) {
   1.121 -            printf("%s", SDL_GetAudioDriver(i));
   1.122 -        } else {
   1.123 -            printf(", %s", SDL_GetAudioDriver(i));
   1.124 -        }
   1.125 -    }
   1.126 -    printf("\n");
   1.127 -
   1.128 -    /* Initialize fillerup() variables */
   1.129 -    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   1.130 -        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   1.131 -        SDL_FreeWAV(wave.sound);
   1.132 -        quit(2);
   1.133 -    }
   1.134 -
   1.135 -    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.136 -
   1.137 -    /* Let the audio run */
   1.138 -    SDL_PauseAudio(0);
   1.139 -    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.140 -        SDL_Delay(1000);
   1.141 -
   1.142 -    /* Clean up on signal */
   1.143 -    SDL_CloseAudio();
   1.144 -    SDL_FreeWAV(wave.sound);
   1.145 -    SDL_Quit();
   1.146 -    return (0);
   1.147 -}
   1.148 -
   1.149 -/* vi: set ts=4 sw=4 expandtab: */
   1.150 +/*
   1.151 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
   1.152 +
   1.153 +  This software is provided 'as-is', without any express or implied
   1.154 +  warranty.  In no event will the authors be held liable for any damages
   1.155 +  arising from the use of this software.
   1.156 +
   1.157 +  Permission is granted to anyone to use this software for any purpose,
   1.158 +  including commercial applications, and to alter it and redistribute it
   1.159 +  freely.
   1.160 +*/
   1.161 +
   1.162 +/* Program to load a wave file and loop playing it using SDL sound */
   1.163 +
   1.164 +/* loopwaves.c is much more robust in handling WAVE files --
   1.165 +    This is only for simple WAVEs
   1.166 +*/
   1.167 +#include "SDL_config.h"
   1.168 +
   1.169 +#include <stdio.h>
   1.170 +#include <stdlib.h>
   1.171 +
   1.172 +#if HAVE_SIGNAL_H
   1.173 +#include <signal.h>
   1.174 +#endif
   1.175 +
   1.176 +#include "SDL.h"
   1.177 +#include "SDL_audio.h"
   1.178 +
   1.179 +struct
   1.180 +{
   1.181 +    SDL_AudioSpec spec;
   1.182 +    Uint8 *sound;               /* Pointer to wave data */
   1.183 +    Uint32 soundlen;            /* Length of wave data */
   1.184 +    int soundpos;               /* Current play position */
   1.185 +} wave;
   1.186 +
   1.187 +
   1.188 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   1.189 +static void
   1.190 +quit(int rc)
   1.191 +{
   1.192 +    SDL_Quit();
   1.193 +    exit(rc);
   1.194 +}
   1.195 +
   1.196 +
   1.197 +void SDLCALL
   1.198 +fillerup(void *unused, Uint8 * stream, int len)
   1.199 +{
   1.200 +    Uint8 *waveptr;
   1.201 +    int waveleft;
   1.202 +
   1.203 +    /* Set up the pointers */
   1.204 +    waveptr = wave.sound + wave.soundpos;
   1.205 +    waveleft = wave.soundlen - wave.soundpos;
   1.206 +
   1.207 +    /* Go! */
   1.208 +    while (waveleft <= len) {
   1.209 +        SDL_memcpy(stream, waveptr, waveleft);
   1.210 +        stream += waveleft;
   1.211 +        len -= waveleft;
   1.212 +        waveptr = wave.sound;
   1.213 +        waveleft = wave.soundlen;
   1.214 +        wave.soundpos = 0;
   1.215 +    }
   1.216 +    SDL_memcpy(stream, waveptr, len);
   1.217 +    wave.soundpos += len;
   1.218 +}
   1.219 +
   1.220 +static int done = 0;
   1.221 +void
   1.222 +poked(int sig)
   1.223 +{
   1.224 +    done = 1;
   1.225 +}
   1.226 +
   1.227 +int
   1.228 +main(int argc, char *argv[])
   1.229 +{
   1.230 +    int i;
   1.231 +    char filename[4096];
   1.232 +
   1.233 +	/* Enable standard application logging */
   1.234 +	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.235 +
   1.236 +    /* Load the SDL library */
   1.237 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   1.238 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.239 +        return (1);
   1.240 +    }
   1.241 +
   1.242 +    if (argc >= 1) {
   1.243 +        SDL_strlcpy(filename, argv[1], sizeof(filename));
   1.244 +    } else {
   1.245 +        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
   1.246 +    }
   1.247 +    /* Load the wave file into memory */
   1.248 +    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   1.249 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
   1.250 +        quit(1);
   1.251 +    }
   1.252 +
   1.253 +    wave.spec.callback = fillerup;
   1.254 +#if HAVE_SIGNAL_H
   1.255 +    /* Set the signals */
   1.256 +#ifdef SIGHUP
   1.257 +    signal(SIGHUP, poked);
   1.258 +#endif
   1.259 +    signal(SIGINT, poked);
   1.260 +#ifdef SIGQUIT
   1.261 +    signal(SIGQUIT, poked);
   1.262 +#endif
   1.263 +    signal(SIGTERM, poked);
   1.264 +#endif /* HAVE_SIGNAL_H */
   1.265 +
   1.266 +    /* Show the list of available drivers */
   1.267 +    SDL_Log("Available audio drivers:");
   1.268 +    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   1.269 +		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   1.270 +    }
   1.271 +
   1.272 +    /* Initialize fillerup() variables */
   1.273 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   1.274 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   1.275 +        SDL_FreeWAV(wave.sound);
   1.276 +        quit(2);
   1.277 +    }
   1.278 +
   1.279 +    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   1.280 +
   1.281 +    /* Let the audio run */
   1.282 +    SDL_PauseAudio(0);
   1.283 +    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.284 +        SDL_Delay(1000);
   1.285 +
   1.286 +    /* Clean up on signal */
   1.287 +    SDL_CloseAudio();
   1.288 +    SDL_FreeWAV(wave.sound);
   1.289 +    SDL_Quit();
   1.290 +    return (0);
   1.291 +}
   1.292 +
   1.293 +/* vi: set ts=4 sw=4 expandtab: */