This seems to be incorrectly connected. I just learn all this myself, but found that the effects were added correctly when you connect them to the node mixer. Also, you want to click the mixer, not the output of the node engine. I just copied your code and made a few changes to take this into account.
private func playAudio(pitch : Float, rate: Float, reverb: Float, echo: Float) { // Initialize variables audioEngine = AVAudioEngine() audioPlayerNode = AVAudioPlayerNode() audioEngine.attachNode(audioPlayerNode) // Setting the pitch let pitchEffect = AVAudioUnitTimePitch() pitchEffect.pitch = pitch audioEngine.attachNode(pitchEffect) // Setting the playback-rate let playbackRateEffect = AVAudioUnitVarispeed() playbackRateEffect.rate = rate audioEngine.attachNode(playbackRateEffect) // Setting the reverb effect let reverbEffect = AVAudioUnitReverb() reverbEffect.loadFactoryPreset(AVAudioUnitReverbPreset.Cathedral) reverbEffect.wetDryMix = reverb audioEngine.attachNode(reverbEffect) // Setting the echo effect on a specific interval let echoEffect = AVAudioUnitDelay() echoEffect.delayTime = NSTimeInterval(echo) audioEngine.attachNode(echoEffect) // Set up a mixer node let audioMixer = AVAudioMixerNode() audioEngine.attachNode(audioMixer) // Chain all these up, ending with the output audioEngine.connect(audioPlayerNode, to: playbackRateEffect, format: nil) audioEngine.connect(playbackRateEffect, to: pitchEffect, format: nil) audioEngine.connect(pitchEffect, to: reverbEffect, format: nil) audioEngine.connect(reverbEffect, to: echoEffect, format: nil) audioEngine.connect(echoEffect, to: audioMixer, format: nil) audioEngine.connect(audioMixer, to: audioEngine.outputNode, format: nil) audioPlayerNode.stop() let length = 4000 let buffer = AVAudioPCMBuffer(PCMFormat: audioPlayerNode.outputFormatForBus(0),frameCapacity:AVAudioFrameCount(length)) buffer.frameLength = AVAudioFrameCount(length) try! audioEngine.start() let dirPaths: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] let tmpFileUrl: NSURL = NSURL.fileURLWithPath(dirPaths.stringByAppendingPathComponent("effectedSound.m4a")) do{ print(dirPaths) let settings = [AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC), AVSampleRateKey: NSNumber(integer: 44100), AVNumberOfChannelsKey: NSNumber(integer: 2)] self.newAudio = try AVAudioFile(forWriting: tmpFileUrl, settings: settings) audioMixer.installTapOnBus(0, bufferSize: (AVAudioFrameCount(self.player!.duration)), format: self.audioMixer.outputFormatForBus(0)){ (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) in print(self.newAudio.length) print("=====================") print(self.audioFile.length) print("**************************") if (self.newAudio.length) < (self.audioFile.length){ do{ //print(buffer) try self.newAudio.writeFromBuffer(buffer) }catch _{ print("Problem Writing Buffer") } }else{ self.audioMixer.removeTapOnBus(0) } } }catch _{ print("Problem") } audioPlayerNode.play() }
I also failed to format the file correctly. I finally got it working when I changed my path to the output file from m4a to caf . Another suggestion is to not have nil for the format parameter. I used audioFile.processingFormat . Hope this helps. My sound effects / mixes are functional, although I did not link my effects. So feel free to ask questions.
source share