0

I'm trying to play positional audio using the front and back channels in Sprite Kit, and testing on an Apple TV device.

I'm using the following code:

    let musicURL = NSBundle.mainBundle().URLForResource("music", withExtension: "m4a")
    let music = SKAudioNode(URL: musicURL!)

    addChild(music)

    music.positional = true
    music.position = CGPoint(x: 0, y: 0)

    let moveForward = SKAction.moveToY(1024, duration: 2)
    let moveBack = SKAction.moveToY(-1024, duration: 2)


    let sequence = SKAction.sequence([moveForward, moveBack])
    let repeatForever = SKAction.repeatActionForever(sequence)

    music.runAction(repeatForever)

What I want to accomplish is a sound that pans from the front to the back channels but Sprite Kit seems to be using just the 2 channel stereo output.

If I use moveToX instead of moveToY I get a sound panning from left to right.

I'm surely missing some initialization code to signal I want a 5.1 sound output, but I'm not sure if the SKAudioNode positional feature only works for 2 channel stereo output.

Is positional audio with more than 2 channels achievable in Sprite Kit or should I resort to AVFoundation or even OpenAL for this?

I have tried similar code with SceneKit and it seems that it also uses only 2 channels for positional audio.

xpereta
  • 692
  • 9
  • 21
  • From what I have read in the documentation the reason may be that the underlaying AVAudioEnvironmentNode is not configured with the proper audio channel layout. – xpereta Aug 03 '16 at 06:52

1 Answers1

0

A sound can't be positioned in 3D space using SceneKit. You should not use an SKAudioNode but use AVFoundation directly to play the sound.

First you have to setup the audio session to use a 5.1 channel output layout:

let session = AVAudioSession.sharedInstance()  
session.setCategory(AVAudioSessionCategoryPlayback)  
session.setActive(true)  
session.setPreferredOutputNumberOfChannels(6)

And then wire an AVAudioEnvironmentNode setup to output the 6 output channels.

A starting point can be found in this existing answer:

https://stackoverflow.com/a/35657416/563802

Community
  • 1
  • 1
xpereta
  • 692
  • 9
  • 21