斯威夫特3铸造问题 - AudioQueue
问题描述:
我想转换为斯威夫特3语法如下代码:斯威夫特3铸造问题 - AudioQueue
fileprivate func generateTone(_ buffer: AudioQueueBufferRef) {
if noteAmplitude == 0 {
memset(buffer.pointee.mAudioData, 0, Int(buffer.pointee.mAudioDataBytesCapacity))
} else {
let count: Int = Int(buffer.pointee.mAudioDataBytesCapacity)/MemoryLayout<Float32>.size
var x: Double = 0
var y: Double = 0
let audioData = UnsafeMutablePointer<Float32>(buffer.pointee.mAudioData)
for frame in 0..<count {
x = noteFrame * noteFrequency/kSampleRate
y = sin (x * 2.0 * M_PI) * noteAmplitude
audioData[frame] = Float32(y)
noteAmplitude -= noteDecay
if noteAmplitude < 0.0 {
noteAmplitude = 0
}
noteFrame += 1
}
}
buffer.pointee.mAudioDataByteSize = buffer.pointee.mAudioDataBytesCapacity
}
我坚持用:
let audioData = UnsafeMutablePointer<Float32>(buffer.pointee.mAudioData)
Xcode的抱怨:
无法使用类型为'(UnsafeMutableRawPointer)'的参数列表调用类型为'UnsafeMutablePointer' 的初始值设定项'
该项目可在here
任何帮助将是非常赞赏:)
答
mAudioData
是 “无类型指针”(UnsafeMutableRawPointer
),你可以 将它转换为一个带有指针的指针assumingMemoryBound
:
let audioData = buffer.pointee.mAudioData.assumingMemoryBound(to: Float32.self)
有关原始指针的更多信息,请参见SE-0107 UnsafeRawPointer API 。
是的,现在对我来说很简单。谢谢马丁! –