我有一个带有自定义 AURenderCallback 的 AVAudioEngine。当我运行引擎时,cpu 会飙升,这似乎与 UnsafeMutableBufferPointer.getter 和 .setter 操作有关:
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
}
什么会导致获取和设置指针数据的这种低效行为?因为它正在处理音频样本,所以效率低下会导致音频信号出现断断续续的情况。