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