Using sound effects with AudioEngine
Asked Answered
M

2

24

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. 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.

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 ?

Misha answered 15/8, 2014 at 19:44 Comment(0)
S
13

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)
Sottish answered 9/9, 2014 at 11:39 Comment(3)
everything seems connected. why do you suggest that code?Lehrer
In the OP's example given, multiple nodes were not connected. "I am now curious about adding multiple sound effects" so I gave him the answer. Thanks for downvoting my correct answer.Sottish
Excuse me. Missed that.Lehrer
S
7

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.

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

    }
Suspensory answered 1/12, 2015 at 8:21 Comment(1)
How to set the current time for audio engine or player node?Larkspur

© 2022 - 2024 — McMap. All rights reserved.