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;
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;
}