2
我有一個帶有自定義AURenderCallback的AVAudioEngine。當我運行的引擎,CPU峯值起來,它似乎已經與UnsafeMutableBufferPointer.getter和.setter行動做:使用getter時UnsafeMutableBufferPointer速度較慢
func performRender(ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>, inTimeStamp: UnsafePointer<AudioTimeStamp>, inBusNumber: UInt32, inNumberFrames: UInt32, ioData: UnsafeMutablePointer<AudioBufferList>?) -> OSStatus {
let blI = UnsafeMutableAudioBufferListPointer(ioData)
let numSamples = Int((blI?[0].mDataByteSize)!/UInt32(MemoryLayout<Float32>.stride))
for input in 0 ..< blI!.count{
let bI = blI?[input]
guard let inputData = bI?.mData else {
//assert(false)
return kAudioUnitErr_Uninitialized
}
let samplesBI = UnsafeMutablePointer<Float32>(inputData.assumingMemoryBound(to: Float32.self))
let samplesI = UnsafeMutableBufferPointer<Float32>(start: samplesBI, count: numSamples)
for sampleIndex in 0 ..< samplesI.count {
samplesI[sampleIndex] *= 0.5
}
}
return noErr
}
什麼會導致獲取和設置的這個低效行爲指針數據?由於它正在處理音頻樣本,因此效率低下會導致音頻信號中出現斷斷續續的情況。