- 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);
+ // 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);