Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio Setting the position of an AudioSource whose pitch isn't 1

Discussion in 'Audio & Video' started by srylain, Jun 15, 2017.

  1. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    I'm trying to create a rewind effect of sorts for my game, and I'd like to be able to rewind the audio to a second or so before and play it from there. The gameplay relies on the audio being synced to it, and I can't seem to figure out how to sync it properly when the pitch is different than 1.

    I know that the pitch makes audio longer/faster, but from what I'm seeing is that the length value in the AudioClip doesn't change when the pitch is changed and dividing the timer I keep track of the gameplay with by the pitch value doesn't correctly give me the right time either.

    I then tried setting the time to a position that's longer than the clip (clip is ~450 seconds, so at .6 pitch it should be roughly 750 seconds) which was 600, and it threw an error saying there was an invalid seek position was passed.

    So is this something that's possible with Unity's audio engine?
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Open your audio track in Audacity. Flip it. Save it as new track.
    Have both tracks play in your game it once, except rewind one while the other is playing.
    (main volume 100, flipped 0)
    Then, while you are rewinding, just switch tracks and play the flipped track while rewinding main track.
    (main volume 0, flipped 100)
     
  3. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    It's not that I want the track to play in reverse (I've got another sound effect for that), it's just that I want to be able to set the position of that clip to a second earlier and then play it from there. When the pitch is set to 1, it works just fine. But anything other than 1 and the time ends being off.

    Example: I'm ten seconds into the clip and it's set to .5 pitch. I rewind it to 9 seconds, and then play it from there. The audio is then not at 9 seconds when it starts. The timer I use for the gameplay also doesn't match up with the time the AudioSource is giving me (that's after dividing the timer by the pitch).

    What I did just notice is that if I play the same clip twice, once at .5 pitch and the other at 1, the timeSamples value is exactly half on the .5 pitch one than it is on the normal one. Can I use this instead?
     
  4. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    Looks like I figured it out. I was right in using AudioSource.timeSamples, seems it's quite a bit more accurate than just setting the time value.
     
    FMark92 likes this.