+ unsigned int factor, RequestedFrames, MaxFrames, FrameCount;
+ unsigned int StartOffset, EndOffset;
+
+ factor = snd_renderbuffer->format.channels * snd_renderbuffer->format.width;
+ if ((unsigned int)len % factor != 0)
+ Sys_Error("SDL sound: invalid buffer length passed to Buffer_Callback (%d bytes)\n", len);
+
+ RequestedFrames = (unsigned int)len / factor;
+
+ if (SndSys_LockRenderBuffer())
+ {
+ 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();
+ }