As I see in the Inspector the Pitch of an AudioSource has range of [-3,3] and the default value is 1. After trying setting the pitch differently, I noticed that the length of the attached AudioClip seems to be lengthened/shrunken depending on the pitch being decreased or increased. So it’s quite useful to know the actual length of the AudioClip in that case. I guess we have to somehow calculate that length based on the AudioClip.length property and the pitch but not sure how to do.
Could you tell me if that’s possible and how to do it? Thank you for your help.
Thank you. It works great. I wonder if you remember how you knew that? I googled around for about half an hour but nothing found. As I know there are some algorithms behind audio pitching. So how it stretches the time may be different. Unity manual as well as reference has nothing about this. Thank you very much.
Thanks but as I said I looked for around and found that there are some algorithms behind audio pitching and they are all complicated. So I was not so sure what exactly they are different especially in how they stretch the time (duration). As I know we even have another kind of pitch which does not stretch time at all (maintain the clip length) and it’s called pitch shift.
Pitch shifting and time stretching are indeed complicated things. In most cases when the pitch of an audio sample changes, it doesn’t time stretch or pitch shift, not even in music software (such as a sampler). Changing pitch is almost always just a simple linear change in playback speed. So, don’t worry about those algorithms unless something is explicitly stated to time stretch or pitch shift. In theory it’s possible to do pitch shifting in Unity by creating a custom audio filter, but I don’t know if anyone has attempted this. It’d be rather CPU hungry and probably not that useful for realtime game audio.
If you’d deal with an actual pitch shifter, the answer would be that the length would be identical if the pitch changed.
Unity 5 actually has a native pitch shifter plugin based on the Fourier transform that does not do any time compression/stretching. I believe it is based on Stephan Bernsee’s implementation.