【发布时间】:2018-11-09 22:32:18
【问题描述】:
我需要导出使用 AudioKit 录制的音频文件,然后稍后重新导入以通过AVAudioPCMBuffer 进行一些处理。
以下是我用于从 AudioKit 导出的代码:
tape = recorder.audioFile!
player.load(audioFile: tape)
if let _ = player.audioFile?.duration {
recorder.stop()
tape.exportAsynchronously(name: "TempTestFile",
baseDir: .documents,
exportFormat: .caf) {_, exportError in
if let error = exportError {
AKLog("Export Failed \(error)")
} else {
AKLog("Export succeeded")
}
}
}
这是我稍后尝试将该音频文件读回我的 macOS 应用程序的地方,并填写 AVAudioPCMBuffer(其中file 是我要读取的音频文件):
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: Double(sampleRate), channels: AVAudioChannelCount(channels), interleaved: interleaved)
if let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: AVAudioFrameCount(file.length)){
if ((try? file.read(into: buffer)) != nil) {
let arraySize = Int(buffer.frameLength)
switch type {
case is Double.Type:
let doublePointer = UnsafeMutablePointer<Double>.allocate(capacity: arraySize)
vDSP_vspdp(buffer.floatChannelData![0], 1, doublePointer, 1, vDSP_Length(arraySize))
return Array(UnsafeBufferPointer(start: doublePointer, count:arraySize)) as? [T]
case is Float.Type:
return Array(UnsafeBufferPointer(start: buffer.floatChannelData![0], count:arraySize)) as? [T]
default: return nil
}
}
}
但是,我一直收到以下错误:
EXCEPTION (-50):“缓冲区数量错误”
[avae] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioFile.mm:445:-[AVAudioFile readIntoBuffer:frameCount:error:]: (ExtAudioFileRead(_imp->_extAudioFile, &ioFrames, buffer.mutableAudioBufferList)): 错误 -50
这与导出和导入音频时使用的文件格式无关。
但是,如果我使用从应用程序内部直接读取的this .wav file,它确实可以正常工作。
有没有人知道为什么我似乎无法将音频文件中的数据读入AVPCMBuffer。
【问题讨论】:
标签: swift avaudioplayer audiokit avaudiopcmbuffer