Swift 3带UnsafemtableRawPointer的强制转换问题-AudioQueue
我正在尝试将以下代码转换为Swift 3语法:Swift 3带UnsafemtableRawPointer的强制转换问题-AudioQueue,swift,casting,swift3,audioqueue,Swift,Casting,Swift3,Audioqueue,我正在尝试将以下代码转换为Swift 3语法: fileprivate func generateTone(_ buffer: AudioQueueBufferRef) { if noteAmplitude == 0 { memset(buffer.pointee.mAudioData, 0, Int(buffer.pointee.mAudioDataBytesCapacity)) } else { let coun
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
}
fileprivate func generateTone(ubuffer:AudioQueueBufferRef){
如果振幅==0{
memset(buffer.pointee.mAudioData,0,Int(buffer.pointee.mAudioData字节容量))
}否则{
let count:Int=Int(buffer.pointee.mAudioDataBytesCapacity)/MemoryLayout.size
变量x:Double=0
变量y:Double=0
让audioData=unsafemeutablepointer(buffer.pointee.mAudioData)
对于0中的帧
可以使用assumingMemoryBound
将其转换为键入的指针:
let audioData = buffer.pointee.mAudioData.assumingMemoryBound(to: Float32.self)
看
有关原始指针的更多信息。是的,现在对我来说非常简单。谢谢Martin!你好,Artur,您介意用您的解决方案更新您的示例项目吗?
let audioData = buffer.pointee.mAudioData.assumingMemoryBound(to: Float32.self)