From UnsafePointer<UnsafePointer<CFloat>> to an array of floats in Swift?

三世轮回 提交于 2019-12-04 02:45:29
trilorez

Does this work?

let channels = UnsafeBufferPointer(start: myAudioBuffer.floatChannelData, count: Int(myAudioBuffer.format.channelCount))
let floats = UnsafeBufferPointer(start: channels[0], count: Int(myAudioBuffer.frameLength))

What you can also do is in order to access AVAudioBuffer:

for var i = 0; i < Int(audioBuffer.frameLength); i+=Int(audioMixerNode.outputFormatForBus(0).channelCount) {
    self.audioBuffer.floatChannelData.memory[i] = 0.0f; // or whatever
}

You can find more about that in the Apple pre-release documentation:

The floatChannelData property returns pointers to the buffer’s audio samples, if the buffer’s format is 32-bit float. It returns nil if it is another format. The returned pointer is to format.channelCount pointers to float. Each of these pointers is to frameLength valid samples, which are spaced by stride samples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!