Skip to content

Commit

Permalink
Visualization: Improve latency and buffering appearance
Browse files Browse the repository at this point in the history
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 <kode54@gmail.com>
  • Loading branch information
kode54 committed Oct 4, 2023
1 parent 416e77d commit 8e90da6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Audio/Visualization/VisualizationController.swift
Expand Up @@ -98,15 +98,26 @@ class VisualizationController : NSObject {
var outPCMCopy = Array<Float>(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..<samplesToDo {
let x = self.visAudio[j]
outPCMCopy[i] = x
j += 1; if j >= k { j = 0 }
}
if(samplesToDo < 4096) {
for i in samplesToDo...4095 {
outPCMCopy[i] = 0
}
}
}

if(outPCM != nil) {
Expand Down

0 comments on commit 8e90da6

Please sign in to comment.