[英]Convert audio into raw PCM data of type [Double]
I'm trying to convert a recorded audio file into raw PCM data of type [Double].我正在尝试将录制的音频文件转换为 [Double] 类型的原始 PCM 数据。 I found a way to load and convert audio to PCM data of the type [Float32] like this:
我找到了一种加载音频并将其转换为 [Float32] 类型的 PCM 数据的方法,如下所示:
// load audio from path
let file = try! AVAudioFile(forReading: self.soundFileURL)
// declare format
let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
// initialize audiobuffer with the length of the audio file
let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length))
// write to file
try! file.readIntoBuffer(buf)
// copy to array
let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))
The problem is, I need the data as [Double] and AVAudioPCMBuffer() only knows the .PCMFormatFloat32
.问题是,我需要数据为 [Double] 并且 AVAudioPCMBuffer() 只知道
.PCMFormatFloat32
。 Does anybody know a workaround?有人知道解决方法吗? Thank you.
谢谢。
But AVAudioFormat
does know .PCMFormatFloat64
:但
AVAudioFormat
确实知道.PCMFormatFloat64
:
let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
Maybe you mean AVAudioPCMBuffer
doesn't have a float64ChannelData
convenience property?也许您的意思是
AVAudioPCMBuffer
没有float64ChannelData
便利属性?
That's fine, you can use AVAudioPCMBuffer
's superclass, AVAudioBuffer
has everying you need to be able to get at the raw Double
/ Float64
samples:没关系,您可以使用
AVAudioPCMBuffer
的超类, AVAudioBuffer
拥有您需要能够获得原始Double
/ Float64
样本的Float64
:
let abl = buf.audioBufferList.memory
let doubles = UnsafePointer<Double>(abl.mBuffers.mData)
doubles[0] // etc...
In full:全文:
let file = try! AVAudioFile(forReading: self.soundFileURL)
let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length))
try! file.readIntoBuffer(buf)
let abl = buf.audioBufferList.memory
let doubles = UnsafePointer<Float64>(abl.mBuffers.mData)
that code doesn't work anymore maybe.那个代码可能不再工作了。
here is new working code.这是新的工作代码。
Swift 3.2斯威夫特 3.2
let file = try! AVAudioFile(forReading: soundFileURL)
let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: AVAudioFrameCount(file.length))
try! file.readIntoBuffer(buf)
let abl = Array(UnsafeBufferPointer(start: buf.audioBufferList, count: Int(buf.audioBufferList.pointee.mNumberBuffers)))
let buffer = audioBufferList[0].mBuffers
let mDataList = Array(UnsafeMutableRawBufferPointer(start: buffer.mData, count: Int(buffer.mDataByteSize)))
updated to Swift 5更新到Swift 5
do {
let file = try AVAudioFile(forReading: soundFileURL)
if let format = AVAudioFormat(commonFormat: .pcmFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false), let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(file.length)){
try file.read(into: buf)
let abl = Array(UnsafeBufferPointer(start: buf.audioBufferList, count: Int(buf.audioBufferList.pointee.mNumberBuffers)))
let buffer = buf.audioBufferList[0].mBuffers
let mDataList = Array(UnsafeMutableRawBufferPointer(start: buffer.mData, count: Int(buffer.mDataByteSize)))
}
} catch{
print("Audio Error: \(error)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.