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