使用AudioEngine播放声音效果

23

背景 - 我看了苹果最近WWDC发布的视频列表中标题为“AVAudioEngine实践”的视频,以将音效应用于音频。

https://developer.apple.com/videos/wwdc/2014/

之后,我成功地使用以下代码更改了音频的音调:

 //Audio Engine is initialized in viewDidLoad()
 audioEngine = AVAudioEngine()
 //The following Action is called on clicking a button
 @IBAction func chipmunkPlayback(sender: UIButton) {
        var pitchPlayer = AVAudioPlayerNode()
        var timePitch = AVAudioUnitTimePitch()
        timePitch.pitch = 1000

        audioEngine.attachNode(pitchPlayer)
        audioEngine.attachNode(timePitch)

        audioEngine.connect(pitchPlayer, to: timePitch, format: myAudioFile.processingFormat)
        audioEngine.connect(timePitch, to: audioEngine.outputNode, format: myAudioFile.processingFormat)

        pitchPlayer.scheduleFile(myAudioFile, atTime: nil, completionHandler: nil)
        audioEngine.startAndReturnError(&er)

        pitchPlayer.play()

    }

据我所了解,我使用AudioEngine将AudioPlayerNode与AudioEffect连接,并将其附加到输出。

现在,我想尝试给音频添加多个声音效果。例如,改变音高和混响。如何添加多个声音效果到音频中?

此外,将节点附加和连接到viewDidLoad而不是像我在这里的IBAction中完成,这样做有意义吗?

2个回答

13

只需要将它们连接起来。

engine.connect(playerNode, to: reverbNode, format: format)
engine.connect(reverbNode, to: distortionNode, format: format)
engine.connect(distortionNode, to: delayNode, format: format)
engine.connect(delayNode, to: mixer, format: format)

一切似乎都联系在一起了。你为什么建议使用那段代码? - Ilya
在OP提供的示例中,多个节点未连接。 "我现在很想添加多个音效",所以我给了他答案。感谢您对我的正确答案进行投票。 - Gene De Lisa
对不起,我没听清楚。 - Ilya

6
背景 - 我看了Udacity发布的一系列视频中标题为“整合iOS应用程序开发和Swift”的视频,旨在将声音效果应用于音频。 https://youtu.be/XiQfjaYJjuQ 之后,我成功地用以下代码改变了音频的音调:
func playAudioWithVariablePith(pitch: Float){
        audioPlayer.stop()
        audioEngine.stop()
        audioEngine.reset()

        let audioPlayerNode = AVAudioPlayerNode()
        audioEngine.attachNode(audioPlayerNode)

        let changePitchEffect = AVAudioUnitTimePitch()
        changePitchEffect.pitch = pitch

        audioEngine.attachNode(changePitchEffect)

        audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)

        audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)

        audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)

        try! audioEngine.start()

        audioPlayerNode.play()

    }

如何为音频引擎或播放器节点设置当前时间? - Bharathi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接