'How to access distanceAttenuationParameters for an SKAudioNode
I have an SKAudioNode in my scene but the audio from it is not falling off with range as much as I would like (my scene is very large, and when this node is a very long way away it is still clearly audible. Reducing its volume will just then make it too quite when it is at close range - I need to change the fall-off in volume with range).
I can see from the documentation that the parameters I need to adjust are the distanceAttenuationParameters in AVAudioEnvironmentNode.
When I look at SKScene I can see that it has an audioEngine AVAudioEngine property that in turn exposes a mainMixerMode AVAudioMixer property. But in none of these places can I find a handle to an AVAudioEnvironmentNode.
So how can I access the parameters needed to make my node's audio behave correctly in a large scene?
Solution 1:[1]
You can find the AVAudioEnvironmentNode in the audioEngine's attachedNodes array. Here is an extension of the SKScene that adds a computed property to directly access it from any SKScene.
import Foundation
import SpriteKit
import AVFoundation
extension SKScene {
var audioEnvironmentNode : AVAudioEnvironmentNode? {
audioEngine.attachedNodes.first(where: {audioNode in
audioNode is AVAudioEnvironmentNode
}) as? AVAudioEnvironmentNode
}
}
Example:
if let environmetNode = scene.audioEnvironmentNode {
environmetNode.distanceAttenuationParameters.distanceAttenuationModel = .linear
environmetNode.distanceAttenuationParameters.referenceDistance = 400
environmetNode.distanceAttenuationParameters.maximumDistance = 1500
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Double M |
