Mercurial > hg > forks > pwpunix
view pwplib/snd-sdl.c @ 10:a379d6a2a717
Take into account that SDL does not guarantee audio buffer size to be what
we request it to be, so implement fragment / tick handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 18 May 2010 17:58:37 +0300 |
parents | e2f028bf775a |
children | e3b0773ba1be |
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" #define TIMERHZ 30 static int pwp_sdlaudio_run = 1; static int pwp_sdlaudio_frag = -1, pwp_sdlaudio_curr; extern void gb_sound(int, int, int, int); static void pwp_sdlaudio_fill(void *udata, Uint8 * buf, int len) { while (len > 0) { if (pwp_sdlaudio_curr > len) { pwp_sdlaudio_curr -= len; gb_genwave(buf, len); len = 0; pwplib.player(); } 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) { fprintf(stderr, "\nsound starting\n"); pwp_sdlaudio_curr = pwp_sdlaudio_frag; pwp_sdlaudio_run = 0; SDL_PauseAudio(0); } } static void pwp_sdlaudio_close(void) { SDL_CloseAudio(); } int pwp_sdlaudio_init() { SDL_AudioSpec fmt; pwp_sdlaudio_run = 1; fmt.freq = 44100; fmt.format = AUDIO_U8; fmt.channels = 1; fmt.samples = 2048; fmt.callback = pwp_sdlaudio_fill; pwp_sdlaudio_frag = fmt.freq / TIMERHZ; if (SDL_OpenAudio(&fmt, NULL) < 0) { pwpwrite("* SDL: Could not get desired audio format.\n"); return 0; } pwpwrite("* SDL sound\n"); pwplib.sound = gb_sound; pwplib.loopflush = pwp_sdlaudio_start; gb_init(fmt.freq); // pwp_regdestr(pwp_sdlaudio_close); return 1; } #endif