3

I using AVAudioEngine to alter the pitch and rate of audio in Swift.

I figured out how to change the speed/rate and keep the pitch the same, but when I do it there is a sort of audio glitch where you can hear the audio pitch adjusting very quickly when you switch the rate (I guess because the code first changes the rate and then changes the pitch to sound like the original)

I would like to get rid of this effect and have the pitch and rate alterations happen simultaneously. Is this even possible?

W

    var engine : AVAudioEngine = AVAudioEngine()
    var speedControl : AVAudioUnitVarispeed = AVAudioUnitVarispeed()
    var pitchControl : AVAudioUnitTimePitch = AVAudioUnitTimePitch()

    // ...

    func changeTempo(to tempo: Float) {
        speedControl.rate = tempo

        // to keep the same pitch:
        let pitchChange = log2(speedControl.rate) * 1200.0
        pitchControl.pitch = pitchChange * -1
    }
spitchay
  • 89
  • 1
  • 10

1 Answers1

0

You appear to of overlooked that AVAudioUnitTimePitch can do both pitch and time.

https://developer.apple.com/documentation/avfaudio/avaudiounittimepitch

Ryan Francesconi
  • 988
  • 7
  • 17