Matthew Matthew - 4 years ago 362
Swift Question

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

I'm trying to access

AVAudioPCMBuffer.floatChannelData
using Swift but it is of type
UnsafePointer<UnsafePointer<CFloat>>
(in Objective-C,
@property(nonatomic, readonly) float *const *floatChannelData
) and any attempt I make to access it results in execution failed.

Sample code to set-up a quick AVAudioPCMBuffer in a Swift Playground is included in a previous question:

Getting AVAudioPCMBuffer working (AVAudioFile.mm error code -50)

Answer Source

Does this work?

let channels = UnsafeBufferPointer(start: myAudioBuffer.floatChannelData, count: Int(myAudioBuffer.format.channelCount))
let floats = UnsafeBufferPointer(start: channels[0], count: Int(myAudioBuffer.frameLength))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download