test/loopwave.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Tue, 10 Feb 2015 20:40:03 +0100
changeset 9338 86bc1c9a090f
parent 9280 f33128b58c5d
child 9356 e87d6e1e812a
permissions -rw-r--r--
Fixed bug 2873 - Joystick test won't reload after reattach

lectem

-plug in the joystick
-start testjoystick 0
-unplug
-replug

The joystick is detected but doesn't enter the loop anymore since done==SDL_TRUE
     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 #ifdef __EMSCRIPTEN__
    83 void
    84 loop()
    85 {
    86     if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
    87         emscripten_cancel_main_loop();
    88 }
    89 #endif
    90 
    91 int
    92 main(int argc, char *argv[])
    93 {
    94     int i;
    95     char filename[4096];
    96 
    97 	/* Enable standard application logging */
    98 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    99 
   100     /* Load the SDL library */
   101     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   102         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   103         return (1);
   104     }
   105 
   106     if (argc > 1) {
   107         SDL_strlcpy(filename, argv[1], sizeof(filename));
   108     } else {
   109         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
   110     }
   111     /* Load the wave file into memory */
   112     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   113         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
   114         quit(1);
   115     }
   116 
   117     wave.spec.callback = fillerup;
   118 #if HAVE_SIGNAL_H
   119     /* Set the signals */
   120 #ifdef SIGHUP
   121     signal(SIGHUP, poked);
   122 #endif
   123     signal(SIGINT, poked);
   124 #ifdef SIGQUIT
   125     signal(SIGQUIT, poked);
   126 #endif
   127     signal(SIGTERM, poked);
   128 #endif /* HAVE_SIGNAL_H */
   129 
   130     /* Show the list of available drivers */
   131     SDL_Log("Available audio drivers:");
   132     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   133 		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   134     }
   135 
   136     /* Initialize fillerup() variables */
   137     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   138         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   139         SDL_FreeWAV(wave.sound);
   140         quit(2);
   141     }
   142 
   143     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   144 
   145     /* Let the audio run */
   146     SDL_PauseAudio(0);
   147 
   148 #ifdef __EMSCRIPTEN__
   149     emscripten_set_main_loop(loop, 0, 1);
   150 #else
   151     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   152         SDL_Delay(1000);
   153 #endif
   154 
   155     /* Clean up on signal */
   156     SDL_CloseAudio();
   157     SDL_FreeWAV(wave.sound);
   158     SDL_Quit();
   159     return (0);
   160 }
   161 
   162 /* vi: set ts=4 sw=4 expandtab: */