我的情况是需要导出使用 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
                    }
                }
            }
但是,我一直收到以下错误:
异常(-50):“缓冲区数量错误”
[avae] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioFile.mm:445:-[AVAudioFile readIntoBuffer:frameCount:error:]: (ExtAudioFileRead(_imp->_extAudioFile, &ioFrames, buffer.mutableAudioBufferList)): 错误 -50
这与导出和导入音频时使用的文件格式无关。
但是,如果我使用从应用程序内部直接读取的这个 .wav 文件,它确实可以正常工作。
有没有人知道为什么我似乎无法将音频文件中的数据读入AVPCMBuffer.