2
votes

I am using the rate property of AVPlayer to change the playback speed of an audio sample. It seems to always apply pitch correction, but I want no pitch correction so that when it speeds up it gets higher like a record or an old tape player. Is there any way to shut off pitch correction altogether in AVPLayer?

I am currently using Swift 3, but Objective C answers are welcome, too.

2

2 Answers

1
votes

Not sure if this is possible using an AVPlayer, but if you're just using it to play audio you can easily do this with an AVAudioEngine:

var audioPlayer = AVAudioPlayerNode()
var engine = AVAudioEngine()
var speedControl = AVAudioUnitVarispeed()

// engine setup:

do {

    let file = try AVAudioFile(forReading: "myFile.mp3")

    engine.attach(audioPlayer)
    engine.attach(speedControl)

    engine.connect(audioPlayer, to: speedControl, format: nil)
    engine.connect(speedControl, to: engine.mainMixerNode, format: nil)

    audioPlayer.scheduleFile(file, at: nil)

    try engine.start()

} catch {
    print(error)
}

// changing rate without pitch correction:

speedControl.rate = 0.91

0
votes

Actually, this is possible with AVPlayer --

let player = AVPlayer(url: fileURL)

// to turn off pitch correction:

player.currentItem?.audioTimePitchAlgorithm = .varispeed