How to overlap sounds of different pitch?

So, I'm trying to make a simple game with a hit sound that has a different pitch whenever you hit something. I thought it'd be simple, but it ended up with a whole lot of stuff (most of which I don't understand and that I completely copied from someone else):

func hitSound(value: Float) {
   
    let audioPlayerNode = AVAudioPlayerNode()
   
    audioPlayerNode.stop()
    engine.stop() // This is an AVAudioEngine defined previously
    engine.reset()
   
    engine.attach(audioPlayerNode)
   
    let changeAudioUnitTime = AVAudioUnitTimePitch()
    changeAudioUnitTime.pitch = value
   
    engine.attach(changeAudioUnitTime)
    engine.connect(audioPlayerNode, to: changeAudioUnitTime, format: nil)
    engine.connect(changeAudioUnitTime, to: engine.outputNode, format: nil)
    audioPlayerNode.scheduleFile(file, at: nil, completionHandler: nil) // File is an AVAudioFile defined previously
    try? engine.start()
   
    audioPlayerNode.play()
}

My question is, since this code seems to stop playing any sounds currently being played in order to play the new sound, is there a way I can alter this behaviour so it doesn't stop playing anything? I tried removing the engine.stop and engine.reset bits, but this just crashes the app. Also, this code is incredibly slow when called frequently. Is there something I could do to speed it up? This hitsound is needed very frequently.

Replies

Still no replies, huh?


Anyone know a forum for random Xcode questions like this that might get faster responses? I feel like this one is a bit slower than most

Alright, I posted this on Stack Overflow and got an answer. I think I'll use that from now on