snd_sdl.c: lock the render buffer before running the threaded sound mixer
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 29 Apr 2009 15:23:37 +0000 (15:23 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 29 Apr 2009 15:23:37 +0000 (15:23 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8941 d7cf8633-e32d-0410-b094-e92efae38249

snd_sdl.c

index 667d56b..32990af 100644 (file)
--- a/snd_sdl.c
+++ b/snd_sdl.c
@@ -39,41 +39,47 @@ static void Buffer_Callback (void *userdata, Uint8 *stream, int len)
 
        RequestedFrames = (unsigned int)len / factor;
 
-       if (snd_usethreadedmixing)
+       if (SndSys_LockRenderBuffer())
        {
-               S_MixToBuffer(stream, RequestedFrames);
-               if (snd_blocked)
-                       memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
-               return;
-       }
+               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;
+               // 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);
+                       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);
-       }
-       else
-               memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
+                       PartialLength2 = FrameCount * factor - PartialLength1;
+                       memcpy(&stream[PartialLength1], &snd_renderbuffer->ring[0], PartialLength2);
+               }
+               else
+                       memcpy(stream, &snd_renderbuffer->ring[StartOffset * factor], FrameCount * factor);
 
-       snd_renderbuffer->startframe += FrameCount;
+               snd_renderbuffer->startframe += FrameCount;
 
-       if (FrameCount < RequestedFrames && developer.integer >= 1000 && vid_activewindow)
-               Con_Printf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
+               if (FrameCount < RequestedFrames && developer.integer >= 1000 && vid_activewindow)
+                       Con_Printf("SDL sound: %u sample frames missing\n", RequestedFrames - FrameCount);
 
-       sdlaudiotime += RequestedFrames;
+               sdlaudiotime += RequestedFrames;
+
+               SndSys_UnlockRenderBuffer();
+       }
 }