From 0dee26397c40abe329e518f61ab3369e18775922 Mon Sep 17 00:00:00 2001 From: Adam Olsen Date: Fri, 27 Apr 2001 07:41:47 +0000 Subject: [PATCH] Make sdl audio work. And despite claims, it's not actually bad (except for esd, but that's not sdl's fault). I havn't tested it with other hardware or different config variables, and I've experienced the occasional hang or segfault on exit, so feedback is very welcome. :) --- configure.in | 3 ++- libs/audio/targets/Makefile.am | 2 +- libs/audio/targets/snd_sdl.c | 40 +++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/configure.in b/configure.in index 1c72224c5..57970f0d3 100644 --- a/configure.in +++ b/configure.in @@ -756,6 +756,7 @@ AC_ARG_ENABLE(sdl-audio, if test -z "$SNDTYPE" -a "x$HAVE_SDL" = "xyes"; then SNDTYPE="SDL" SOUND_LIBS="$SDL_LIBS" + SOUND_CFLAGS="$SDL_CFLAGS" fi ) @@ -1188,7 +1189,7 @@ if test "x$optimize" = xyes; then fi else AC_MSG_RESULT(no) - CFLAGS="$CFLAGS -O" + CFLAGS="$CFLAGS " fi AC_MSG_CHECKING(for packet logging) diff --git a/libs/audio/targets/Makefile.am b/libs/audio/targets/Makefile.am index 7033163f2..c2aedfb52 100644 --- a/libs/audio/targets/Makefile.am +++ b/libs/audio/targets/Makefile.am @@ -1,4 +1,4 @@ -INCLUDES= -I$(top_srcdir)/include +INCLUDES= -I$(top_srcdir)/include $(SOUND_CFLAGS) lib_LTLIBRARIES = @SND_TARGETS@ EXTRA_LTLIBRARIES = libQFsound.la diff --git a/libs/audio/targets/snd_sdl.c b/libs/audio/targets/snd_sdl.c index dbad4178e..7b51c4878 100644 --- a/libs/audio/targets/snd_sdl.c +++ b/libs/audio/targets/snd_sdl.c @@ -32,11 +32,13 @@ #include #include +#include #include "QF/cmd.h" #include "QF/console.h" #include "QF/qargs.h" #include "QF/sound.h" +#include "QF/sys.h" static dma_t the_shm; static int snd_inited; @@ -47,11 +49,27 @@ extern int desired_bits; static void paint_audio (void *unused, Uint8 * stream, int len) { + int streamsamples; + int sampleposbytes; + int samplesbytes; + if (shm) { - shm->buffer = stream; - shm->samplepos += len / (shm->samplebits / 8); - // Check for samplepos overflow? - S_PaintChannels (shm->samplepos); + streamsamples = len / (shm->samplebits / 8); + sampleposbytes = shm->samplepos * (shm->samplebits / 8); + samplesbytes = shm->samples * (shm->samplebits / 8); + + shm->samplepos += streamsamples; + if (shm->samplepos >= shm->samples) + shm->samplepos -= shm->samples; + S_PaintChannels (soundtime + streamsamples); + + if (shm->samplepos + streamsamples <= shm->samples) + memcpy (stream, shm->buffer + sampleposbytes, len); + else { + memcpy (stream, shm->buffer + sampleposbytes, samplesbytes - sampleposbytes); + memcpy (stream + samplesbytes - sampleposbytes, shm->buffer, len - (samplesbytes - sampleposbytes)); + } + soundtime += streamsamples; } } @@ -113,6 +131,7 @@ SNDDMA_Init (void) memcpy (&obtained, &desired, sizeof (desired)); break; } + SDL_LockAudio(); SDL_PauseAudio (0); /* Fill the audio DMA information block */ @@ -121,10 +140,14 @@ SNDDMA_Init (void) shm->samplebits = (obtained.format & 0xFF); shm->speed = obtained.freq; shm->channels = obtained.channels; - shm->samples = obtained.samples * shm->channels; + shm->samples = obtained.samples * 4; shm->samplepos = 0; shm->submission_chunk = 1; - shm->buffer = NULL; + shm->buffer = calloc(shm->samples * (shm->samplebits / 8), 1); + if (!shm->buffer) + { + Sys_Error ("Failed to allocate buffer for sound!\n"); + } snd_inited = 1; return 1; @@ -140,8 +163,11 @@ void SNDDMA_Shutdown (void) { if (snd_inited) { + SDL_PauseAudio (1); + SDL_UnlockAudio (); SDL_CloseAudio (); snd_inited = 0; + shm = NULL; } } @@ -155,4 +181,6 @@ SNDDMA_Shutdown (void) void SNDDMA_Submit (void) { + SDL_UnlockAudio(); + SDL_LockAudio(); }