Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get AudioClip's Length when AudioSource having modified pitch?

Discussion in 'Audio & Video' started by VipHaLongPro, Jul 7, 2015.

  1. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    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.
     
    Last edited: Jul 8, 2015
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    A pitch of 3, and -3 both have the same length. It's just that -3 plays backwards
    A pitch of 0 shouldn't play.

    Playing backwards is limited to non streaming, non compressed audio.

    Obviously you don't want to divide by zero, but:

    NewLength = (Length / Math.Abs(Pitch))
     
    Last edited: Jul 8, 2015
    kaisersam and VipHaLongPro like this.
  3. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    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.
     
  4. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    It's pretty straightforward. It's like a multiplier. A pitch of 3 means it's playing three times as fast, so it'll be 1/3 as long.
     
  5. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    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.
     
  6. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    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.
     
  7. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    164
    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.
     
  8. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    Wow, neat! I must have missed that out somehow!
     
  9. jalemanyf

    jalemanyf

    Joined:
    Jun 16, 2013
    Posts:
    25
    Hi,

    I'm working in a project where I have to control the speed of audio (human voice) while maintaining pitch and sound quality.

    Have you find the solution to do this?... please, could you say me the best/easy way to do it?

    Thanks!