changeset 1166:92691ddcb9fe

fix: large audio-buffers lead to latency when playing sounds
author sam <sam@basx.dev>
date Tue, 25 Jun 2024 17:42:10 +0700
parents f32359ffd882
children cdf7ec8d04d2
files semicongine/audio.nim
diffstat 1 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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