From b07a6fd098c961e237291bb38ec739b765a1bd2f Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 3 Oct 2023 19:34:42 -0700 Subject: [PATCH] Visualization: Improve latency and buffering appearance Adjust the buffering so if latency is too low, we fill the rest of the output with silence instead of peeking at the oldest part of the buffer. Also increase latency by half a buffer size so that the requested sample is in the center of the buffer, which improves the 4096 sample situation with the current low latency output. Signed-off-by: Christopher Snowhill --- Audio/Visualization/VisualizationController.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Audio/Visualization/VisualizationController.swift b/Audio/Visualization/VisualizationController.swift index b77d764cb..6e22eb0e3 100644 --- a/Audio/Visualization/VisualizationController.swift +++ b/Audio/Visualization/VisualizationController.swift @@ -98,15 +98,26 @@ class VisualizationController : NSObject { var outPCMCopy = Array(repeating: 0.0, count: 4096) serialQueue.sync { - let latencySamples = (Int)(self.latency * self.sampleRate) + // Offset latency so the target sample is in the center of the window + let latencySamples = (Int)((self.latency + latencyOffset) * self.sampleRate) + 2048 + var samplesToDo = 4096; + if(latencySamples < 4096) { + // Latency can sometimes dip below this threshold + samplesToDo = latencySamples; + } var j = self.visAudioCursor - latencySamples let k = self.visAudioSize if j < 0 { j += k } - for i in 0...4095 { + for i in 0..= k { j = 0 } } + if(samplesToDo < 4096) { + for i in samplesToDo...4095 { + outPCMCopy[i] = 0 + } + } } if(outPCM != nil) {