test/loopwavequeue.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 15 Feb 2015 11:33:39 +0100
changeset 9348 7bda52b051f9
parent 9079 373df87521e5
child 9420 5ffb5a958c43
permissions -rw-r--r--
Fixed compile warning about unused variable in wave test program.
     1 /*
     2   Copyright (C) 1997-2014 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 queueing */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 
    18 #include "SDL.h"
    19 
    20 #if HAVE_SIGNAL_H
    21 #include <signal.h>
    22 #endif
    23 
    24 struct
    25 {
    26     SDL_AudioSpec spec;
    27     Uint8 *sound;               /* Pointer to wave data */
    28     Uint32 soundlen;            /* Length of wave data */
    29     int soundpos;               /* Current play position */
    30 } wave;
    31 
    32 
    33 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    34 static void
    35 quit(int rc)
    36 {
    37     SDL_Quit();
    38     exit(rc);
    39 }
    40 
    41 static int done = 0;
    42 void
    43 poked(int sig)
    44 {
    45     done = 1;
    46 }
    47 
    48 int
    49 main(int argc, char *argv[])
    50 {
    51     char filename[4096];
    52 
    53 	/* Enable standard application logging */
    54 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    55 
    56     /* Load the SDL library */
    57     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    58         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    59         return (1);
    60     }
    61 
    62     if (argc > 1) {
    63         SDL_strlcpy(filename, argv[1], sizeof(filename));
    64     } else {
    65         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    66     }
    67     /* Load the wave file into memory */
    68     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
    69         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
    70         quit(1);
    71     }
    72 
    73     wave.spec.callback = NULL;  /* we'll push audio. */
    74 
    75 #if HAVE_SIGNAL_H
    76     /* Set the signals */
    77 #ifdef SIGHUP
    78     signal(SIGHUP, poked);
    79 #endif
    80     signal(SIGINT, poked);
    81 #ifdef SIGQUIT
    82     signal(SIGQUIT, poked);
    83 #endif
    84     signal(SIGTERM, poked);
    85 #endif /* HAVE_SIGNAL_H */
    86 
    87     /* Initialize fillerup() variables */
    88     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
    89         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
    90         SDL_FreeWAV(wave.sound);
    91         quit(2);
    92     }
    93 
    94     /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
    95 
    96     /* Let the audio run */
    97     SDL_PauseAudio(0);
    98 
    99     /* Note that we stuff the entire audio buffer into the queue in one
   100        shot. Most apps would want to feed it a little at a time, as it
   101        plays, but we're going for simplicity here. */
   102     
   103     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   104     {
   105         /* The device from SDL_OpenAudio() is always device #1. */
   106         const Uint32 queued = SDL_GetQueuedAudioSize(1);
   107         SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
   108         if (queued <= 8192) {  /* time to requeue the whole thing? */
   109             if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
   110                 SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
   111             } else {
   112                 SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
   113             }
   114         }
   115 
   116         SDL_Delay(100);  /* let it play for awhile. */
   117     }
   118 
   119     /* Clean up on signal */
   120     SDL_CloseAudio();
   121     SDL_FreeWAV(wave.sound);
   122     SDL_Quit();
   123     return 0;
   124 }
   125 
   126 /* vi: set ts=4 sw=4 expandtab: */