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)
}