Post not yet marked as solved
Post marked as unsolved with 0 replies, 689 views
Hi, i have voice chat application, and i use AVAudioEngine to play audio after decode it, i use opus codec, from library called OpusKit and it's decode the audio fine, but when i get PCM audio and schedule it to AVAudioPlayerNode i hear Intermittent sound , not really clear, and i do not know, why this issue happend, there is no error or somthing happend.the main process that i do is,1 - recive commpressed audio packet from stream ( Network )2- get the data from stream and pass it to opus decoder3- get raw pcm Float32 pcm format like thisAVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32 , sampleRate: 48000.0, channels: 1 , interleaved: false)4- add the decoded packers to array list5- in loop i get elemnts one by one, then pass it to AVAudioPlayerNode , and this is part of codelet pcmData:AVAudioPCMBuffer = self.toPCMBuffer(data: decodedDataChunk as NSData)
self.player?.scheduleBuffer(pcmData)this is the function i use to do this task:func openOpus(){
OpusKit.shared.initialize(sampleRate: self.sample_rate, numberOfChannels: self.channel, packetSize: self.packetSize, encodeBlockSize: self.encodeBlockSize)
audioEngine = AVAudioEngine()
self.player = AVAudioPlayerNode()
var inputNode = audioEngine!.inputNode
var outputNode = audioEngine!.outputNode
let main = audioEngine!.mainMixerNode
audioEngine!.attach(player!)
//let's get the input audio format right as it is
let format = inputNode.inputFormat(forBus: 0)
//I initialize a 48KHz format I need:
let format48KHzMono = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32 , sampleRate: 48000.0, channels: 1 , interleaved: false)
audioEngine!.connect(self.player! , to: audioEngine!.mainMixerNode, format: format48KHzMono)
audioEngine!.prepare()
try! audioEngine!.start()
try! self.player?.play()
DispatchQueue.init(label: "play audio").async {
self.play_audio()
}
}and this is the loop functionfunc play_audio(){
while self.room_oppened {
var frame: Data?
self.serialAudioQueue.sync {
if self.audio_queue.isEmpty == false{
frame = self.audio_queue.removeFirst() // block this part of code to get audio frame from array
}
}
// FIXME: calculate the jitter bufer
if frame != nil && self.mutted == false{
// do the audio decoding and paly it
if let decodedDataChunk = OpusKit.shared.decodeData(frame!) {
//print("we decode it ...")
let pcmData:AVAudioPCMBuffer = self.toPCMBuffer(data: decodedDataChunk as NSData)
self.player?.scheduleBuffer(pcmData)
} else {
print("failed to decode ")
}
let ms = 1000
usleep(useconds_t(32 * ms)) //will sleep for 2 milliseconds (.002 seconds)
}else{
let ms = 1000
usleep(useconds_t(240 * ms)) //will sleep for 2 milliseconds (.002 seconds)
}
}
}