synthcat
synthcat

Reputation: 302

AVPlayer : change rate with NO pitch correction?

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.

Upvotes: 4

Views: 1449

Answers (3)

Alexander Yakovlev
Alexander Yakovlev

Reputation: 510

Using AVAudioUnitTimePitch is what you need. Changing rate property (default is 1.0) will change speed of audio without pitch changing.

Upvotes: 0

spitchay
spitchay

Reputation: 149

Actually, this is possible with AVPlayer --

let player = AVPlayer(url: fileURL)

// to turn off pitch correction:

player.currentItem?.audioTimePitchAlgorithm = .varispeed

Upvotes: 0

spitchay
spitchay

Reputation: 149

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

Upvotes: 2

Related Questions