我正在使用那些NSData到PCMBuffer函数,一旦它尝试获取指向浮动通道数据(可变通道)的指针,就不能将其作为Float64,或者我也尝试了其他格式,并且没有输入浮动频道数据。它给我返回错误:
“致命错误:UnsafeBufferPointer的起始位置为零,且计数为非零”
任何帮助或见识都会有用。
func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer {
let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat64, sampleRate: 48000, channels: 2, interleaved: true) // given NSData audio format
let PCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat!, frameCapacity: UInt32(data.length) / audioFormat!.streamDescription.pointee.mBytesPerFrame)
PCMBuffer!.frameLength = PCMBuffer!.frameCapacity
let channels = UnsafeBufferPointer(start: PCMBuffer?.floatChannelData, count:2)
data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.length/8)
data.getBytes(UnsafeMutableRawPointer(channels[1]) , length: data.length/8)
return PCMBuffer!
}