test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2017 15:12:19 -0800
changeset 10933 a9ce2bbd76e1
parent 10932 98b94b9e3e8c
child 11020 3b993dbe4cf9
permissions -rw-r--r--
Fixed tabs to spaces
slouken@8582
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
philipp@9945
    13
/* Program to load a wave file and loop playing it using SDL audio */
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
philipp@10902
    33
static 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@10932
    41
static SDL_AudioDeviceID device;
slouken@8582
    42
slouken@8582
    43
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    44
static void
slouken@8582
    45
quit(int rc)
slouken@8582
    46
{
slouken@8582
    47
    SDL_Quit();
slouken@8582
    48
    exit(rc);
slouken@8582
    49
}
slouken@8582
    50
slouken@10932
    51
static void
slouken@10932
    52
close_audio()
slouken@10932
    53
{
slouken@10933
    54
    if (device != 0) {
slouken@10933
    55
        SDL_CloseAudioDevice(device);
slouken@10933
    56
        device = 0;
slouken@10933
    57
    }
slouken@10932
    58
}
slouken@10932
    59
slouken@10932
    60
static void
slouken@10932
    61
open_audio()
slouken@10932
    62
{
slouken@10933
    63
    /* Initialize fillerup() variables */
slouken@10933
    64
    device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
slouken@10933
    65
    if (!device) {
slouken@10933
    66
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
slouken@10933
    67
        SDL_FreeWAV(wave.sound);
slouken@10933
    68
        quit(2);
slouken@10933
    69
    }
slouken@10932
    70
slouken@10932
    71
slouken@10933
    72
    /* Let the audio run */
slouken@10933
    73
    SDL_PauseAudioDevice(device, SDL_FALSE);
slouken@10932
    74
}
slouken@10932
    75
slouken@10932
    76
static void reopen_audio()
slouken@10932
    77
{
slouken@10933
    78
    close_audio();
slouken@10933
    79
    open_audio();
slouken@10932
    80
}
slouken@10932
    81
slouken@8582
    82
slouken@8582
    83
void SDLCALL
slouken@8582
    84
fillerup(void *unused, Uint8 * stream, int len)
slouken@8582
    85
{
slouken@8582
    86
    Uint8 *waveptr;
slouken@8582
    87
    int waveleft;
slouken@8582
    88
slouken@8582
    89
    /* Set up the pointers */
slouken@8582
    90
    waveptr = wave.sound + wave.soundpos;
slouken@8582
    91
    waveleft = wave.soundlen - wave.soundpos;
slouken@8582
    92
slouken@8582
    93
    /* Go! */
slouken@8582
    94
    while (waveleft <= len) {
slouken@8582
    95
        SDL_memcpy(stream, waveptr, waveleft);
slouken@8582
    96
        stream += waveleft;
slouken@8582
    97
        len -= waveleft;
slouken@8582
    98
        waveptr = wave.sound;
slouken@8582
    99
        waveleft = wave.soundlen;
slouken@8582
   100
        wave.soundpos = 0;
slouken@8582
   101
    }
slouken@8582
   102
    SDL_memcpy(stream, waveptr, len);
slouken@8582
   103
    wave.soundpos += len;
slouken@8582
   104
}
slouken@8582
   105
slouken@8582
   106
static int done = 0;
slouken@8582
   107
void
slouken@8582
   108
poked(int sig)
slouken@8582
   109
{
slouken@8582
   110
    done = 1;
slouken@8582
   111
}
slouken@8582
   112
icculus@9280
   113
#ifdef __EMSCRIPTEN__
icculus@9278
   114
void
icculus@9278
   115
loop()
icculus@9278
   116
{
slouken@10932
   117
    if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
icculus@9278
   118
        emscripten_cancel_main_loop();
icculus@9278
   119
}
icculus@9280
   120
#endif
icculus@9278
   121
slouken@8582
   122
int
slouken@8582
   123
main(int argc, char *argv[])
slouken@8582
   124
{
slouken@8582
   125
    int i;
slouken@8582
   126
    char filename[4096];
slouken@8582
   127
philipp@9922
   128
    /* Enable standard application logging */
philipp@9922
   129
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
   130
slouken@8582
   131
    /* Load the SDL library */
slouken@10932
   132
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
slouken@8582
   133
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
   134
        return (1);
slouken@8582
   135
    }
slouken@8582
   136
slouken@8836
   137
    if (argc > 1) {
slouken@8582
   138
        SDL_strlcpy(filename, argv[1], sizeof(filename));
slouken@8582
   139
    } else {
slouken@8582
   140
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
slouken@8582
   141
    }
slouken@8582
   142
    /* Load the wave file into memory */
slouken@8582
   143
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@8837
   144
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
slouken@8582
   145
        quit(1);
slouken@8582
   146
    }
slouken@8582
   147
slouken@8582
   148
    wave.spec.callback = fillerup;
slouken@8582
   149
slouken@8582
   150
    /* Show the list of available drivers */
slouken@8582
   151
    SDL_Log("Available audio drivers:");
slouken@8582
   152
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
philipp@9922
   153
        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
slouken@10933
   154
    }
slouken@8582
   155
slouken@10933
   156
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@8582
   157
slouken@10933
   158
    open_audio();
slouken@8582
   159
slouken@10933
   160
    SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
icculus@9278
   161
icculus@9278
   162
#ifdef __EMSCRIPTEN__
icculus@9278
   163
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   164
#else
slouken@10933
   165
    while (!done) {
slouken@10933
   166
        SDL_Event event;
slouken@10932
   167
slouken@10933
   168
        while (SDL_PollEvent(&event) > 0) {
slouken@10933
   169
            if (event.type == SDL_QUIT) {
slouken@10933
   170
                done = 1;
slouken@10933
   171
            }
slouken@10933
   172
            if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
slouken@10933
   173
                (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
slouken@10933
   174
                reopen_audio();
slouken@10933
   175
            }
slouken@10933
   176
        }
slouken@10933
   177
        SDL_Delay(100);
slouken@10933
   178
    }
icculus@9278
   179
#endif
slouken@8582
   180
slouken@8582
   181
    /* Clean up on signal */
slouken@10933
   182
    close_audio();
slouken@8582
   183
    SDL_FreeWAV(wave.sound);
slouken@8582
   184
    SDL_Quit();
slouken@8582
   185
    return (0);
slouken@8582
   186
}
slouken@8582
   187
slouken@8582
   188
/* vi: set ts=4 sw=4 expandtab: */