Oriol Gomez
Oriol Gomez

Reputation: 199

Changing the pitch of a sound in realtime (Swift)

I have been trying, for the past weeks, to find a simple way to control audio frequency (I.E. change the pitch of an audio file) from within swift in realtime. I have tried with AVAudioPlayer.rate but that only changes the speed. I even tried connecting an AVAudioUnitTimePitch to an audio engine with no success. AVAudioUnitTimePitch just gives me an error, and rate changes the playback speed which is not what I need. What I would like to do is, make a sound higher or lower pitch, say from -1.0 to 2.0 (audio source.duration/2 so it would play twice as fast.

Do you guys know of any way to do this even if I have to use external libraries or classes?

Thank you, I am stumped as to how to proceed.

Upvotes: 1

Views: 1386

Answers (1)

hotpaw2
hotpaw2

Reputation: 70743

If you use the Audio Unit API, the iOS built-in kAudioUnitSubType_NewTimePitch Audio Unit component can be used to change pitch while playing audio at the same speed. e.g. the kNewTimePitchParam_Rate and kNewTimePitchParam_Pitch parameters are independent.

The Audio Unit framework has a (currently non-deprecated) C API, but one can call C API functions from Swift code.

Upvotes: 1

Related Questions