Record, modify pitch and play back audio in real t

2019-07-07 00:43发布

问题:

I just wondering if anyone can explain to me how I would go about recording audio, modifying it (pitch?) and playing it back to the user on iPhone.

I'm not asking for someone to do this for me, just a few hints on how I should go about it. looking through the docs it seems like I should be using AVAudioSession, AVAudioRecorder and AVAudioPlayer (AVFoundation.Framework) for the recording playing parts. Or should I be using the CoreAudio.Framework? and then there is the question regarding modifying the audio.

回答1:

OpenAL is a library, that supports what you're searching for. And its very popular on the iPhone, so maybe you should take a look on this. i.e. The pitch of an sample is easily changeable with only one command:

alSourcef(source, AL_PITCH, pitch);

But I'm not sure, if pitch change is also with the AVFoundation or CoreAudio classes possible - never used them.



回答2:

AVAudioUnitTimePitch should do the trick.

Sample code from udacity.com Intro to iOS App Development with Swift:

func playAudioWithVariablePitch(pitch: Float) {
    audioEngine.stop()
    audioEngine.reset()

    let audioPlayerNode = AVAudioPlayerNode()
    audioEngine.attachNode(audioPlayerNode)

    let changePitchEffect = AVAudioUnitTimePitch()
    changePitchEffect.pitch = pitch
    audioEngine.attachNode(changePitchEffect)

    audioEngine.connect(audioPlayerNode, to:changePitchEffect, format:nil)
    audioEngine.connect(changePitchEffect, to:audioEngine.outputNode, format:nil)

    audioPlayerNode.scheduleFile(audioFile, atTime:nil, completionHandler: nil)
    try! audioEngine.start()

    audioPlayerNode.play()
}


回答3:

This might help. DIRAC-mobile, a library for real-time tempo and pitch manipulation of polyphonic audio signals. They also have sample codes that you can take a look once you download the library.