File tree Expand file tree Collapse file tree
src/Spice86.Core/Emulator/Devices/Sound Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313/// Virtual device which emulates OPL3 FM sound.
1414/// </summary>
1515public class Opl3Fm : DefaultIOPortHandler , IDisposable {
16+ /// <summary>
17+ /// Maximum number of interleaved samples generated per lock acquisition when producing audio.
18+ /// Keep this value even so we always generate whole stereo frames.
19+ /// </summary>
20+ private const int MaxSamplesPerGenerationBatch = 256 ;
21+
1622 private readonly AdLibGoldDevice ? _adLibGold ;
1723 private readonly AdLibGoldIo ? _adLibGoldIo ;
1824 private readonly Opl3Chip _chip = new ( ) ;
@@ -290,9 +296,17 @@ private void RenderTo(Span<float> destination) {
290296
291297 Span < short > interleaved = _tmpInterleaved . AsSpan ( 0 , samples ) ;
292298
293- lock ( _chipLock ) {
294- interleaved . Clear ( ) ;
295- _chip . GenerateStream ( interleaved ) ;
299+ int processed = 0 ;
300+ while ( processed < samples ) {
301+ int chunkSamples = Math . Min ( MaxSamplesPerGenerationBatch , samples - processed ) ;
302+ Span < short > chunk = interleaved . Slice ( processed , chunkSamples ) ;
303+
304+ lock ( _chipLock ) {
305+ chunk . Clear ( ) ;
306+ _chip . GenerateStream ( chunk ) ;
307+ }
308+
309+ processed += chunkSamples ;
296310 }
297311
298312 const float scale = 1.0f / 32768f ;
You can’t perform that action at this time.
0 commit comments