test/loopwave.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 25 Nov 2015 21:39:28 +0100
changeset 9922 b066d5fd5fa7
parent 9619 b94b6d0bff0f
child 9945 09d546e9d8a1
permissions -rw-r--r--
Replaced tabs with spaces in test programs.
slouken@8582
     1
/*
slouken@9619
     2
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     3
slouken@8582
     4
  This software is provided 'as-is', without any express or implied
slouken@8582
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     6
  arising from the use of this software.
slouken@8582
     7
slouken@8582
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
     9
  including commercial applications, and to alter it and redistribute it
slouken@8582
    10
  freely.
slouken@8582
    11
*/
slouken@8582
    12
slouken@8582
    13
/* Program to load a wave file and loop playing it using SDL sound */
slouken@8582
    14
slouken@8582
    15
/* loopwaves.c is much more robust in handling WAVE files --
slouken@8582
    16
    This is only for simple WAVEs
slouken@8582
    17
*/
slouken@8582
    18
#include "SDL_config.h"
slouken@8582
    19
slouken@8582
    20
#include <stdio.h>
slouken@8582
    21
#include <stdlib.h>
slouken@8582
    22
slouken@8582
    23
#if HAVE_SIGNAL_H
slouken@8582
    24
#include <signal.h>
slouken@8582
    25
#endif
slouken@8582
    26
icculus@9278
    27
#ifdef __EMSCRIPTEN__
icculus@9278
    28
#include <emscripten/emscripten.h>
icculus@9278
    29
#endif
icculus@9278
    30
slouken@8582
    31
#include "SDL.h"
slouken@8582
    32
slouken@8582
    33
struct
slouken@8582
    34
{
slouken@8582
    35
    SDL_AudioSpec spec;
slouken@8582
    36
    Uint8 *sound;               /* Pointer to wave data */
slouken@8582
    37
    Uint32 soundlen;            /* Length of wave data */
slouken@8582
    38
    int soundpos;               /* Current play position */
slouken@8582
    39
} wave;
slouken@8582
    40
slouken@8582
    41
slouken@8582
    42
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    43
static void
slouken@8582
    44
quit(int rc)
slouken@8582
    45
{
slouken@8582
    46
    SDL_Quit();
slouken@8582
    47
    exit(rc);
slouken@8582
    48
}
slouken@8582
    49
slouken@8582
    50
slouken@8582
    51
void SDLCALL
slouken@8582
    52
fillerup(void *unused, Uint8 * stream, int len)
slouken@8582
    53
{
slouken@8582
    54
    Uint8 *waveptr;
slouken@8582
    55
    int waveleft;
slouken@8582
    56
slouken@8582
    57
    /* Set up the pointers */
slouken@8582
    58
    waveptr = wave.sound + wave.soundpos;
slouken@8582
    59
    waveleft = wave.soundlen - wave.soundpos;
slouken@8582
    60
slouken@8582
    61
    /* Go! */
slouken@8582
    62
    while (waveleft <= len) {
slouken@8582
    63
        SDL_memcpy(stream, waveptr, waveleft);
slouken@8582
    64
        stream += waveleft;
slouken@8582
    65
        len -= waveleft;
slouken@8582
    66
        waveptr = wave.sound;
slouken@8582
    67
        waveleft = wave.soundlen;
slouken@8582
    68
        wave.soundpos = 0;
slouken@8582
    69
    }
slouken@8582
    70
    SDL_memcpy(stream, waveptr, len);
slouken@8582
    71
    wave.soundpos += len;
slouken@8582
    72
}
slouken@8582
    73
slouken@8582
    74
static int done = 0;
slouken@8582
    75
void
slouken@8582
    76
poked(int sig)
slouken@8582
    77
{
slouken@8582
    78
    done = 1;
slouken@8582
    79
}
slouken@8582
    80
icculus@9280
    81
#ifdef __EMSCRIPTEN__
icculus@9278
    82
void
icculus@9278
    83
loop()
icculus@9278
    84
{
icculus@9278
    85
    if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
icculus@9278
    86
        emscripten_cancel_main_loop();
icculus@9278
    87
}
icculus@9280
    88
#endif
icculus@9278
    89
slouken@8582
    90
int
slouken@8582
    91
main(int argc, char *argv[])
slouken@8582
    92
{
slouken@8582
    93
    int i;
slouken@8582
    94
    char filename[4096];
slouken@8582
    95
philipp@9922
    96
    /* Enable standard application logging */
philipp@9922
    97
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    98
slouken@8582
    99
    /* Load the SDL library */
slouken@8582
   100
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@8582
   101
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
   102
        return (1);
slouken@8582
   103
    }
slouken@8582
   104
slouken@8836
   105
    if (argc > 1) {
slouken@8582
   106
        SDL_strlcpy(filename, argv[1], sizeof(filename));
slouken@8582
   107
    } else {
slouken@8582
   108
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
slouken@8582
   109
    }
slouken@8582
   110
    /* Load the wave file into memory */
slouken@8582
   111
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@8837
   112
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
slouken@8582
   113
        quit(1);
slouken@8582
   114
    }
slouken@8582
   115
slouken@8582
   116
    wave.spec.callback = fillerup;
slouken@8582
   117
#if HAVE_SIGNAL_H
slouken@8582
   118
    /* Set the signals */
slouken@8582
   119
#ifdef SIGHUP
slouken@8582
   120
    signal(SIGHUP, poked);
slouken@8582
   121
#endif
slouken@8582
   122
    signal(SIGINT, poked);
slouken@8582
   123
#ifdef SIGQUIT
slouken@8582
   124
    signal(SIGQUIT, poked);
slouken@8582
   125
#endif
slouken@8582
   126
    signal(SIGTERM, poked);
slouken@8582
   127
#endif /* HAVE_SIGNAL_H */
slouken@8582
   128
slouken@8582
   129
    /* Show the list of available drivers */
slouken@8582
   130
    SDL_Log("Available audio drivers:");
slouken@8582
   131
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
philipp@9922
   132
        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
slouken@8582
   133
    }
slouken@8582
   134
slouken@8582
   135
    /* Initialize fillerup() variables */
slouken@8582
   136
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@8582
   137
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
slouken@8582
   138
        SDL_FreeWAV(wave.sound);
slouken@8582
   139
        quit(2);
slouken@8582
   140
    }
slouken@8582
   141
slouken@8582
   142
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@8582
   143
slouken@8582
   144
    /* Let the audio run */
slouken@8582
   145
    SDL_PauseAudio(0);
icculus@9278
   146
icculus@9278
   147
#ifdef __EMSCRIPTEN__
icculus@9278
   148
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   149
#else
slouken@8582
   150
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@8582
   151
        SDL_Delay(1000);
icculus@9278
   152
#endif
slouken@8582
   153
slouken@8582
   154
    /* Clean up on signal */
slouken@8582
   155
    SDL_CloseAudio();
slouken@8582
   156
    SDL_FreeWAV(wave.sound);
slouken@8582
   157
    SDL_Quit();
slouken@8582
   158
    return (0);
slouken@8582
   159
}
slouken@8582
   160
slouken@8582
   161
/* vi: set ts=4 sw=4 expandtab: */