From: bones_was_here Date: Sun, 25 Aug 2024 15:34:33 +0000 (+1000) Subject: snd: don't lock the device while we already have the lock X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=6fac3b2c6a5610437ba0f6d4b460879287b6487d;p=xonotic%2Fdarkplaces.git snd: don't lock the device while we already have the lock while also having a comment about how we don't need to do it... Signed-off-by: bones_was_here --- diff --git a/snd_sdl.c b/snd_sdl.c index e93d3021..3752dcde 100644 --- a/snd_sdl.c +++ b/snd_sdl.c @@ -29,7 +29,8 @@ static unsigned int sdlaudiotime = 0; static int audio_device = 0; -// Note: SDL calls SDL_LockAudio() right before this function, so no need to lock the audio data here +// This function is called when the audio device needs more data. +// Note: SDL obtains the device lock before calling this function, no need to lock it here. static void Buffer_Callback (void *userdata, Uint8 *stream, int len) { unsigned int factor, RequestedFrames, MaxFrames, FrameCount; @@ -41,57 +42,51 @@ static void Buffer_Callback (void *userdata, Uint8 *stream, int len) RequestedFrames = (unsigned int)len / factor; - if (SndSys_LockRenderBuffer()) + if (snd_usethreadedmixing) { - if (snd_usethreadedmixing) - { - S_MixToBuffer(stream, RequestedFrames); - if (snd_blocked) - memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len); - SndSys_UnlockRenderBuffer(); - return; - } - - // Transfert up to a chunk of samples from snd_renderbuffer to stream - MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe; - if (MaxFrames > RequestedFrames) - FrameCount = RequestedFrames; - else - FrameCount = MaxFrames; - StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes; - EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes; - if (StartOffset > EndOffset) // if the buffer wraps - { - unsigned int PartialLength1, PartialLength2; - - PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor; - memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1); - - PartialLength2 = FrameCount * factor - PartialLength1; - memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2); - - // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence - // FIXME this is another place that assumes 8bit is always unsigned and others always signed - memset(&stream[PartialLength1 + PartialLength2], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (PartialLength1 + PartialLength2)); - } - else - { - memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor); - - // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence - // FIXME this is another place that assumes 8bit is always unsigned and others always signed - memset(&stream[FrameCount * factor], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (FrameCount * factor)); - } - - snd_renderbuffer->startframe += FrameCount; - - if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow) - Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount); - - sdlaudiotime += RequestedFrames; - - SndSys_UnlockRenderBuffer(); + S_MixToBuffer(stream, RequestedFrames); + if (snd_blocked) + memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len); + return; } + + // Transfert up to a chunk of samples from snd_renderbuffer to stream + MaxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe; + if (MaxFrames > RequestedFrames) + FrameCount = RequestedFrames; + else + FrameCount = MaxFrames; + StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes; + EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes; + if (StartOffset > EndOffset) // if the buffer wraps + { + unsigned int PartialLength1, PartialLength2; + + PartialLength1 = (snd_renderbuffer->maxframes - StartOffset) * factor; + memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], PartialLength1); + + PartialLength2 = FrameCount * factor - PartialLength1; + memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2); + + // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence + // FIXME this is another place that assumes 8bit is always unsigned and others always signed + memset(&stream[PartialLength1 + PartialLength2], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (PartialLength1 + PartialLength2)); + } + else + { + memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor); + + // As of SDL 2.0 buffer needs to be fully initialized, so fill leftover part with silence + // FIXME this is another place that assumes 8bit is always unsigned and others always signed + memset(&stream[FrameCount * factor], snd_renderbuffer->format.width == 1 ? 0x80 : 0, len - (FrameCount * factor)); + } + + snd_renderbuffer->startframe += FrameCount; + + if (FrameCount < RequestedFrames && developer_insane.integer && vid_activewindow) + Con_DPrintf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount); + + sdlaudiotime += RequestedFrames; }