]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
snd: don't lock the device while we already have the lock
authorbones_was_here <bones_was_here@xonotic.au>
Sun, 25 Aug 2024 15:34:33 +0000 (01:34 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Thu, 12 Sep 2024 13:07:47 +0000 (23:07 +1000)
while also having a comment about how we don't need to do it...

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
snd_sdl.c

index e93d302113f31e5cb509faa5f5824c584ae21a1f..3752dcdee49c47f726f93071afc923eeb04ccd87 100644 (file)
--- 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;
 }