将pcm加载到AVAudioPCMBuffer [英] load a pcm into a AVAudioPCMBuffer
本文介绍了将pcm加载到AVAudioPCMBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
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
And I get the following error: 1954115647
which is: kAudioFileUnsupportedFileTypeError
我的A4.f32文件包含PCM浮点32.这里缺少什么吗?我的文件中没有标题,是否可能导致此问题?
My A4.f32 file contains a PCM float 32. Is there something I'm missing here? There's no header in my file, is it something that could lead to this problem?
推荐答案
感谢Rhythmic Fistman的评论,我亲自去加载了它,
Thanks to Rhythmic Fistman in the comments, I went and loaded it myself, that way:
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)
}
}
这篇关于将pcm加载到AVAudioPCMBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文