test/loopwave.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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
slouken@1895
    83
    /* Load the SDL library */
slouken@1895
    84
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@1895
    85
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    86
        return (1);
slouken@1895
    87
    }
icculus@2049
    88
slouken@1895
    89
    if (argv[1] == NULL) {
slouken@1895
    90
        argv[1] = "sample.wav";
slouken@1895
    91
    }
slouken@1895
    92
    /* Load the wave file into memory */
slouken@1895
    93
    if (SDL_LoadWAV(argv[1], &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
slouken@1895
    94
        fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
slouken@1895
    95
        quit(1);
slouken@1895
    96
    }
slouken@1895
    97
slouken@1895
    98
    wave.spec.callback = fillerup;
slouken@1463
    99
#if HAVE_SIGNAL_H
slouken@1895
   100
    /* Set the signals */
slouken@0
   101
#ifdef SIGHUP
slouken@1895
   102
    signal(SIGHUP, poked);
slouken@0
   103
#endif
slouken@1895
   104
    signal(SIGINT, poked);
slouken@0
   105
#ifdef SIGQUIT
slouken@1895
   106
    signal(SIGQUIT, poked);
slouken@0
   107
#endif
slouken@1895
   108
    signal(SIGTERM, poked);
slouken@1463
   109
#endif /* HAVE_SIGNAL_H */
slouken@0
   110
slouken@6715
   111
    /* Show the list of available drivers */
slouken@6715
   112
    printf("Available audio drivers: ");
slouken@6715
   113
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
slouken@6715
   114
        if (i == 0) {
slouken@6715
   115
            printf("%s", SDL_GetAudioDriver(i));
slouken@6715
   116
        } else {
slouken@6715
   117
            printf(", %s", SDL_GetAudioDriver(i));
slouken@6715
   118
        }
slouken@6715
   119
    }
slouken@6715
   120
    printf("\n");
slouken@6715
   121
slouken@1895
   122
    /* Initialize fillerup() variables */
slouken@1895
   123
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@1895
   124
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1895
   125
        SDL_FreeWAV(wave.sound);
slouken@1895
   126
        quit(2);
slouken@1895
   127
    }
icculus@3359
   128
icculus@3360
   129
    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@0
   130
slouken@1895
   131
    /* Let the audio run */
icculus@3359
   132
    SDL_PauseAudio(0);
slouken@1895
   133
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@1895
   134
        SDL_Delay(1000);
slouken@0
   135
slouken@1895
   136
    /* Clean up on signal */
slouken@1895
   137
    SDL_CloseAudio();
slouken@1895
   138
    SDL_FreeWAV(wave.sound);
slouken@1895
   139
    SDL_Quit();
slouken@1895
   140
    return (0);
slouken@0
   141
}
slouken@6715
   142
slouken@6715
   143
/* vi: set ts=4 sw=4 expandtab: */