我正在尝试将以下代码转换为SWIFT 3语法:
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投诉:
无法为类型“unsafemutablepointer”调用初始值设定项
具有类型为“(unsafemutablerawpointer)”的参数列表
该项目可在here
如有任何帮助,我们将不胜感激:)
最佳答案
mAudioData
是一个“非类型化指针”(UnsafeMutableRawPointer
),而您
可以用assumingMemoryBound
将其转换为类型指针:
let audioData = buffer.pointee.mAudioData.assumingMemoryBound(to: Float32.self)
见SE-0107 UnsafeRawPointer API
有关原始指针的详细信息。