Martin Ma
Martin Ma

Reputation: 85

Swift: How to make positional audio more realistic for sound localization?

I am coding some application about sound localization and I used.isPositional in SCNAudioPlayerto produce spatial audio on a SCNNode. Here is what my codes like:

func spatialAudio(inputNode: SCNNode){
    let audio = SCNAudioSource(fileNamed: "source.wav")
    audio!.isPositional = true
    audio?.load()
    let player = SCNAudioPlayer(source: audio!)
    inputNode.addAudioPlayer(player)
}

But the outcome of the spatial audio isn't quite well that it is difficult for user to localize the sound source. Is there any other way to produce more realistic spatial audio in Swift?

I have heard of some algorithms used in sound localization, like head-related transfer function (HRTF), interaural time difference (ITD), and interaural level difference (ILD). Does Swift's positional audio use these algorithms? If it doesn't, how could I alter the audio to use these algorithms?

Upvotes: 1

Views: 532

Answers (1)

Ash
Ash

Reputation: 9351

For me, the problem was basically that sound attenuation was far too weak. I could move ten or twenty metres away from the source and it would be almost as loud as when I was right on top of it. This made the 3D audio effect very difficult to determine. My solution was to change the settings on the audio environment node. You can find this on your SCNView - it's called audioEnvironmentNode.

I'm finding that the following settings provide a pretty strong effect, but you may want to play around with them to get the effect that's right for your circumstances:

view.audioEnvironmentNode.distanceAttenuationParameters.maximumDistance = 2
view.audioEnvironmentNode.distanceAttenuationParameters.referenceDistance = 0.1
view.audioEnvironmentNode.renderingAlgorithm = .HRTFHQ

(If your 3D scenes are particularly computationally expensive, you might want to use HRTF rather than HRTFHQ, or even some other less accurate but more performant algorithm. In my situation, audio quality was more important than graphical.)

Upvotes: 1

Related Questions