使用AudioEngine的声音效果

时间:2014-08-15 19:44:54

标签: ios swift avaudioengine avaudioplayernode

背景 - 我在实践中看到了一个标题为" AVAudioEngine"从Apple最近发布的WWDC上发布的以下视频列表中,将声音效果应用于音频。 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连接,而AudioEffect又连接到输出。

我现在很想知道为音频添加多种音效。例如,音高变化和混响。我该如何为音频添加多种音效?

此外,在viewDidLoad中附加和连接节点是否有意义,而不是我在IBAction中如何完成它?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:5)

背景 - 我看到一个标题为“#34;全部放在一起的视频 - 使用Swift介绍iOS应用程序开发"从Udacity发布的以下视频列表中将声音效果应用于音频。

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()

    }