gpt4 book ai didi

swift - 将 pcm 加载到 AVAudioPCMBuffer

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:25 27 4
gpt4 key购买 nike

我有这个代码:

func loadSoundfont(_ pitch : String) {
let path: String = Bundle.main.path(forResource: "\(self.id)/\(pitch)", ofType: "f32")!
let url = URL(fileURLWithPath: path)

do {
let file = try AVAudioFile(forReading: url, commonFormat: AVAudioCommonFormat.pcmFormatFloat32, interleaved: true)
let format = file.processingFormat
let capacity = AVAudioFrameCount(file.length)
self.buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: capacity)
try file.read(into: self.buffer!)
} catch let error as NSError {
print("ERROR HERE", error.localizedDescription)
}
}

我收到以下错误:1954115647 也就是:kAudioFileUnsupportedFileTypeError

我的 A4.f32 文件包含一个 PCM float 32。我在这里遗漏了什么吗?我的文件中没有 header ,是否会导致此问题?

最佳答案

感谢 Rhythmic Fistman 的评论,我自己去加载了它,就是这样:

func loadSoundfont(_ pitch : String) {
let path: String = Bundle.main.path(forResource: "\(self.id)/\(pitch)", ofType: "f32")!
let url = URL(fileURLWithPath: path)

do {
let data = try Data(contentsOf: url)
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: true)

self.buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(data.count))

self.buffer!.floatChannelData!.pointee.withMemoryRebound(to: UInt8.self, capacity: data.count) {
let stream = OutputStream(toBuffer: $0, capacity: data.count)
stream.open()
_ = data.withUnsafeBytes {
stream.write($0, maxLength: data.count)
}
stream.close()
}

} catch let error as NSError {
print("ERROR HERE", error.localizedDescription)
}
}

关于swift - 将 pcm 加载到 AVAudioPCMBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41132418/

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