test/loopwave.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 8377 3a7e8110201e
child 8477 ad08f0d710f3
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
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@0
    15
/* loopwaves.c is much more robust in handling WAVE files -- 
slouken@0
    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;
dludwig@8354
    82
    char filename[4096];
slouken@6715
    83
slouken@1895
    84
    /* Load the SDL library */
slouken@1895
    85
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@1895
    86
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    87
        return (1);
slouken@1895
    88
    }
icculus@2049
    89
dludwig@8354
    90
    if (argc >= 1) {
dludwig@8354
    91
        SDL_strlcpy(filename, argv[1], sizeof(filename));
dludwig@8354
    92
    } else {
dludwig@8354
    93
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
slouken@1895
    94
    }
slouken@1895
    95
    /* Load the wave file into memory */
dludwig@8354
    96
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
dludwig@8354
    97
        fprintf(stderr, "Couldn't load %s: %s\n", filename, 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 */
slouken@6715
   115
    printf("Available audio drivers: ");
slouken@6715
   116
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
slouken@6715
   117
        if (i == 0) {
slouken@6715
   118
            printf("%s", SDL_GetAudioDriver(i));
slouken@6715
   119
        } else {
slouken@6715
   120
            printf(", %s", SDL_GetAudioDriver(i));
slouken@6715
   121
        }
slouken@6715
   122
    }
slouken@6715
   123
    printf("\n");
slouken@6715
   124
slouken@1895
   125
    /* Initialize fillerup() variables */
slouken@1895
   126
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
slouken@1895
   127
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@1895
   128
        SDL_FreeWAV(wave.sound);
slouken@1895
   129
        quit(2);
slouken@1895
   130
    }
icculus@3359
   131
icculus@3360
   132
    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
slouken@0
   133
slouken@1895
   134
    /* Let the audio run */
icculus@3359
   135
    SDL_PauseAudio(0);
slouken@1895
   136
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
slouken@1895
   137
        SDL_Delay(1000);
slouken@0
   138
slouken@1895
   139
    /* Clean up on signal */
slouken@1895
   140
    SDL_CloseAudio();
slouken@1895
   141
    SDL_FreeWAV(wave.sound);
slouken@1895
   142
    SDL_Quit();
slouken@1895
   143
    return (0);
slouken@0
   144
}
slouken@6715
   145
slouken@6715
   146
/* vi: set ts=4 sw=4 expandtab: */