【发布时间】:2022-06-20 07:03:57
【问题描述】:
我有一个Float 数组(代表音频样本),我想把它变成AVAudioPCMBuffer,这样我就可以把它传递给AVAudioFile 的write(from:)。有一个明显的方法(其实一点都不明显,我抄自this gist):
var floats: [Float] = ... // this comes from somewhere else
let audioBuffer = AudioBuffer(mNumberChannels: 1, mDataByteSize: UInt32(floats.count * MemoryLayout<Float>.size), mData: &floats)
var bufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: audioBuffer)
let outputAudioBuffer = AVAudioPCMBuffer(pcmFormat: buffer.format, bufferListNoCopy: &bufferList)!
try self.renderedAudioFile?.write(from: outputAudioBuffer)
这有效(我得到了我期望的音频输出)但是在 Xcode 13.4.1 中这给了我一个警告 &floats:Cannot use inout expression here; argument 'mData' must be a pointer that outlives the call to 'init(mNumberChannels:mDataByteSize:mData:)'
好的,然后确定指针的范围:
var floats: [Float] = ... // this comes from somewhere else
try withUnsafeMutablePointer(to: &floats) { bytes in
let audioBuffer = AudioBuffer(mNumberChannels: 1, mDataByteSize: UInt32(bytes.pointee.count * MemoryLayout<Float>.size), mData: bytes)
var bufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: audioBuffer)
let outputAudioBuffer = AVAudioPCMBuffer(pcmFormat: buffer.format, bufferListNoCopy: &bufferList)!
try self.renderedAudioFile?.write(from: outputAudioBuffer)
}
警告消失了,但现在输出是垃圾。我真的不明白这是因为floats.count 和bytes.pointee.count 是同一个数字。我做错了什么?
【问题讨论】:
-
你不想
withUnsafeMutableBufferPointer吗? -
显然不是:
Cannot convert value of type 'UnsafeMutableBufferPointer<Float>' to expected argument type 'UnsafeMutableRawPointer?'(它作为mData传递到AudioBuffer构造函数) -
Objective-C 中的辅助函数可能是一个选项
标签: swift pointers avaudioengine audiobuffer audiobufferlist