test/loopwave.c
author Alex Baines <alex@abaines.me.uk>
Tue, 19 Aug 2014 23:31:50 +0100
changeset 9097 56d712662a82
parent 8837 3fa15ed506f3
child 9278 8900afb78a19
permissions -rw-r--r--
Add a SDL_IM_INTERNAL_EDITING event to make IMs like iBus render editing text in its own UI instead of sending TEXTEDITING events.
This is useful for applications that handle TEXTINPUT events but not TEXTEDITING events.
     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 #include "SDL.h"
    28 #include "SDL_audio.h"
    29 
    30 struct
    31 {
    32     SDL_AudioSpec spec;
    33     Uint8 *sound;               /* Pointer to wave data */
    34     Uint32 soundlen;            /* Length of wave data */
    35     int soundpos;               /* Current play position */
    36 } wave;
    37 
    38 
    39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    40 static void
    41 quit(int rc)
    42 {
    43     SDL_Quit();
    44     exit(rc);
    45 }
    46 
    47 
    48 void SDLCALL
    49 fillerup(void *unused, Uint8 * stream, int len)
    50 {
    51     Uint8 *waveptr;
    52     int waveleft;
    53 
    54     /* Set up the pointers */
    55     waveptr = wave.sound + wave.soundpos;
    56     waveleft = wave.soundlen - wave.soundpos;
    57 
    58     /* Go! */
    59     while (waveleft <= len) {
    60         SDL_memcpy(stream, waveptr, waveleft);
    61         stream += waveleft;
    62         len -= waveleft;
    63         waveptr = wave.sound;
    64         waveleft = wave.soundlen;
    65         wave.soundpos = 0;
    66     }
    67     SDL_memcpy(stream, waveptr, len);
    68     wave.soundpos += len;
    69 }
    70 
    71 static int done = 0;
    72 void
    73 poked(int sig)
    74 {
    75     done = 1;
    76 }
    77 
    78 int
    79 main(int argc, char *argv[])
    80 {
    81     int i;
    82     char filename[4096];
    83 
    84 	/* Enable standard application logging */
    85 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    86 
    87     /* Load the SDL library */
    88     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    89         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    90         return (1);
    91     }
    92 
    93     if (argc > 1) {
    94         SDL_strlcpy(filename, argv[1], sizeof(filename));
    95     } else {
    96         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
    97     }
    98     /* Load the wave file into memory */
    99     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
   100         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
   101         quit(1);
   102     }
   103 
   104     wave.spec.callback = fillerup;
   105 #if HAVE_SIGNAL_H
   106     /* Set the signals */
   107 #ifdef SIGHUP
   108     signal(SIGHUP, poked);
   109 #endif
   110     signal(SIGINT, poked);
   111 #ifdef SIGQUIT
   112     signal(SIGQUIT, poked);
   113 #endif
   114     signal(SIGTERM, poked);
   115 #endif /* HAVE_SIGNAL_H */
   116 
   117     /* Show the list of available drivers */
   118     SDL_Log("Available audio drivers:");
   119     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
   120 		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
   121     }
   122 
   123     /* Initialize fillerup() variables */
   124     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
   125         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
   126         SDL_FreeWAV(wave.sound);
   127         quit(2);
   128     }
   129 
   130     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   131 
   132     /* Let the audio run */
   133     SDL_PauseAudio(0);
   134     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
   135         SDL_Delay(1000);
   136 
   137     /* Clean up on signal */
   138     SDL_CloseAudio();
   139     SDL_FreeWAV(wave.sound);
   140     SDL_Quit();
   141     return (0);
   142 }
   143 
   144 /* vi: set ts=4 sw=4 expandtab: */