From 2ad43d300d4b56da3a012cb0fd4470f9314af464 Mon Sep 17 00:00:00 2001 From: molivier Date: Sun, 25 Jun 2006 17:00:07 +0000 Subject: [PATCH] Decreased the OSS buffer size, and cleaned some code git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@6485 d7cf8633-e32d-0410-b094-e92efae38249 --- snd_alsa.c | 15 +++++------ snd_mix.c | 1 - snd_oss.c | 77 ++++++++++++++++++++++++++++++------------------------ 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/snd_alsa.c b/snd_alsa.c index 23d66bd9..173a9f52 100644 --- a/snd_alsa.c +++ b/snd_alsa.c @@ -295,6 +295,11 @@ static snd_pcm_sframes_t SndSys_Write (const unsigned char* buffer, unsigned int written, snd_strerror (written)); } } + if (written > 0) + { + snd_renderbuffer->startframe += written; + expected_delay += written; + } return written; } @@ -325,12 +330,7 @@ void SndSys_Submit (void) if (nbframes > limit) { written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit); - if (written < 0) - return; - snd_renderbuffer->startframe += written; - expected_delay += written; - - if ((snd_pcm_uframes_t)written != limit) + if (written < 0 || (snd_pcm_uframes_t)written != limit) return; nbframes -= limit; @@ -340,9 +340,6 @@ void SndSys_Submit (void) written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], nbframes); if (written < 0) return; - - snd_renderbuffer->startframe += written; - expected_delay += written; } diff --git a/snd_mix.c b/snd_mix.c index af6ba2b8..940774e9 100644 --- a/snd_mix.c +++ b/snd_mix.c @@ -32,7 +32,6 @@ typedef struct portable_samplepair_s portable_sampleframe_t paintbuffer[PAINTBUFFER_SIZE]; -// FIXME: this desyncs with the video too easily extern void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate); static void S_CaptureAVISound(size_t length) { diff --git a/snd_oss.c b/snd_oss.c index d1bc5d12..c3a67aaf 100644 --- a/snd_oss.c +++ b/snd_oss.c @@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // OSS module, used by Linux and FreeBSD +#include #include #include #include @@ -90,10 +91,8 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) else Con_Print("SndSys_Init: fcntl(F_GETFL) failed!\n"); - ioctl(audio_fd, SNDCTL_DSP_RESET, NULL); - // Set the fragment size (up to "NB_FRAGMENTS" fragments of "fragmentsize" bytes) - fragmentsize = requested->speed * requested->channels * requested->width / 5; + fragmentsize = requested->speed * requested->channels * requested->width / 10; fragmentsize = (unsigned int)ceilf((float)fragmentsize / (float)NB_FRAGMENTS); fragmentsize = CeilPowerOf2(fragmentsize); ioctl_param = (NB_FRAGMENTS << 16) | log2i(fragmentsize); @@ -103,6 +102,8 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) SndSys_Shutdown (); return false; } + Con_Printf ("SndSys_Init: using %u fragments of %u bytes\n", + ioctl_param >> 16, 1 << (ioctl_param & 0xFFFF)); // Set the sound width if (requested->width == 1) @@ -200,6 +201,42 @@ void SndSys_Shutdown (void) } +/* +==================== +SndSys_Write +==================== +*/ +static int SndSys_Write (const unsigned char* buffer, unsigned int nb_bytes) +{ + int written; + unsigned int factor; + + written = write (audio_fd, buffer, nb_bytes); + if (written < 0) + { + if (errno != EAGAIN) + Con_Printf ("SndSys_Write: audio write returned %d! (errno= %d)\n", + written, errno); + return written; + } + + factor = snd_renderbuffer->format.width * snd_renderbuffer->format.channels; + if (written % factor != 0) + Sys_Error ("SndSys_Write: nb of bytes written (%d) isn't aligned to a frame sample!\n", + written); + + snd_renderbuffer->startframe += written / factor; + + if ((unsigned int)written < nb_bytes) + { + Con_DPrintf("SndSys_Submit: audio can't keep up! (%u < %u)\n", + written, nb_bytes); + } + + return written; +} + + /* ==================== SndSys_Submit @@ -222,42 +259,15 @@ void SndSys_Submit (void) nbframes = snd_renderbuffer->endframe - snd_renderbuffer->startframe; if (nbframes > limit) { - written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], limit * factor); - if (written < 0) - { - Con_Printf("SndSys_Submit: audio write returned %d!\n", written); - return; - } - - if (written % factor != 0) - Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written); - - snd_renderbuffer->startframe += written / factor; - - if ((unsigned int)written < limit * factor) - { - Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, limit * factor); + written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit * factor); + if (written < 0 || (unsigned int)written < limit * factor) return; - } nbframes -= limit; startoffset = 0; } - written = write (audio_fd, &snd_renderbuffer->ring[startoffset * factor], nbframes * factor); - if (written < 0) - { - Con_Printf("SndSys_Submit: audio write returned %d!\n", written); - return; - } - - if (written % factor != 0) - Sys_Error("SndSys_Submit: nb of bytes written (%d) isn't aligned to a frame sample!\n", written); - - snd_renderbuffer->startframe += written / factor; - - if ((unsigned int)written < nbframes * factor) - Con_Printf("SndSys_Submit: audio can't keep up! (%u < %u)\n", written, nbframes * factor); + SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], nbframes * factor); } @@ -274,7 +284,6 @@ unsigned int SndSys_GetSoundTime (void) int new_osstime; unsigned int timediff; - // TODO: use SNDCTL_DSP_GETODELAY instead if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1) { Con_Print ("SndSys_GetSoundTimeDiff: can't ioctl (SNDCTL_DSP_GETOPTR)\n"); -- 2.39.2