3

i have app decoding stream audio over network from opus to pcm, the out data is PCM 16 bit, i decode it successfully, but when i try to play it using AVAudioEngine it fail, i can not change the sample rate from 44100 to 48000 which opus generate.

this is part of code that i use to play the audio

self.self.audioEngine  = AVAudioEngine()
self.player  = AVAudioPlayerNode() 
self.downMixer      = AVAudioMixerNode()

self.audioEngine!.attach(downMixer!)
self.audioEngine!.attach(player!)

 let format48KHzMono = AVAudioFormat.init(commonFormat: AVAudioCommonFormat.pcmFormatInt16 , sampleRate: 48000.0, channels: 1, interleaved: true)

self.audioEngine!.connect(downMixer!, to: audioEngine!.outputNode, format: format48KHzMono)//use new audio format
self.audioEngine!.connect(downMixer!, to: audioEngine!.mainMixerNode  , format: format48KHzMono)
self.audioEngine!.connect(player!, to: audioEngine!.outputNode , format: player!.outputFormat(forBus: 0))  

downMixer!.outputVolume = 0.0

audioEngine!.prepare()
try! audioEngine!.start()       
try! self.player?.play()

this is method where i convert the pcm data to AVAudioPCMBuffer

   func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer {
        let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16 , sampleRate: 48000.0, channels: 1, interleaved: true)  // given NSData audio format
        let PCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat!, frameCapacity: UInt32(data.length) / audioFormat!.streamDescription.pointee.mBytesPerFrame )
        PCMBuffer!.frameLength = PCMBuffer!.frameCapacity
        let channels = UnsafeBufferPointer(start: PCMBuffer?.int16ChannelData, count: Int(PCMBuffer!.format.channelCount))
        data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.length)
        return PCMBuffer!
     }

and in the loop i use this

if let decodedDataChunk = OpusKit.shared.decodeData(frame) {
 let pcmData:AVAudioPCMBuffer = self.toPCMBuffer(data: decodedDataChunk as NSData)                               
 self.player?.scheduleBuffer(pcmData)
}
Ahmed
  • 112
  • 2
  • 12
  • What do you want your audio processing graph to look like? I think you want player -> down mixer -> main mixer -> output but you're connecting down mixer to both output and main mixer and then connecting the player to output, bypassing the mixers. – sbooth May 21 '20 at 11:35
  • i have pcmFormatInt16 audio output from opus codec , but when i run above code it's not working – Ahmed May 21 '20 at 13:05

0 Answers0