Visualization: Optimize Swift code handling arrays
This looks a lot better than some ruddy for-loops. Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
parent
6470b2627f
commit
146dae216a
1 changed files with 18 additions and 8 deletions
|
@ -67,10 +67,17 @@ class VisualizationController : NSObject {
|
||||||
let bufferPointer = UnsafeBufferPointer<Float>(start: inPCM, count: amount)
|
let bufferPointer = UnsafeBufferPointer<Float>(start: inPCM, count: amount)
|
||||||
var j = self.visAudioCursor
|
var j = self.visAudioCursor
|
||||||
let k = self.visAudioSize
|
let k = self.visAudioSize
|
||||||
for i in 0..<amount {
|
if(j + amount <= k) {
|
||||||
let x = bufferPointer[i]
|
let endIndex = j + amount;
|
||||||
self.visAudio[j] = x
|
self.visAudio.replaceSubrange(j..<endIndex, with: bufferPointer)
|
||||||
j += 1; if j >= k { j = 0 }
|
j += amount
|
||||||
|
if(j >= k) { j = 0 }
|
||||||
|
} else {
|
||||||
|
let inEndIndex = k - j
|
||||||
|
let remainder = amount - inEndIndex
|
||||||
|
self.visAudio.replaceSubrange(j..<k, with: bufferPointer.prefix(inEndIndex))
|
||||||
|
self.visAudio.replaceSubrange(0..<remainder, with: bufferPointer.suffix(remainder))
|
||||||
|
j = remainder
|
||||||
}
|
}
|
||||||
self.visAudioCursor = j
|
self.visAudioCursor = j
|
||||||
self.visSamplesPosted += UInt64(amount);
|
self.visSamplesPosted += UInt64(amount);
|
||||||
|
@ -108,10 +115,13 @@ class VisualizationController : NSObject {
|
||||||
var j = self.visAudioCursor - latencySamples
|
var j = self.visAudioCursor - latencySamples
|
||||||
let k = self.visAudioSize
|
let k = self.visAudioSize
|
||||||
if j < 0 { j += k }
|
if j < 0 { j += k }
|
||||||
for i in 0..<samplesToDo {
|
if(j + samplesToDo <= k) {
|
||||||
let x = self.visAudio[j]
|
outPCMCopy.replaceSubrange(0..<samplesToDo, with: self.visAudio.suffix(from: j).prefix(samplesToDo))
|
||||||
outPCMCopy[i] = x
|
} else {
|
||||||
j += 1; if j >= k { j = 0 }
|
let outEndIndex = k - j
|
||||||
|
let remainder = samplesToDo - outEndIndex
|
||||||
|
outPCMCopy.replaceSubrange(0..<outEndIndex, with: self.visAudio.suffix(from: j))
|
||||||
|
outPCMCopy.replaceSubrange(outEndIndex..<samplesToDo, with: self.visAudio.prefix(remainder))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue