gpt4 book ai didi

swift - 3D 定位音频——沿 Y 轴和 Z 轴移动 SCNAudioPlayer

转载 作者:行者123 更新时间:2023-11-28 07:23:23 24 4
gpt4 key购买 nike

使用 SceneKit,我可以在 x 轴上从左向右移动 audioNode,但在 y 和 z 轴上移动时遇到问题。我戴着耳机,所以我可以听到双耳(3d 音频)效果。我也在 MacOS 上运行它。

我的测试代码如下。有人能让我知道我错过了什么吗?我将不胜感激!

import Cocoa
import SceneKit

class ViewController: NSViewController {

@IBOutlet weak var sceneView: SCNView!

override func viewDidLoad() {
super.viewDidLoad()

let path = Bundle.main.path(forResource: "Sounds/Test.mp3",
ofType: nil)

let url = URL(fileURLWithPath: path!)
let source = SCNAudioSource(url:url)!
source.loops = true
source.shouldStream = false
source.isPositional = true
source.load()

let player = SCNAudioPlayer(source: source)
let box = SCNBox(width: 100.0,
height: 100.0,
length: 100.0,
chamferRadius: 100.0)
let boxNode = SCNNode(geometry: box)
let audioNode = SCNNode()
boxNode.addChildNode(audioNode)

let scene = SCNScene()
scene.rootNode.addChildNode(boxNode)
sceneView.scene = scene
audioNode.addAudioPlayer(player)

let avm = player.audioNode as! AVAudioMixing
avm.volume = 1.0
let up = SCNAction.moveBy(x: 0, y: 100, z: 0, duration: 5)
let down = SCNAction.moveBy(x: 0, y: -100, z: 0, duration: 5)
let sequence = SCNAction.sequence([up, down])
let loop = SCNAction.repeatForever(sequence)
boxNode.runAction(loop)

// Do any additional setup after loading the view.
}
}

最佳答案

已更新

您正在将 player.audioNode 转换为 AVAudioMixing 协议(protocol):

let avm = player.audioNode as! AVAudioMixing

但是您必须将它转换为一个类,而不是它。代码如下所示:

let avm = player.audioNode as? AVAudioEnvironmentNode

Any node that conforms to the AVAudioMixing protocol (for example, AVAudioPlayerNode) can act as a source in this environment. The environment has an implicit listener. By controlling the listener’s position and orientation, the application controls the way the user experiences the virtual world. This node also defines properties for distance attenuation and reverberation that help characterize the environment.

And take into account !

Only inputs with a mono channel connection format to the environment node are spatialized. If the input is stereo, the audio is passed through without being spatialized. Inputs with connection formats of more than two channels aren't supported.

当然,您需要实现 AVAudio3DMixing协议(protocol)

这是一个工作代码:

import SceneKit
import AVFoundation

class ViewController: NSViewController, AVAudio3DMixing {

// YOU NEED MONO AUDIO !!!

var renderingAlgorithm = AVAudio3DMixingRenderingAlgorithm.sphericalHead
var rate: Float = 0.0
var reverbBlend: Float = 0.5
var obstruction: Float = -100.0
var occlusion: Float = -100.0
var position: AVAudio3DPoint = AVAudio3DPoint(x: 0, y: 0, z: -100)

override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()

let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.camera?.zFar = 200
cameraNode.position = SCNVector3(x: 0, y: 0, z: 40)
scene.rootNode.addChildNode(cameraNode)

let sceneView = self.view as! SCNView
sceneView.scene = scene
sceneView.backgroundColor = NSColor.black
sceneView.autoenablesDefaultLighting = true

let path = Bundle.main.path(forResource: "Test_Mono", ofType: "mp3")
let url = URL(fileURLWithPath: path!)
let source = SCNAudioSource(url: url)!
source.loops = true
source.shouldStream = false // MUST BE FALSE
source.isPositional = true
source.load()

let player = SCNAudioPlayer(source: source)
let audioNode = SCNNode()

let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.2)
let boxNode = SCNNode(geometry: box)

boxNode.addChildNode(audioNode)
scene.rootNode.addChildNode(boxNode)

audioNode.addAudioPlayer(player)
let avm = player.audioNode as? AVAudioEnvironmentNode
avm?.reverbBlend = reverbBlend
avm?.renderingAlgorithm = renderingAlgorithm
avm?.occlusion = occlusion
avm?.obstruction = obstruction

let up = SCNAction.moveBy(x: 0, y: 0, z: 70, duration: 5)
let down = SCNAction.moveBy(x: 0, y: 0, z: -70 , duration: 5)
let sequence = SCNAction.sequence([up, down])
let loop = SCNAction.repeatForever(sequence)
boxNode.runAction(loop)

avm?.position = AVAudio3DPoint(
x: Float(boxNode.position.x),
y: Float(boxNode.position.y),
z: Float(boxNode.position.z))
}
}

关于swift - 3D 定位音频——沿 Y 轴和 Z 轴移动 SCNAudioPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com