From a9b15ee1137eaf7ea5040b0ceb74c844121e9a7f Mon Sep 17 00:00:00 2001 From: parsons Date: Thu, 14 May 2026 22:39:43 +0100 Subject: [PATCH] Audio latency is much better. Sprites are corrupted in GAW --- Desktop/Form1.cs | 2 +- Desktop/NAudioPlayer.cs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Desktop/Form1.cs b/Desktop/Form1.cs index d6923f1..ea24f5c 100644 --- a/Desktop/Form1.cs +++ b/Desktop/Form1.cs @@ -89,7 +89,7 @@ namespace Desktop IsRunning = true; TotalFrameCount = 0; - double TargetFrameTime = 1000.0 / 60.0; + double TargetFrameTime = 1000.0 / 59.92274; _emulatorTask = Task.Run(() => { diff --git a/Desktop/NAudioPlayer.cs b/Desktop/NAudioPlayer.cs index aaf4c2e..95d5546 100644 --- a/Desktop/NAudioPlayer.cs +++ b/Desktop/NAudioPlayer.cs @@ -12,24 +12,24 @@ namespace Desktop.Audio // Change this to match your project's namespace public NAudioPlayer() { - // Set up the audio format: 44100 Hz, 32-bit IEEE float, 1 channel (Mono) WaveFormat format = WaveFormat.CreateIeeeFloatWaveFormat(44100, 1); _buffer = new BufferedWaveProvider(format); - // CRITICAL FOR EMULATORS: - // If the emulator runs slightly too fast, the audio buffer will fill up - // and the sound will lag behind the gameplay. Discarding overflow keeps it synced! + // THE FIX: Restrict the physical size of the buffer to exactly 100 milliseconds! + // If the emulator generates audio too fast, the lag can never exceed this limit. + _buffer.BufferDuration = TimeSpan.FromMilliseconds(100); + _buffer.DiscardOnBufferOverflow = true; _waveOut = new WaveOutEvent(); - // 100ms latency is a great sweet spot for WinForms emulators. - // Too low = crackling audio. Too high = delayed sound effects. - _waveOut.DesiredLatency = 100; + // Drop the latency to 50ms for a tighter, snappier response + _waveOut.DesiredLatency = 50; _waveOut.Init(_buffer); _waveOut.Play(); } + public void AddSample(float sample) {