test/loopwavequeue.c
author Edward Rudd <urkle@outoforder.cc>
Wed, 03 Dec 2014 12:47:39 -0500
changeset 9262 b3df9db209b0
parent 9079 373df87521e5
child 9348 7bda52b051f9
permissions -rw-r--r--
ugh.. stray character
icculus@9012
     1
/*
icculus@9012
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
icculus@9012
     3
icculus@9012
     4
  This software is provided 'as-is', without any express or implied
icculus@9012
     5
  warranty.  In no event will the authors be held liable for any damages
icculus@9012
     6
  arising from the use of this software.
icculus@9012
     7
icculus@9012
     8
  Permission is granted to anyone to use this software for any purpose,
icculus@9012
     9
  including commercial applications, and to alter it and redistribute it
icculus@9012
    10
  freely.
icculus@9012
    11
*/
icculus@9012
    12
icculus@9012
    13
/* Program to load a wave file and loop playing it using SDL sound queueing */
icculus@9012
    14
icculus@9012
    15
#include <stdio.h>
icculus@9012
    16
#include <stdlib.h>
icculus@9012
    17
slouken@9079
    18
#include "SDL.h"
slouken@9079
    19
icculus@9012
    20
#if HAVE_SIGNAL_H
icculus@9012
    21
#include <signal.h>
icculus@9012
    22
#endif
icculus@9012
    23
icculus@9012
    24
struct
icculus@9012
    25
{
icculus@9012
    26
    SDL_AudioSpec spec;
icculus@9012
    27
    Uint8 *sound;               /* Pointer to wave data */
icculus@9012
    28
    Uint32 soundlen;            /* Length of wave data */
icculus@9012
    29
    int soundpos;               /* Current play position */
icculus@9012
    30
} wave;
icculus@9012
    31
icculus@9012
    32
icculus@9012
    33
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@9012
    34
static void
icculus@9012
    35
quit(int rc)
icculus@9012
    36
{
icculus@9012
    37
    SDL_Quit();
icculus@9012
    38
    exit(rc);
icculus@9012
    39
}
icculus@9012
    40
icculus@9012
    41
static int done = 0;
icculus@9012
    42
void
icculus@9012
    43
poked(int sig)
icculus@9012
    44
{
icculus@9012
    45
    done = 1;
icculus@9012
    46
}
icculus@9012
    47
icculus@9012
    48
int
icculus@9012
    49
main(int argc, char *argv[])
icculus@9012
    50
{
icculus@9012
    51
    int i;
icculus@9012
    52
    char filename[4096];
icculus@9012
    53
icculus@9012
    54
	/* Enable standard application logging */
icculus@9012
    55
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9012
    56
icculus@9012
    57
    /* Load the SDL library */
icculus@9012
    58
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
icculus@9012
    59
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@9012
    60
        return (1);
icculus@9012
    61
    }
icculus@9012
    62
icculus@9012
    63
    if (argc > 1) {
icculus@9012
    64
        SDL_strlcpy(filename, argv[1], sizeof(filename));
icculus@9012
    65
    } else {
icculus@9012
    66
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
icculus@9012
    67
    }
icculus@9012
    68
    /* Load the wave file into memory */
icculus@9012
    69
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
icculus@9012
    70
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
icculus@9012
    71
        quit(1);
icculus@9012
    72
    }
icculus@9012
    73
icculus@9012
    74
    wave.spec.callback = NULL;  /* we'll push audio. */
icculus@9012
    75
icculus@9012
    76
#if HAVE_SIGNAL_H
icculus@9012
    77
    /* Set the signals */
icculus@9012
    78
#ifdef SIGHUP
icculus@9012
    79
    signal(SIGHUP, poked);
icculus@9012
    80
#endif
icculus@9012
    81
    signal(SIGINT, poked);
icculus@9012
    82
#ifdef SIGQUIT
icculus@9012
    83
    signal(SIGQUIT, poked);
icculus@9012
    84
#endif
icculus@9012
    85
    signal(SIGTERM, poked);
icculus@9012
    86
#endif /* HAVE_SIGNAL_H */
icculus@9012
    87
icculus@9012
    88
    /* Initialize fillerup() variables */
icculus@9012
    89
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
icculus@9012
    90
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
icculus@9012
    91
        SDL_FreeWAV(wave.sound);
icculus@9012
    92
        quit(2);
icculus@9012
    93
    }
icculus@9012
    94
icculus@9012
    95
    /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
icculus@9012
    96
icculus@9012
    97
    /* Let the audio run */
icculus@9012
    98
    SDL_PauseAudio(0);
icculus@9012
    99
icculus@9012
   100
    /* Note that we stuff the entire audio buffer into the queue in one
icculus@9012
   101
       shot. Most apps would want to feed it a little at a time, as it
icculus@9012
   102
       plays, but we're going for simplicity here. */
icculus@9012
   103
    
icculus@9012
   104
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
icculus@9012
   105
    {
icculus@9012
   106
        /* The device from SDL_OpenAudio() is always device #1. */
icculus@9012
   107
        const Uint32 queued = SDL_GetQueuedAudioSize(1);
icculus@9012
   108
        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
icculus@9012
   109
        if (queued <= 8192) {  /* time to requeue the whole thing? */
icculus@9012
   110
            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
icculus@9012
   111
                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
icculus@9012
   112
            } else {
icculus@9012
   113
                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
icculus@9012
   114
            }
icculus@9012
   115
        }
icculus@9012
   116
icculus@9012
   117
        SDL_Delay(100);  /* let it play for awhile. */
icculus@9012
   118
    }
icculus@9012
   119
icculus@9012
   120
    /* Clean up on signal */
icculus@9012
   121
    SDL_CloseAudio();
icculus@9012
   122
    SDL_FreeWAV(wave.sound);
icculus@9012
   123
    SDL_Quit();
icculus@9012
   124
    return 0;
icculus@9012
   125
}
icculus@9012
   126
icculus@9012
   127
/* vi: set ts=4 sw=4 expandtab: */