'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.
Solution 1:[1]
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
Solution 2:[2]
Actually, this is possible with AVPlayer --
let player = AVPlayer(url: fileURL)
// to turn off pitch correction:
player.currentItem?.audioTimePitchAlgorithm = .varispeed
Solution 3:[3]
Using AVAudioUnitTimePitch
is what you need. Changing rate
property (default is 1.0) will change speed of audio without pitch changing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | spitchay |
Solution 2 | spitchay |
Solution 3 | Alexander Yakovlev |