cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

loopwavequeue.c (3515B)


      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 queueing */
     14
     15#include <stdio.h>
     16#include <stdlib.h>
     17
     18#include "SDL.h"
     19
     20#if HAVE_SIGNAL_H
     21#include <signal.h>
     22#endif
     23
     24struct
     25{
     26    SDL_AudioSpec spec;
     27    Uint8 *sound;               /* Pointer to wave data */
     28    Uint32 soundlen;            /* Length of wave data */
     29    int soundpos;               /* Current play position */
     30} wave;
     31
     32
     33/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     34static void
     35quit(int rc)
     36{
     37    SDL_Quit();
     38    exit(rc);
     39}
     40
     41static int done = 0;
     42void
     43poked(int sig)
     44{
     45    done = 1;
     46}
     47
     48int
     49main(int argc, char *argv[])
     50{
     51    int i;
     52    char filename[4096];
     53
     54	/* Enable standard application logging */
     55	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
     56
     57    /* Load the SDL library */
     58    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
     59        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
     60        return (1);
     61    }
     62
     63    if (argc > 1) {
     64        SDL_strlcpy(filename, argv[1], sizeof(filename));
     65    } else {
     66        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
     67    }
     68    /* Load the wave file into memory */
     69    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
     70        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
     71        quit(1);
     72    }
     73
     74    wave.spec.callback = NULL;  /* we'll push audio. */
     75
     76#if HAVE_SIGNAL_H
     77    /* Set the signals */
     78#ifdef SIGHUP
     79    signal(SIGHUP, poked);
     80#endif
     81    signal(SIGINT, poked);
     82#ifdef SIGQUIT
     83    signal(SIGQUIT, poked);
     84#endif
     85    signal(SIGTERM, poked);
     86#endif /* HAVE_SIGNAL_H */
     87
     88    /* Initialize fillerup() variables */
     89    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
     90        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
     91        SDL_FreeWAV(wave.sound);
     92        quit(2);
     93    }
     94
     95    /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
     96
     97    /* Let the audio run */
     98    SDL_PauseAudio(0);
     99
    100    /* Note that we stuff the entire audio buffer into the queue in one
    101       shot. Most apps would want to feed it a little at a time, as it
    102       plays, but we're going for simplicity here. */
    103    
    104    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
    105    {
    106        /* The device from SDL_OpenAudio() is always device #1. */
    107        const Uint32 queued = SDL_GetQueuedAudioSize(1);
    108        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
    109        if (queued <= 8192) {  /* time to requeue the whole thing? */
    110            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
    111                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
    112            } else {
    113                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
    114            }
    115        }
    116
    117        SDL_Delay(100);  /* let it play for awhile. */
    118    }
    119
    120    /* Clean up on signal */
    121    SDL_CloseAudio();
    122    SDL_FreeWAV(wave.sound);
    123    SDL_Quit();
    124    return 0;
    125}
    126
    127/* vi: set ts=4 sw=4 expandtab: */