test/loopwavequeue.c
changeset 9012 aa058c87737b
child 9079 373df87521e5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/loopwavequeue.c	Tue Jul 22 21:41:49 2014 -0400
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2014 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 queueing */
    1.17 +
    1.18 +#include <stdio.h>
    1.19 +#include <stdlib.h>
    1.20 +
    1.21 +#if HAVE_SIGNAL_H
    1.22 +#include <signal.h>
    1.23 +#endif
    1.24 +
    1.25 +#include "SDL.h"
    1.26 +
    1.27 +struct
    1.28 +{
    1.29 +    SDL_AudioSpec spec;
    1.30 +    Uint8 *sound;               /* Pointer to wave data */
    1.31 +    Uint32 soundlen;            /* Length of wave data */
    1.32 +    int soundpos;               /* Current play position */
    1.33 +} wave;
    1.34 +
    1.35 +
    1.36 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.37 +static void
    1.38 +quit(int rc)
    1.39 +{
    1.40 +    SDL_Quit();
    1.41 +    exit(rc);
    1.42 +}
    1.43 +
    1.44 +static int done = 0;
    1.45 +void
    1.46 +poked(int sig)
    1.47 +{
    1.48 +    done = 1;
    1.49 +}
    1.50 +
    1.51 +int
    1.52 +main(int argc, char *argv[])
    1.53 +{
    1.54 +    int i;
    1.55 +    char filename[4096];
    1.56 +
    1.57 +	/* Enable standard application logging */
    1.58 +	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    1.59 +
    1.60 +    /* Load the SDL library */
    1.61 +    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    1.62 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.63 +        return (1);
    1.64 +    }
    1.65 +
    1.66 +    if (argc > 1) {
    1.67 +        SDL_strlcpy(filename, argv[1], sizeof(filename));
    1.68 +    } else {
    1.69 +        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    1.70 +    }
    1.71 +    /* Load the wave file into memory */
    1.72 +    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    1.73 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
    1.74 +        quit(1);
    1.75 +    }
    1.76 +
    1.77 +    wave.spec.callback = NULL;  /* we'll push audio. */
    1.78 +
    1.79 +#if HAVE_SIGNAL_H
    1.80 +    /* Set the signals */
    1.81 +#ifdef SIGHUP
    1.82 +    signal(SIGHUP, poked);
    1.83 +#endif
    1.84 +    signal(SIGINT, poked);
    1.85 +#ifdef SIGQUIT
    1.86 +    signal(SIGQUIT, poked);
    1.87 +#endif
    1.88 +    signal(SIGTERM, poked);
    1.89 +#endif /* HAVE_SIGNAL_H */
    1.90 +
    1.91 +    /* Initialize fillerup() variables */
    1.92 +    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
    1.93 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    1.94 +        SDL_FreeWAV(wave.sound);
    1.95 +        quit(2);
    1.96 +    }
    1.97 +
    1.98 +    /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
    1.99 +
   1.100 +    /* Let the audio run */
   1.101 +    SDL_PauseAudio(0);
   1.102 +
   1.103 +    /* Note that we stuff the entire audio buffer into the queue in one
   1.104 +       shot. Most apps would want to feed it a little at a time, as it
   1.105 +       plays, but we're going for simplicity here. */
   1.106 +    
   1.107 +    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   1.108 +    {
   1.109 +        /* The device from SDL_OpenAudio() is always device #1. */
   1.110 +        const Uint32 queued = SDL_GetQueuedAudioSize(1);
   1.111 +        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
   1.112 +        if (queued <= 8192) {  /* time to requeue the whole thing? */
   1.113 +            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
   1.114 +                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
   1.115 +            } else {
   1.116 +                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
   1.117 +            }
   1.118 +        }
   1.119 +
   1.120 +        SDL_Delay(100);  /* let it play for awhile. */
   1.121 +    }
   1.122 +
   1.123 +    /* Clean up on signal */
   1.124 +    SDL_CloseAudio();
   1.125 +    SDL_FreeWAV(wave.sound);
   1.126 +    SDL_Quit();
   1.127 +    return 0;
   1.128 +}
   1.129 +
   1.130 +/* vi: set ts=4 sw=4 expandtab: */