Mercurial > hg > forks > pwpunix
view pwplib/snd-sdl.c @ 43:093315d84a22
Add a small delay in SDL audio pseudo-loopflush function to avoid tightlooping in prod main loops (explained better in the added comment.)
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 24 May 2010 05:07:33 +0300 |
parents | b9d679965320 |
children | 85671798fdb3 |
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 <stdio.h> #include <stdlib.h> #include <unistd.h> #include <SDL.h> #include "pwplib.h" #include "gb.h" #define TIMERHZ 72 static int pwp_sdlaudio_run = 1; static int pwp_sdlaudio_frag = -1, pwp_sdlaudio_curr; static void pwp_sdlaudio_fill(void * udata, Uint8 * buf, int len) { (void) udata; while (len > 0) { if (pwp_sdlaudio_curr >= len) { pwp_sdlaudio_curr -= len; gb_genwave(buf, len); len = 0; } else { gb_genwave(buf, pwp_sdlaudio_curr); buf += pwp_sdlaudio_curr; len -= pwp_sdlaudio_curr; pwp_sdlaudio_curr = pwp_sdlaudio_frag; pwplib.player(); } } } static void pwp_sdlaudio_start(void) { if (pwp_sdlaudio_run) { pwp_sdlaudio_curr = pwp_sdlaudio_frag; pwp_sdlaudio_run = 0; pwplib.player(); SDL_PauseAudio(0); } /* 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_sdlaudio_init() { SDL_AudioSpec fmt; pwp_sdlaudio_run = 1; fmt.freq = 44100; fmt.format = AUDIO_U8; fmt.channels = 1; fmt.samples = 512; fmt.callback = pwp_sdlaudio_fill; if (SDL_OpenAudio(&fmt, NULL) < 0) { pwpwrite("* SDL: Could not get desired audio format.\n"); return 0; } pwp_sdlaudio_frag = fmt.freq / TIMERHZ; pwpwrite("* SDL sound (freq=%d, fmt=%d, chn=%d, buf=%d, frag=%d)\n", fmt.freq, fmt.format, fmt.channels, fmt.samples, pwp_sdlaudio_frag); pwplib.sound = gb_sound; pwplib.loopflush = pwp_sdlaudio_start; gb_init(fmt.freq); return 1; } #endif