[Cog Audio] Make the Swift Vis Controller work
And this is the actual meat of getting it to work properly, the changes the Swift code needed to actually be fully functional. Signed-off-by: Christopher Snowhill <kode54@gmail.com>xcode15
parent
aed52840ca
commit
41a04760eb
|
@ -8,27 +8,30 @@
|
|||
import Foundation
|
||||
|
||||
@objc(VisualizationController)
|
||||
class VisualizationController {
|
||||
class VisualizationController : NSObject {
|
||||
var serialQueue = DispatchQueue(label: "Visualization Queue")
|
||||
var sampleRate = 44100.0
|
||||
var sampleRate = 0.0
|
||||
var latency = 0.0
|
||||
var visAudio: [Float] = Array(repeating: 0.0, count: 44100 * 45)
|
||||
var visAudioCursor = 0
|
||||
var visAudioSize = 0
|
||||
|
||||
private static var sharedController: VisualizationController = {
|
||||
private static var sharedVisualizationController: VisualizationController = {
|
||||
let visualizationController = VisualizationController()
|
||||
return visualizationController
|
||||
}()
|
||||
|
||||
class func sharedVisualizationController() -> VisualizationController {
|
||||
return sharedController
|
||||
|
||||
@objc
|
||||
class func sharedController() -> VisualizationController {
|
||||
return sharedVisualizationController
|
||||
}
|
||||
|
||||
@objc
|
||||
func postLatency(_ latency: Double) {
|
||||
self.latency = latency
|
||||
}
|
||||
|
||||
@objc
|
||||
func postSampleRate(_ sampleRate: Double) {
|
||||
serialQueue.sync {
|
||||
if(self.sampleRate != sampleRate) {
|
||||
|
@ -40,57 +43,71 @@ class VisualizationController {
|
|||
}
|
||||
}
|
||||
|
||||
@objc
|
||||
func postVisPCM(_ inPCM: UnsafePointer<Float>?, amount: Int) {
|
||||
serialQueue.sync {
|
||||
let bufferPointer = UnsafeBufferPointer(start: inPCM, count: amount)
|
||||
if let bptr = bufferPointer {
|
||||
let dataArray = bptr.assumingMemoryBound(to: Float.self)
|
||||
var j = self.visAudioCursor
|
||||
var k = self.visAudioSize
|
||||
for i in 0..<amount {
|
||||
let x = Float(dataArray[i])
|
||||
self.visAudio[j] = x
|
||||
j++; if j >= k { j = 0 }
|
||||
}
|
||||
self.visAudioCursor = j
|
||||
let bufferPointer = UnsafeBufferPointer<Float>(start: inPCM, count: amount)
|
||||
var j = self.visAudioCursor
|
||||
let k = self.visAudioSize
|
||||
for i in 0..<amount {
|
||||
let x = bufferPointer[i]
|
||||
self.visAudio[j] = x
|
||||
j += 1; if j >= k { j = 0 }
|
||||
}
|
||||
self.visAudioCursor = j
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@objc
|
||||
func readSampleRate() -> Double {
|
||||
serialQueue.sync {
|
||||
return self.sampleRate
|
||||
}
|
||||
}
|
||||
|
||||
func copyVisPCM(_ outPCM: UnsafeMutablePointer<Float>?, visFFT: UnsafeMutablePointer<Float>?, latencyoffset: Double) {
|
||||
let outPCMCopy = Array<Float>(repeating: 0.0, count: 4096)
|
||||
|
||||
@objc
|
||||
func copyVisPCM(_ outPCM: UnsafeMutablePointer<Float>?, visFFT: UnsafeMutablePointer<Float>?, latencyOffset: Double) {
|
||||
if(self.visAudioSize == 0) {
|
||||
if(outPCM != nil) {
|
||||
let pcmPointer = UnsafeMutableBufferPointer<Float>(start: outPCM, count: 4096)
|
||||
for i in 0...4095 {
|
||||
pcmPointer[i] = 0.0
|
||||
}
|
||||
}
|
||||
if(visFFT != nil) {
|
||||
let fftPointer = UnsafeMutableBufferPointer<Float>(start: visFFT, count: 2048)
|
||||
for i in 0...2047 {
|
||||
fftPointer[i] = 0.0
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var outPCMCopy = Array<Float>(repeating: 0.0, count: 4096)
|
||||
|
||||
serialQueue.sync {
|
||||
var latencySamples = (Int)(self.latency * self.sampleRate)
|
||||
let latencySamples = (Int)(self.latency * self.sampleRate)
|
||||
var j = self.visAudioCursor - latencySamples
|
||||
var k = self.visAudioSize
|
||||
let k = self.visAudioSize
|
||||
if j < 0 { j += k }
|
||||
for i in 0..4095 {
|
||||
for i in 0...4095 {
|
||||
let x = self.visAudio[j]
|
||||
outPCMCopy[i] = x
|
||||
j++; if j >= k { j = 0 }
|
||||
j += 1; if j >= k { j = 0 }
|
||||
}
|
||||
}
|
||||
|
||||
let pcmPointer = UnsafeMutableBufferPointer(start: outPCM, count: 4096)
|
||||
if let bptr = pcmPointer {
|
||||
let dataArray = bptr.assumingMemoryBound(to: Float.self)
|
||||
for i in 0..4095 {
|
||||
|
||||
if(outPCM != nil) {
|
||||
let pcmPointer = UnsafeMutableBufferPointer<Float>(start: outPCM, count: 4096)
|
||||
for i in 0...4095 {
|
||||
let x = outPCMCopy[i]
|
||||
dataArray[i] = x
|
||||
pcmPointer[i] = x
|
||||
}
|
||||
}
|
||||
|
||||
let fftPointer = UnsafeMutablePointer(start: visFFT, count: 2048)
|
||||
if let bptr = fftPointer {
|
||||
|
||||
if(visFFT != nil) {
|
||||
serialQueue.sync {
|
||||
fft_calculate(outPCMCopy, bptr, 2048)
|
||||
fft_calculate(outPCMCopy, visFFT, 2048)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue