简体   繁体   English

使用AudioEngine的声音效果

[英]Using sound effects with AudioEngine

Background - I saw a video titled "AVAudioEngine in Practice" from the following list of videos published at Apple's recent WWDC to apply sound effects to an audio. 背景 - 我在Apple最近的WWDC发布的视频列表中看到了一个名为“AVAudioEngine in Practice”的视频,用于将音效应用于音频。 https://developer.apple.com/videos/wwdc/2014/ https://developer.apple.com/videos/wwdc/2014/

After that, I was successfully able to change the pitch of an audio with the following code: 之后,我成功地使用以下代码更改了音频的音高:

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

    }

From what I understand, I used the AudioEngine to attach the AudioPlayerNode with the AudioEffect, which I in turn attached to the Output. 据我所知,我使用AudioEngine将AudioPlayerNode与AudioEffect连接,而AudioEffect又连接到Output。

I am now curious about adding multiple sound effects to the audio. 我现在很想知道为音频添加多种音效。 For instance, pitch change AND reverb. 例如,音高变化和混响。 How would I go about adding multiple sound effects to the audio? 我该如何为音频添加多种音效?

Also, would it make sense to attach and connect the nodes in viewDidLoad rather than how I have done it here in an IBAction ? 另外,在viewDidLoad中附加和连接节点是否有意义,而不是我在IBAction中如何完成它?

Just connect them. 只需连接它们。

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)

Background - I saw a video titled "Putting it all together - Intro to iOS App Development with Swift" from the following list of videos published at Udacity to apply sound effects to an audio. 背景 - 我从Udacity发布的以下视频列表中看到了一个标题为“全部放在一起 - 使用Swift进行iOS应用程序开发的介绍”的视频,以将音效应用于音频。

https://youtu.be/XiQfjaYJjuQ https://youtu.be/XiQfjaYJjuQ

After that, I was successfully able to change the pitch of an audio with the following code: 之后,我成功地使用以下代码更改了音频的音高:

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM