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