# HG changeset patch # User sam # Date 1719312130 -25200 # Node ID 92691ddcb9fefc38961467fb464ff5e88806af72 # Parent f32359ffd8828ea3a88bc5c47c7a703e5b58a0a4 fix: large audio-buffers lead to latency when playing sounds diff -r f32359ffd882 -r 92691ddcb9fe semicongine/audio.nim --- a/semicongine/audio.nim Sat Jun 15 21:02:21 2024 +0700 +++ b/semicongine/audio.nim Tue Jun 25 17:42:10 2024 +0700 @@ -17,7 +17,7 @@ export audiotypes const NBUFFERS = 4 -const BUFFERSAMPLECOUNT = 2048 +const BUFFERSAMPLECOUNT = 256 type Playback = object @@ -202,6 +202,11 @@ proc updateSoundBuffer(mixer: var Mixer) = let t = getMonoTime() + + let tDebug = getTime() + # echo "" + # echo tDebug + let dt = (t - mixer.lastUpdate).inNanoseconds.float64 / 1_000_000_000'f64 mixer.lastUpdate = t @@ -237,7 +242,9 @@ track.playing.del(id) mixer.buffers[mixer.currentBuffer][i] = mixedSample # send data to sound device + # echo getTime() - tDebug mixer.device.WriteSoundData(mixer.currentBuffer) + # echo getTime() - tDebug mixer.currentBuffer = (mixer.currentBuffer + 1) mod mixer.buffers.len # DSP functions