SKAudioNode .isPositional property on buffered audio

Hi everyone,

I am currently trying to use the SKAudioNode .isPositional property in order to have a positional audio feedback for the nodes audio source.

While this is easy to accomplish just loading in a file as SKAudioNode source, I am having a hard time doing so when I am buffering the audio via an AVAudioPlayerNode.

In general the setup is working to load and play audio, but I am not sure if I correctly setup the EnvironmentNode in order to use the .isPositional property.

Any thoughts and help is highly appreciated, here is my code:

class GameScene: SKScene {
    var audioFilePlayer = AVAudioPlayerNode()
    var envNode: AVAudioEnvironmentNode!;
    var bufferFormat = AVAudioPCMBuffer()

override func didMove(to view: SKView) {
        let player = SKSpriteNode(imageNamed: "player")

        player.position =  CGPoint(x:0,y:0)

        scene?.addChild(player)

    

       

        guard let filePath = Bundle.main.path(forResource: "bass2", ofType: "m4a") else{ return }



        let fileURL = URL(fileURLWithPath: filePath)

        var backgroundMusic = SKAudioNode()

        player.addChild(backgroundMusic)

        do {

            let audioFile = try AVAudioFile(forReading: fileURL)



            let audioFormat = audioFile.processingFormat

            let audioFrameCount = UInt32(audioFile.length)

            guard let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount) else { return }

            try audioFile.read(into: audioFileBuffer)



            // connect the nodes, and use the data to play

            let mainMixer = audioEngine.mainMixerNode

            backgroundMusic.avAudioNode = audioFilePlayer

            envNode = AVAudioEnvironmentNode();

            self.audioEngine.attach(envNode);

            scene?.audioEngine.attach(backgroundMusic.avAudioNode!)

            scene!.audioEngine.connect(backgroundMusic.avAudioNode!, to: envNode, format: audioFileBuffer.format)

            scene!.audioEngine.connect(envNode, to: mainMixer, format: audioFileBuffer.format)

        



            try scene?.audioEngine.start()



            audioFilePlayer.play()

            audioFilePlayer.scheduleBuffer(audioFileBuffer, completionHandler: nil)

            

        } catch {

            print(error)

        }

        backgroundMusic.position = CGPoint(x:0,y:0)

        label?.addChild(backgroundMusic)

    }

Basically any way of using a AVAudioPlayerNode as audio source for a SKAudioNode would help me tons as this appears to be the only way to buffer audio to SKAudioNode.

I hope to accomplish a setup in which I can use the .isPositional property of SKAudioNode.

Thank you in advance.

SKAudioNode .isPositional property on buffered audio
 
 
Q