Pitch Shifting in Real Time With AVAudioEngine using Swift
timePitch.pitch = -500 //Rude man voicetimePitch.rate = 1.5 //In 1.5 times faster
Check this tutorial. And direct link to example from tutorial for more info.
Example for Swift 2.0:
import UIKitimport AVFoundationclass ViewController: UIViewController { var engine: AVAudioEngine! var player: AVAudioPlayerNode! var file = AVAudioFile() override func viewDidLoad() { super.viewDidLoad() engine = AVAudioEngine() player = AVAudioPlayerNode() player.volume = 1.0 let path = NSBundle.mainBundle().pathForResource("in", ofType: "caf")! let url = NSURL.fileURLWithPath(path) let file = try? AVAudioFile(forReading: url) let buffer = AVAudioPCMBuffer(PCMFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length)) do { try file!.readIntoBuffer(buffer) } catch _ { } let pitch = AVAudioUnitTimePitch() // pitch.pitch = -500 //Distortion pitch.rate = 1.5 //Voice speed // engine.attachNode(player) engine.attachNode(pitch) engine.connect(player, to: pitch, format: buffer.format) engine.connect(pitch, to: engine.mainMixerNode, format: buffer.format) player.scheduleBuffer(buffer, atTime: nil, options: AVAudioPlayerNodeBufferOptions.Loops, completionHandler: nil) engine.prepare() do { try engine.start() } catch _ { } player.play() }}
timePitch.pitch = 1000 //Filtered VoicetimePitch.rate = 1 //Normal rate