Search code examples
crashavfoundationswift3avaudioengine

setting AVAudioFormat to connect function has crash


I have been trying play with AVAudioEngine for singed 16 bit stream data.

But pass to AVAudioFormat connect function always make crash.

codes like below:

let AUDIO_OUTPUT_SAMPLE_RATE    = 44100
let AUDIO_OUTPUT_CHANNELS       = 2
let AUDIO_OUTPUT_BITS           = 16
var audioEngine: AVAudioEngine?
var audioPlayer: AVAudioPlayerNode?

...

    audioEngine = AVAudioEngine()
    audioPlayer = AVAudioPlayerNode()

    audioEngine?.attach(audioPlayer!)

    let mixer = audioEngine?.mainMixerNode
    mixer!.outputVolume = 1.0
    let stereoFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: Double(AUDIO_OUTPUT_SAMPLE_RATE), channels: 2, interleaved: false)
    audioEngine!.connect(audioPlayer!, to: mixer!, format: stereoFormat)

...

audioEngine!.connect(...) is crashing line

I'm using Xcode 8 beta 6, OS X El Capitan, And, this happening arise from both of simulator and devices.

This is part of crash message:

ERROR:    >avae> AVAudioNode.mm:751: AUSetFormat: error -10868
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -10868'

...
3   AVFAudio  0x000000011e0a5630 _Z19AVAE_RaiseExceptionP8NSStringz + 176
4   AVFAudio  0x000000011e0f270d _ZN19AVAudioNodeImplBase11AUSetFormatEP28OpaqueAudioComponentInstancejjP13AVAudioFormat + 213
5   AVFAudio  0x000000011e0f2630 _ZN19AVAudioNodeImplBase15SetOutputFormatEmP13AVAudioFormat + 46
6   AVFAudio  0x000000011e0f9663 _ZN21AVAudioPlayerNodeImpl15SetOutputFormatEmP13AVAudioFormat + 25
7   AVFAudio  0x000000011e099cfd _ZN18AVAudioEngineGraph8_ConnectEP19AVAudioNodeImplBaseS1_jjP13AVAudioFormat + 2377
8   AVFAudio  0x000000011e09d15f _ZN18AVAudioEngineGraph7ConnectEP11AVAudioNodeS1_mmP13AVAudioFormat + 355
9   AVFAudio  0x000000011e0fc80e _ZN17AVAudioEngineImpl7ConnectEP11AVAudioNodeS1_mmP13AVAudioFormat + 348

It's no problem playing with buffer and format from audio file.

What is mistake(s) I do?

Thanks.


Solution

  • -10868, a.k.a. kAudioUnitErr_FormatNotSupported, so it looks like your PCMFormatInt16 isn't appreciated. Changing it to .PCMFormatFloat32 does work.