Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Last active August 29, 2019 13:42
Show Gist options
  • Save dhavaln/2021993bbbbe8a9a0934 to your computer and use it in GitHub Desktop.
Save dhavaln/2021993bbbbe8a9a0934 to your computer and use it in GitHub Desktop.
Change Pitch Rate of Sound in Swift - XCode 6.4
var engine: AVAudioEngine!
var file: AVAudioFile!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
engine = AVAudioEngine()
audioFile = AVAudioFile(forReading: filePathUrl, error: nil)
}
func playSound(value: Float, rateOrPitch: String){
var audioPlayerNode = AVAudioPlayerNode()
audioPlayerNode.stop()
engine.stop()
engine.reset()
engine.attachNode(audioPlayerNode)
var changeAudioUnitTime = AVAudioUnitTimePitch()
if (rateOrPitch == "rate") {
changeAudioUnitTime.rate = value
} else {
changeAudioUnitTime.pitch = value
}
engine.attachNode(changeAudioUnitTime)
engine.connect(audioPlayerNode, to: changeAudioUnitTime, format: nil)
engine.connect(changeAudioUnitTime, to: engine.outputNode, format: nil)
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
engine.startAndReturnError(nil)
audioPlayerNode.play()
}
@IBAction func playChipmunkEffect(sender: UIButton) {
playSound(1000, rateOrPitch: "pitch")
}
@IBAction func playDarthVaderEffect(sender: UIButton) {
playSound(-1000, rateOrPitch: "pitch")
}
@IBAction func playSoundSlowly(sender: UIButton) {
playSound(0.5, typeOfChange: "rate")
}
@IBAction func playSoundFast(sender: UIButton) {
playSound(1.5, typeOfChange: "rate")
}
@Miths19
Copy link

Miths19 commented Aug 23, 2016

hey brother, I'm using this coding for changing the pitch but now what should i do for saving that pitch changed audio any solution i m new in iOS pease help..

@ZaeemZafar
Copy link

did you find any solution to your problem ?

@lbaomyscoopapp
Copy link

hey brother, I'm using this coding for changing the pitch but now what should i do for saving that pitch changed audio any solution i m new in iOS pease help..

Hi, did you find solution for this problem. Can you share with us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment