test/loopwave.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 7639 9406b7dd2f2d
child 8583 fb2933ca805f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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