+ unsigned int maxFrames, sampleCount;
+ unsigned int startOffset, endOffset;
+ const short *samples;
+
+ if (snd_usethreadedmixing)
+ {
+ S_MixToBuffer(mixbuffer, submissionChunk);
+ sampleCount = submissionChunk * snd_renderbuffer->format.channels;
+ for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
+ outBuffer[sampleIndex] = mixbuffer[sampleIndex] * scale;
+ // unlock the mutex now
+ SndSys_UnlockRenderBuffer();
+ return 0;
+ }
+
+ // Transfert up to a chunk of sample frames from snd_renderbuffer to outBuffer
+ maxFrames = snd_renderbuffer->endframe - snd_renderbuffer->startframe;
+ if (maxFrames >= submissionChunk)
+ frameCount = submissionChunk;
+ else
+ frameCount = maxFrames;
+
+ // Convert the samples from shorts to floats. Scale the floats to be [-1..1].
+ startOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
+ endOffset = (snd_renderbuffer->startframe + frameCount) % snd_renderbuffer->maxframes;
+ if (startOffset > endOffset) // if the buffer wraps
+ {
+ sampleCount = (snd_renderbuffer->maxframes - startOffset) * snd_renderbuffer->format.channels;
+ samples = (const short*)(&snd_renderbuffer->ring[startOffset * factor]);
+ for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
+ outBuffer[sampleIndex] = samples[sampleIndex] * scale;
+
+ outBuffer = &outBuffer[sampleCount];
+ sampleCount = frameCount * snd_renderbuffer->format.channels - sampleCount;
+ samples = (const short*)(&snd_renderbuffer->ring[0]);
+ for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
+ outBuffer[sampleIndex] = samples[sampleIndex] * scale;
+ }
+ else
+ {
+ sampleCount = frameCount * snd_renderbuffer->format.channels;
+ samples = (const short*)(&snd_renderbuffer->ring[startOffset * factor]);
+ for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
+ outBuffer[sampleIndex] = samples[sampleIndex] * scale;
+ }
+
+ snd_renderbuffer->startframe += frameCount;
+
+ // unlock the mutex now
+ SndSys_UnlockRenderBuffer();