Search Unity

Audio Adjusting the speed of an audio clip...

Discussion in 'Audio & Video' started by ZealUnity, Jun 2, 2018.

  1. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    Trying to adjust the speed (not pitch) of an audio clip, like this...


    I see there are several 'audio assets' on the asset store, but do any of them have this capability? If not how hard would it be to roll my own solution?

    Thanks!
     
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    I myself use audacity for that sort of thing.
    You have to change the Tempo, of the audio file.
    The Tempo of the audio file, is the speed it plays at.

    Open audacity, bring in the audio file and use the Change Tempo effect.
    The Bpm option aka Beats per minute, is what you should be using
    to change the speed of the audio.

    If the speed of the audio, or Bpm, is for eg. 120 bpm, changing it to any number
    that is higher, or above 120 bpm, say for eg. 130 bpm, will make the audio
    go/play faster.

    Anything under 120 bpm, will make the audio play slower.

    Check out this cool tut, from the audacity guys.
    https://manual.audacityteam.org/man/change_tempo.html
     
    Last edited: Jun 3, 2018
    Westland likes this.
  3. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    But the idea is I want to do this in code.

    Is there anyway to do this with the base capabilities of Unity, or will I need to leverage some third party c# lib?
     
  4. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    So looking through the API, Unity can really only adjust the pitch of an audio clip... So how hard would it be to convert an audio clip into a raw data file (wav?) then apply whatever process Audacity uses to adjust the tempo?

    And there are so many "complete audio solutions" on the asset store... do none of them have this capability?
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Well, if you think about how sound works you can’t really stretch a wave without effecting its frequency or pitch. What you need is a midi system that plays sampled at timestamps at normal speed and then slow the pace of triggering those samples
     
  6. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    As was mentioned above, Audacity does it just fine, so it is very possible. The question is, HOW does audacity do it, and how can we implement a similar system inside of Unity...

    Anyone have any ideas?
     
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I'm not saying it's impossible, but I'd say it requires some processing and probably isn't something that is easily implemented to work in realtime.
     
  8. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    Sorry should have clarified - don't need this to work in realtime. I just want to implement what Audacity does.

    Really surprised Unity doesn't have this functionality built in. Being able to adjust the speed (without distorting the pitch) is insanely useful for adjusting sound effects, voice overs, etc....
     
  9. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    Ok so I guess the consensus is that I will need to find a third party lib to do this outside of unity. I remember using FMOD in a c++ project many years ago, I coulda sworn it could change the speed of a wav...

    Is anyone aware of any audio libs I can use in c#/unity? Or perhaps the code/science behind how I could implement this myself (by manipulating the raw wav data)?
     
  10. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Then just use Audacity or whatever other audio software that can do it.

    Or just use Audacity or whatever other audio software that can do it.

    ...

    edit: what is it you really want?
     
  11. jamnes

    jamnes

    Joined:
    Jun 12, 2018
    Posts:
    1
    As was mentioned above, Audacity does it just fine, so it is very possible. The question is, HOW does audacity do it, and how can we implement a similar system inside of Unity...

    Does anyone have any ideas then let me know https://printerchatsupport.co.uk/epson-printer-support/
     
  12. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Unity isn't really designed as an asset creation package so it isn't implemented. I guess you can read bytes and write bytes to a from a sound file but why would you bother when you can use an audio program that already does it? Unity imports and updates assets as soon as they change so I don't know why you'd need this feature inside Unity just like you don't need the power of a modelling package like Maya or Max inside Unity, or a drawing/texture like Photoshop inside Unity.

    What exactly is your use-case that demands it be inside Unity?
     
  13. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    I can't use Audacity, I need this feature to be included in my Unity app.

    The use-case is to extract clips from a user provided music/dialogue track, and manipulate them in various ways (think DJ). I can't just tell everyone "use Audacity".

    There are some DJ assets on the asset store, but as far as I can tell none of them can slow/speed up a clip.
     
  14. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I guess you’ll have to write the code then. :p
     
  15. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    You said earlier that you don't need it for runtime. That's why everyone was suggesting some audio editor.

    You probably need to do this by yourself. Or perhaps you could find C# version / wrapper for this: http://www.surina.net/soundtouch/

    Also you could try combination of changing the speed (pitch) in Audio Mixer, and add the Pitch Shifter audio effect. If you add speed, say 20%, it will also affect the frequency as you know, so to counter that, you need to drop pitch (in pitch shifter fx) 20%. It's far from perfect, but you can try different fft size and other settings (in pitch shifter) to get it better.
     
  16. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    I don't need it to run in real time (don't mind if there is a slight processing delay), but I clearly stated I needed to do this in code.

    Still amazed that none of the "DJ assets" on the asset store do this...

    So you're right, I need to write something custom, and that's why I'm here. Hoping somebody has experience with audio processing and knows how to do this (or better yet knows of a library I can use inside of c#/Unity).
     
  17. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    What you're asking of is non trivial and is not something Unity should have built in.

    The general idea is that if you need to speed up without changing speed you play a small part of the clip say from 0.0 to 0.1, then skip ahead, and quickly crossfade playing 0.2 to 0.3, skipping a small part of the audio and keep repeating that process as you go through the audio, so that you go through it faster.

    To slow down, you play a small part of the clip, say from 0.0 to 0.1, then, you crossfade into playing 0.05 to 0.15 etc.

    How big those parts you play are and how long the crossfade is, gives the audio different characteristics you may or may not want. Most audio software give you control of these, I guess audacity has just simply chosen strong defaults, but that's it.

    There are far more advanced algorithms out there (izotope has a few) that sound a lot better, but they are super slow and I don't really know what they do.
     
  18. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    gregzo's / past truly / g-audio used dirac library for this (no idea to what success)
    https://www.assetstore.unity3d.com/en/#!/content/19322

    note that this is/was iOS only (but dirac *maybe* works on other platforms), is outdated, primarily for unity 4.x and not supported anymore
    (since the main package is freely available i've updated main package (i.e. not this one) for unity 5.x some time ago, which is possible
     
  19. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    @AcidArrow - interesting can anyone confirm this is/isn't how Audacity does it? I could see cross fading snippets working to speed up a clip (since you are just removing chunks), but how would it work to slow down (you would need to insert something in between)?

    @r618 - The documentation link for gaudio seems dead. Although the description claims it can adjust speed...
     
  20. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    You repeat stuff. Like play 0.0->0.1 then 0.03->0.13 then 0.06->0.16 etc
     
  21. ZealUnity

    ZealUnity

    Joined:
    Apr 13, 2014
    Posts:
    64
    Hmm wouldn't that create an echo effect? Can anyone confirm/deny that this is how Audacity does it?