view pwplib/snd-sdl.c @ 71:a87eb778f225

Improvements to the MinGW crossbuild. Should now build with default tools from Debian mingw packages, though you need Win32 version of libSDL with the necessary headers and so on in addition. 64-bit builds not tested and probably won't work. Tested on Debian 7.0, earlier won't work. binutils-mingw-w64-i686 gcc-mingw-w64-i686 mingw-w64-i686-dev
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 14 Aug 2012 03:08:10 +0300
parents 85671798fdb3
children
line wrap: on
line source

/*
 * pwplib SDL (Simple Directmedia Layer) audio "driver"
 * (C) Copyright 2010 ccr/TNSP^PWP <ccr@tnsp.org>
 *
 * This file and other changes are distributed under same
 * license as pwplib itself.
 */

#include "config.h"

#ifdef DRIVE_SDL

#include "sound.h"
#include <SDL.h>

static int pwp_SDL_audio_run = 1;
static int pwp_SDL_audio_frag = -1, pwp_SDL_audio_curr;


static void pwp_SDL_audio_fill(void * udata, Uint8 * buf, int len)
{
    (void) udata;
    while (len > 0) {
        if (pwp_SDL_audio_curr >= len) {
            pwp_SDL_audio_curr -= len;
            gb_genwave(buf, len);
            len = 0;
        } else {
            gb_genwave(buf, pwp_SDL_audio_curr);
            buf += pwp_SDL_audio_curr;
            len -= pwp_SDL_audio_curr;
            pwp_SDL_audio_curr = pwp_SDL_audio_frag;
            pwplib.player();
        }
    }

    pwp_SDL_audio_run = 0;
}

static void pwp_SDL_audio_start(void)
{
    if (pwp_SDL_audio_run)
        SDL_PauseAudio(0);

    while (pwp_SDL_audio_run)
        SDL_Delay(10);
    
    /* NOTICE! A small delay is used here, because the prods using pwplib
     * run in a tight loop (they expect that audio rendering blocks,
     * which it doesn't in case of SDL.) This way we avoid consuming
     * excessive amounts of CPU.
     *
     * -- ccr
     */
    SDL_Delay(10);
}

int pwp_SDL_audio_init(void)
{
    SDL_AudioSpec fmt;

    pwp_SDL_audio_run = 1;

    fmt.freq = 44100;
    fmt.format = AUDIO_U8;
    fmt.channels = 1;
    fmt.samples = 512;
    fmt.callback = pwp_SDL_audio_fill;

    if (SDL_OpenAudio(&fmt, NULL) < 0)
    {
        pwpwrite("* SDL: Could not get desired audio format.\n");
        return 0;
    }

    pwp_SDL_audio_frag = fmt.freq / TIMERHZ;
    pwp_SDL_audio_curr = pwp_SDL_audio_frag;
    pwp_SDL_audio_run = 1;

    pwpwrite("* SDL sound (freq=%d, fmt=%d, chn=%d, buf=%d, frag=%d)\n",
        fmt.freq, fmt.format, fmt.channels, fmt.samples, pwp_SDL_audio_frag);

    pwplib.sound = gb_sound;
    pwplib.loopflush = pwp_SDL_audio_start;
    gb_init(fmt.freq);

    return 1;
}
#endif