I am trying to play bytes coming from the UDP from an android device in IOS. I am using TPCircularBuffer to play the bytes. My code is below:
let success = initCircularBuffer(&circularBuffer, 1024)
if success {
print("Circular buffer init was successful")
} else {
print("Circular buffer init not successful")
}
func udpReceive() {
receivingQueue.async {
repeat {
do {
let datagram = try self.tcpClient?.receive()
let byteData = datagram?["data"] as? Data
let dataLength = datagram?["length"] as? Int
self.dataLength = dataLength!
let _ = TPCircularBufferProduceBytes(&self.circularBuffer, byteData!.bytes, UInt32(dataLength! * MemoryLayout<UInt8>.stride * 2))
} catch {
fatalError(error.localizedDescription)
}
} while true
}
}
func consumeBuffer() -> UnsafeMutableRawPointer? {
self.availableBytes = 0
let tail = TPCircularBufferTail(&self.circularBuffer, &self.availableBytes)
return tail
}
We are recording with 16K sample rate and sending to IOS via UDP from Android side and from there we are using AudioUnit to play our bytes but the problem is the crackling and clipping sound in our voice.
Playback Callback code:
func performPlayback(
_ ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
inTimeStamp: UnsafePointer<AudioTimeStamp>,
inBufNumber: UInt32,
inNumberFrames: UInt32,
ioData: UnsafeMutablePointer<AudioBufferList>
) -> OSStatus {
var buffer = ioData[0].mBuffers
let bufferTail = consumeBuffer()
memcpy(buffer.mData, bufferTail, min(self.dataLength, Int(availableBytes)))
buffer.mDataByteSize = UInt32(min(self.dataLength, Int(availableBytes)))
TPCircularBufferConsume(&self.circularBuffer, UInt32(min(self.dataLength, Int(availableBytes))))
return noErr
}
UDP is sending us 1280 bytes per sample. What we think the problem is the BUFFER SIZE that is not being set correctly. Can anyone guide me how to set proper buffer size. It would be great help indeed. I know the work of @Gruntcakes as an voip engineer https://stackoverflow.com/a/57136561/12020007. I have also studied the work of @hotpaw2 and was looking at https://stackoverflow.com/a/58545845/12020007 to check if there is some threading issue. Any kind of help would be appreciated.