Search Unity

Audio change speed in music/sound

Discussion in 'Audio & Video' started by Bendt, Oct 31, 2017.

Thread Status:
Not open for further replies.
  1. Bendt

    Bendt

    Joined:
    Oct 30, 2017
    Posts:
    3
    Is there any way of changing speed in the music in Unity. I am familiar with the pitch parameter. And I do know that pitch and speed are linked together, but maybe some people has experience with getting around only changing the speed of the music/sample.

    Regards Thomas Bendt
     
  2. mtytel

    mtytel

    Joined:
    Feb 28, 2014
    Posts:
    84
    Time stretching is a difficult problem and so there's really no good open source version.
    To get a good quality time stretching algorithm you'd have to license some expensive piece of software like this: http://www.zynaptiq.com/ztx/
     
    unitybru likes this.
  3. mtytel

    mtytel

    Joined:
    Feb 28, 2014
    Posts:
    84
  4. RayRay26

    RayRay26

    Joined:
    Apr 28, 2016
    Posts:
    2
    I know this thread is slightly old, but this thread appears near the top of most Google results about music speed. I did find a solution, but it requires learning Audio Mixers. It's not perfect; you'll hear imperfections in the sped up audio, but it's better than constantly setting AudioSource.time or creating your own audio buffers.

    Press Ctrl+8 to open the Audio Mixer tab (or find it in the Window menu at the top), then add a mixer. I put my mixer in the Resources folder so I can fetch it with:
    Code (CSharp):
    1. var pitchBendGroup = Resources.Load<UnityEngine.Audio.AudioMixerGroup>("Pitch Bend Mixer");
    2. audioSource.outputAudioMixerGroup = pitchBendGroup;
    Once you made one, add the Pitch Shifter effect to the Master slider. You should see this in your Inspector (not the Audio Mixer tab):

    The bottom slider that says Pitch is the one you want. If you right click the word "Pitch" you can expose it in your mixer so you can change the value by code (that makes the arrow appear next to it). So if you want to keep the song pitch the same but speed up the tempo by 50%, you do:
    Code (CSharp):
    1. audioSource.pitch = 1.5f;
    2. pitchBendGroup.audioMixer.SetFloat("pitchBend", 1f / 1.5f);
    It seems pretty lightweight on DSP CPU, so I don't think there's a need to create your own audio buffers or crazy stuff like that, just use a mixer. If you want to save CPU while the tempo is at 100%, set outputAudioMixerGroup to null. Also I recommend lowering the volume by 3 dB because that tends to be how much louder it gets with 4.00 overlap.

    EDIT: I realized there is an extra step for newer versions. Once you make the arrow appear next to Pitch you have to go back to Audio Mixer and open up Exposed Parameters at the top-right of the panel. It's recommended to right-click and rename the 'MyExposedParam' entry (I named it 'PitchBend').
     
    Last edited: Sep 20, 2019
    emredesu, Magn077p, Cranick and 14 others like this.
  5. civogames

    civogames

    Joined:
    Jan 13, 2016
    Posts:
    7
    Thanx a lot RayRay26 ! Your solution works well for many platforms, such as standalone for windows, mobile app Android... but it does not work for WebGL...

    It seems like it doesn't change the pitch of the AudioMixer... only the pitch of the AudioSource. (When changing the tempo using the 2 code lines you gave us).

    Do you have an idea ?
     
  6. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    318
    This is just kind of abstract general audio production info but I feel it's relevant... Let's say you have a song that is a flat .wav file and you change the pitch to be higher and do nothing else, you're going to have a chipmunk effect because while the pitch was changed, the tone is based on the original sounds so it won't sound great.

    Now if your same song is midi, then you can change your pitch and it will sound good because the software synth will adjust it accordingly because it's more flexible dynamically being a midi.

    Also just general info for anybody who might be reading... Some songs sound good slowed or sped up, while others don't. Think loose jam band compared to say techno music. The techno music is going to sound just fine whatever speed you play it at, while the flow and ebb of jam band sounding music is more likely to sound bad when slowed down or sped up.
     
  7. Hobbeslionheart

    Hobbeslionheart

    Joined:
    Sep 19, 2019
    Posts:
    5
    I'm not an audio person, but WebGL is very limited in terms of what Unity can/will support to ensure that it works so it's highly possible that AudioMixer might not be working for WebGL. The manual for "Using Audio in WebGL" says "This page will document what is expected to work. Anything not listed here is not currently supported on WebGL." Note that there's no mention of AudioMixer thus may not be supported.

    Anyone with evidence to the contrary, feel free to correct me.
     
  8. Tonymotion

    Tonymotion

    Joined:
    Dec 5, 2018
    Posts:
    12
    Thank you, @RayRay26 ! 2+ years later and it still works in 2019.4.13f1! I would probably never have figured this out before the end of this month without your help.
    The only problem I had was typing "pitchBlend" instead of "pitchBend". I've hooked the controls for the tempo changes up to the arrow keys and added some other audio controls so I can test out my visualizations reacting to a song's speed. Big thanks!
     
Thread Status:
Not open for further replies.