Using AudioKit and SpriteKit audio simultaneously

503 views Asked by At

I'm building a game that uses the AudioKit framework to detect the frequency of sound received by the mic. I set it up as follows:

import SpriteKit
import AudioKit

class GameScene: SKScene {
    var mic : AKMicrophone!
    var tracker : AKFrequencyTracker!
    var silence : AKBooster!
    let mixer = AKMixer()

    override func didMove(to view: SKView) {
       mic = AKMicrophone()
       tracker = AKFrequencyTracker.init(mic)
       silence = AKBooster(tracker, gain: 0)
       mixer.connect(silence)
       AudioKit.output = mixer
       AudioKit.start()
    }
}

I would also like to use SKAction.playAudioFileNamed for the playback of sound effects etc, but when I use it, the playback volume is very low. I assume it has something to do with the scene's mixer node and the AKMixer? Playing sound files using AudioKit is far more complicated than I need.

Do I need to make an extension of SKScene? Help would be very much appreciated!

1

There are 1 answers

0
derekFairholm On BEST ANSWER

It seems that Aurelius was correct in that the AudioSession output route was being directed to the headset. I'm still not sure why this is was the case, but overriding and setting the output worked as follows:

        let session = AVAudioSession()

    do {
        try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    } catch {
        print("error setting output")
    }

This needs to be done after initializing AudioKit components. If there's a better way of doing this, please let me know!