How to change audio pitch during playback? (Swift 4)

半世苍凉 提交于 2020-01-23 01:44:28

问题


I'm looking to change the pitch and playback speed of some audio in real time with a slider, or a variable (i.e. while the sound is playing) in Xcode, Swift 4.

Currently, I'm using AVAudioEngine, which allows me to set these values before playback starts, but I can't change them while I the audio is actually playing.

Here is the code in question:

func Play() {
    engine = AVAudioEngine()
    audioPlayer = AVAudioPlayerNode()
    audioPlayer.volume = 1.0

    let path = Bundle.main.path(forResource: "filename", ofType: "mp3")!
    let url = NSURL.fileURL(withPath: path)

    let file = try? AVAudioFile(forReading: url)
    let buffer = AVAudioPCMBuffer(pcmFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))
    do {
        try file!.read(into: buffer!)
    } catch _ {
    }

    let pitch = AVAudioUnitTimePitch()
    let speed = AVAudioUnitVarispeed()

    pitch.pitch = speedIncrement * (-500)
    speed.rate = speedIncrement

    engine.attach(audioPlayer)

    engine.attach(pitch)

    engine.attach(speed)

    engine.connect(audioPlayer, to: speed, format: buffer?.format)

    engine.connect(speed, to: pitch, format: buffer?.format)

    engine.connect(pitch, to: engine.mainMixerNode, format: buffer?.format)

    audioPlayer.scheduleBuffer(buffer!, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)

    engine.prepare()
    do {
        try engine.start()
    } catch _ {
    }

    audioPlayer.play()

}

Is this even possible with AVAudioEngine?

If this is not possible with AVAudioEngine, what are other possible solutions?


回答1:


Actually, I got the answer from reddit. All I had to do was to declare the pitch and speed variables outside of the Play() function, along with the pitch.pitch = speedIncrement * (-500) and speed.rate = speedIncrement expressions. I knew I was missing something simple...



来源:https://stackoverflow.com/questions/49100483/how-to-change-audio-pitch-during-playback-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!