将音频转换为 [Double] 类型的原始 PCM 数据

Convert audio into raw PCM data of type [Double]

我正在尝试将录制的音频文件转换为 [Double] 类型的原始 PCM 数据。我找到了一种方法来加载音频并将其转换为 [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)))

问题是,我需要数据作为 [Double] 而 AVAudioPCMBuffer() 只知道 .PCMFormatFloat32。有人知道解决方法吗? 谢谢。

但是AVAudioFormat知道.PCMFormatFloat64:

let format = AVAudioFormat(commonFormat: .PCMFormatFloat64, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)

也许你的意思是 AVAudioPCMBuffer 没有 float64ChannelData 便利 属性?

没关系,您可以使用 AVAudioPCMBuffer 的超类,AVAudioBuffer 拥有您需要的一切,能够获取原始 Double/Float64 样本:

let abl = buf.audioBufferList.memory
let doubles = UnsafePointer<Double>(abl.mBuffers.mData)
doubles[0] // etc...

完整:

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)

该代码可能不再有效。
这是新的工作代码。

Swift 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)))

更新为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)")
        }