test/loopwave.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 8837 3fa15ed506f3
child 9280 f33128b58c5d
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jylänki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
slouken@8582
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
#include "SDL_audio.h"
slouken@8582
    33
slouken@8582
    34
struct
slouken@8582
    35
{
slouken@8582
    36
    SDL_AudioSpec spec;
slouken@8582
    37
    Uint8 *sound;               /* Pointer to wave data */
slouken@8582
    38
    Uint32 soundlen;            /* Length of wave data */
slouken@8582
    39
    int soundpos;               /* Current play position */
slouken@8582
    40
} wave;
slouken@8582
    41
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@8582
    51
slouken@8582
    52
void SDLCALL
slouken@8582
    53
fillerup(void *unused, Uint8 * stream, int len)
slouken@8582
    54
{
slouken@8582
    55
    Uint8 *waveptr;
slouken@8582
    56
    int waveleft;
slouken@8582
    57
slouken@8582
    58
    /* Set up the pointers */
slouken@8582
    59
    waveptr = wave.sound + wave.soundpos;
slouken@8582
    60
    waveleft = wave.soundlen - wave.soundpos;
slouken@8582
    61
slouken@8582
    62
    /* Go! */
slouken@8582
    63
    while (waveleft <= len) {
slouken@8582
    64
        SDL_memcpy(stream, waveptr, waveleft);
slouken@8582
    65
        stream += waveleft;
slouken@8582
    66
        len -= waveleft;
slouken@8582
    67
        waveptr = wave.sound;
slouken@8582
    68
        waveleft = wave.soundlen;
slouken@8582
    69
        wave.soundpos = 0;
slouken@8582
    70
    }
slouken@8582
    71
    SDL_memcpy(stream, waveptr, len);
slouken@8582
    72
    wave.soundpos += len;
slouken@8582
    73
}
slouken@8582
    74
slouken@8582
    75
static int done = 0;
slouken@8582
    76
void
slouken@8582
    77
poked(int sig)
slouken@8582
    78
{
slouken@8582
    79
    done = 1;
slouken@8582
    80
}
slouken@8582
    81
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@9278
    88
slouken@8582
    89
int
slouken@8582
    90
main(int argc, char *argv[])
slouken@8582
    91
{
slouken@8582
    92
    int i;
slouken@8582
    93
    char filename[4096];
slouken@8582
    94
slouken@8582
    95
	/* Enable standard application logging */
slouken@8582
    96
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    97
slouken@8582
    98
    /* Load the SDL library */
slouken@8582
    99
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@8582
   100
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
   101
        return (1);
slouken@8582
   102
    }
slouken@8582
   103
slouken@8836
   104
    if (argc > 1) {
slouken@8582
   105
        SDL_strlcpy(filename, argv[1], sizeof(filename));
slouken@8582
   106
    } else {
slouken@8582
   107
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
slouken@8582
   108
    }
slouken@8582
   109
    /* Load the wave file into memory */
slouken@8582
   110
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@8837
   111
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
slouken@8582
   112
        quit(1);
slouken@8582
   113
    }
slouken@8582
   114
slouken@8582
   115
    wave.spec.callback = fillerup;
slouken@8582
   116
#if HAVE_SIGNAL_H
slouken@8582
   117
    /* Set the signals */
slouken@8582
   118
#ifdef SIGHUP
slouken@8582
   119
    signal(SIGHUP, poked);
slouken@8582
   120
#endif
slouken@8582
   121
    signal(SIGINT, poked);
slouken@8582
   122
#ifdef SIGQUIT
slouken@8582
   123
    signal(SIGQUIT, poked);
slouken@8582
   124
#endif
slouken@8582
   125
    signal(SIGTERM, poked);
slouken@8582
   126
#endif /* HAVE_SIGNAL_H */
slouken@8582
   127
slouken@8582
   128
    /* Show the list of available drivers */
slouken@8582
   129
    SDL_Log("Available audio drivers:");
slouken@8582
   130
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
slouken@8582
   131
		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
slouken@8582
   132
    }
slouken@8582
   133
slouken@8582
   134
    /* Initialize fillerup() variables */
slouken@8582
   135
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@8582
   136
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
slouken@8582
   137
        SDL_FreeWAV(wave.sound);
slouken@8582
   138
        quit(2);
slouken@8582
   139
    }
slouken@8582
   140
slouken@8582
   141
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@8582
   142
slouken@8582
   143
    /* Let the audio run */
slouken@8582
   144
    SDL_PauseAudio(0);
icculus@9278
   145
icculus@9278
   146
#ifdef __EMSCRIPTEN__
icculus@9278
   147
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   148
#else
slouken@8582
   149
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@8582
   150
        SDL_Delay(1000);
icculus@9278
   151
#endif
slouken@8582
   152
slouken@8582
   153
    /* Clean up on signal */
slouken@8582
   154
    SDL_CloseAudio();
slouken@8582
   155
    SDL_FreeWAV(wave.sound);
slouken@8582
   156
    SDL_Quit();
slouken@8582
   157
    return (0);
slouken@8582
   158
}
slouken@8582
   159
slouken@8582
   160
/* vi: set ts=4 sw=4 expandtab: */