AVAudioConverter压缩/ PCM转换抛出OSStatus -50(kAudio_ParamError)

时间:2016-08-18 19:01:31

标签: ios macos avfoundation core-audio audiotoolbox

我正在尝试使用AVAudioConverter将压缩音频数据(在本例中为mp3,96kbps,CBR,立体声)转换为PCM数据(标准输出格式,通过调用audioEngine.outputNode.outputFormatForBus(0)获得)。我使用AudioFileStream来获取数据包。我之前使用过AudioQueue来播放这些数据包,因此它们是有效的。

这就是我调用convertToBuffer的地方:

let outputBuffer = AVAudioPCMBuffer(PCMFormat: outputFormat, frameCapacity: outputBufferFrameLength)  
outputBuffer.frameLength = outputBufferFrameLength  
var error:NSError?  
audioConverter.convertToBuffer(outputBuffer, error: &error, withInputFromBlock: { (packetCount:AVAudioPacketCount, inputStatus:UnsafeMutablePointer<AVAudioConverterInputStatus>) -> AVAudioBuffer? in  
    return self.getAudioConverterInput(packetCount, inputStatus: inputStatus)  
})  
if let error = error{  
    print("conv error:", error)  
}else{  
    // TODO: Do stuff with buffer  
}  

这是处理输入的函数。

func getAudioConverterInput(packetCount:AVAudioPacketCount, inputStatus:UnsafeMutablePointer<AVAudioConverterInputStatus>) -> AVAudioBuffer?{  
        if let currentAudioFormat = currentAudioFormat, firstPackets = packets.first{  
            inputStatus.memory = .HaveData  
            let buffer = AVAudioCompressedBuffer(format: currentAudioFormat, packetCapacity: packetCount, maximumPacketSize: Int(currentMaximumPacketSize))  
            var currentPackets:Packets = firstPackets  
            var currentStartOffset:Int64 = 0  
            for i in 0..<packetCount{  
                let currentDescription = currentPackets.packetDescriptions[currentPacket]  
                memcpy(buffer.data.advancedBy(Int(currentStartOffset)), currentPackets.data.advancedBy(Int(currentDescription.mStartOffset)), Int(currentDescription.mDataByteSize))  
                buffer.packetDescriptions[Int(i)] = AudioStreamPacketDescription(mStartOffset: currentStartOffset, mVariableFramesInPacket: currentDescription.mVariableFramesInPacket, mDataByteSize: currentDescription.mDataByteSize)  

                currentStartOffset += Int64(currentDescription.mDataByteSize)  
                currentPacket+=1  

                if (currentPackets.numberOfPackets == UInt32(currentPacket)){  
                    currentPacket = 0  
                    packets.removeFirst()  
                    if let firstPackets = packets.first{  
                        currentPackets = firstPackets  
                    }else{  
                        buffer.packetCount = i + 1  
                        return buffer  
                    }  
                }  
            }  
            buffer.packetCount = packetCount  
            return buffer  
        }  
        inputStatus.memory = .NoDataNow  
        return nil  
    }  

每次调用convertToBuffer(...)函数并将packetCount变量设置为“1”时,都会调用此函数一次。抛出错误“conv error:Error Domain = NSOSStatusErrorDomain Code = -50”(null)“”。

我在这里做错了什么?

0 个答案:

没有答案