来自UnsafePointer <unsafepointer <cfloat>&gt;到Swift中的浮点数组?</unsafepointer <cfloat>

时间:2014-06-06 20:27:59

标签: avfoundation swift

我正在尝试使用Swift访问AVAudioPCMBuffer.floatChannelData,但它的类型为UnsafePointer<UnsafePointer<CFloat>>(在Objective-C中,@property(nonatomic, readonly) float *const *floatChannelData)并且我为访问它而进行的任何尝试都会导致执行失败

在Swift Playground中设置快速AVAudioPCMBuffer的示例代码包含在上一个问题中:

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

2 个答案:

答案 0 :(得分:6)

这有用吗?

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

答案 1 :(得分:3)

您还可以做的是访问AVAudioBuffer

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

您可以在Apple pre-release documentation:

中找到更多相关信息
  

floatChannelData属性返回指向缓冲区音频的指针   样本,如果缓冲区的格式是32位浮点数。如果它返回nil   是另一种格式。返回的指针是format.channelCount   指针浮动。这些指针中的每一个都是frameLength有效的   样本,由stride个样本间隔开。