AVAudioConverter Compressed to PCM conversion throwing OSStatus -50 (kAudio_ParamError)

I'm trying to use AVAudioConverter to convert compressed audio data (in this case mp3, 96kbps, CBR, stereo) into PCM data (the standard output format, obtained by calling audioEngine.outputNode.outputFormatForBus(0)). I use an AudioFileStream to get the packets. I have previously used an AudioQueue to play these packets, so they are valid.


This is where I call 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
}


And this is the function that handles the input.

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
    }

This function is called once every time I call the convertToBuffer(...) function with the packetCount variable set to '1'. The error "conv error: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"" is thrown.


What am I doing wrong here?

Replies

I'm seeing the exact same issue on iOS 10.2. I've asked DTS for help about this. Hoping they can help shed some light. If all else fails, we may have to switch back to using the C based APIs from AudioConverter to work around the issues.

FYI for anyone following along, comments added to < https://forums.developer.apple.com/thread/65901>