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

Audio Time Stretching

Discussion in 'General Discussion' started by Zades, Feb 25, 2014.

  1. Zades

    Zades

    Joined:
    Feb 25, 2014
    Posts:
    2
    I've looked all over for this and I haven't really found a way to do in unity without actually writing the algorithm for it. What I want is the ability to time stretch audio clips, that is to change the tempo (length) of the clip without changing the pitch. I know Unity itself has the pitch property but that changes both pitch and length, where as I only want the length to be changed. Is there an asset out there that does this?

    I would rather it be an asset as then it would easily integrate into unity as oppose to trying to hack together something from the C# library to modify audio files in Unity when Unity already has a audio management system, it would be messy.

    If there is no asset can someone point me to where I can learn to create assets that integrate into Unity?

    Thanks in advance.
     
  2. VIC20

    VIC20

    Joined:
    Jan 19, 2008
    Posts:
    2,682
    I think the main problem is that time stretching does not produce any acceptable results with compressed audio - so it mostly does not make any sense to use it, hence no one has written something like that yet.
     
  3. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    254
    Changing the pitch and length is an easy thing to do (algorithmically speaking). Changing only one of them is much harder, and I'm not sure if it can be done in real time.

    How many different tempos do you need? Hopefully you're not trying to do anything like a perfectly smooth transition from 60 bpm to 90 bpm that goes through all the tempos in between.

    If you just need a small number of different tempos, honestly, what I would try is manually time stretch the audio clip in an external tool like Audacity and then import all the different resultant audio files into Unity. Then, in-game, when you want a tempo change, just switch from one tempo's AudioClip to another and jump to the appropriate sample in the new clip so that it's relatively seamless to the listener
     
  4. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    254
    I don't think that's the problem at all. First of all, OP didn't mention compressed audio, so he may just be using uncompressed WAV format.

    Second, the compression itself isn't really a problem, it's how much is it compressed that affects the audio quality. As long as the compressed audio is still encoded at 44kHz and a high-quality bitrate, it should translate back into uncompressed audio with minimal noticeable difference. From there, it shouldn't be much different than if he had started with uncompressed audio.
     
  5. MarkrosoftGames

    MarkrosoftGames

    Joined:
    Jan 5, 2012
    Posts:
    442
    i honestly have no idea if unity supports something like this built in, or if there are any existing assets to do it.

    but i do know it should be possible to do in realtime. the 3ds has a sound app that you can record stuff and then add effects and stuff to it, and one of them has a graph where you can slide it around to adjust the speed and pitch. the point starts in the middle at 0,0, and as you slide it left and right it changes the audio speed but doesnt affect the pitch, and sliding it up and down affects the pitch without the speed. im guessing that in the background, wen you increase the speed, it is also decreasing the pitch by the same amount, so that it retains its pitch.

    if you can find a way to adjust the pitch as well as the temp, then you could write a quick algorithm like, when tempo is x set pitch by y, to maintain consitency.
     
  6. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Have a look at DIRAC's products, their stretching algorithms work really well even on mobile. You'll have to write a plugin and play audio natively, or maybe you can route it through Unity's audio buffer access? Great tech, that's for sure.

    About implementing time stretching algorithms yourself, I can just write one thing: good luck!

    Cheers,

    Gregzo
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  8. Zades

    Zades

    Joined:
    Feb 25, 2014
    Posts:
    2
    I’m making a music game, the idea is two have a normal speed, a harder version running at 1.5x speed and an easier version running at a lower speed, something like 0.5x. I don’t think an external program is viable as my program works by grabbing song files from a directory external to the assets in Unity. The user should be able to add or remove songs from the directory at will. Since the songs in the directory will be dynamitic based on user preference, I cannot make the different versions of the songs beforehand. They will need to be created at runtime. However, they do not need to be created while the song is running. The time stretch can occur when the music is loading but before it plays.

    I’m actually considering between WAV and MP3. WAV makes everything easier but MP3 is more widespread and easier for the user to obtain but at the same time I would need to uncompress it for time stretch to work.

    I don’t think I would actually write the time-stretch algorithm myself but use an existing C# library and integrate that into Unity’s system. Libraries I’m considering include:
    http://naudio.codeplex.com/
    http://www.surina.net/soundtouch/
    And the Dirac listed above.

    And I think this is the only way to get it into Unity. I’ve looked far and wide and it just seems that no one has attempted to implement this feature yet. I think I would need to make my own asset. The problem right now is I have no idea how to write an asset that processes music and integrates into Unity. Can anyone point me to some documentation or examples? I want it to be able to change properties and such in the inspector.

    Thanks in advance.
     
  9. DarkArts-Studios

    DarkArts-Studios

    Joined:
    May 2, 2013
    Posts:
    389
  10. jalemanyf

    jalemanyf

    Joined:
    Jun 16, 2013
    Posts:
    25
    Hi Dark Arts,

    Did you added this feature on your plugin? I need to slowdown human voice sound and I will buy it, if you show me how it can be done. Thanks!
     
  11. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Hey there,

    Have anyone used SoundTouch ? I have a problem with it
     
  12. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    I would also like help with nAudio. It seems simpler but I didn't understand how to use it in a Unity application.
     
  13. Jdread

    Jdread

    Joined:
    May 5, 2020
    Posts:
    1
    If someone's still interested: Since Unity has a pitch shifter plugin, a hacky workaround can be to put your audiosource's pitch down, and then add pitchifter and pitch it up the same amount. Playing with the fft size can give variably usable results...
     
    Last edited: Nov 23, 2021