gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:32:01 24 4
gpt4 key购买 nike

我正在尝试将录制的音频文件转换为 [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 的父类(super class),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)

关于ios - 将音频转换为 [Double] 类型的原始 PCM 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com