- Home /
How I can change the speed of a song or sound?
I'm trying to change the speed of the audio but I could not find a way to do it.
Could someone please help me.
Answer by Wolfram · Aug 16, 2010 at 10:27 PM
Unity can only change the speed of an AudioSource. It calls this parameter "Pitch". However, changing it will always change both pitch and tempo, since this is just a matterl of changin the playback sampling rate and can be done in realtime at almost no cost and with (almost) no sound distortion.
Unity cannot change just the pitch (and keep the tempo constant) or just the tempo (and keep the pitch constant). What you are looking for is the latter.
Up until recently I would have answered this is a very expensive process since you actually need to resample your audio clip by splitting it into very short segments and then overlap or copy&extend them, so you would have a hard time doing this in realtime during your game.
However, as the videoplayer vlc has shown in recent versions (>=1.0 I think), there is an algorithm that can be evaluated in realtime to do just that (When increasing/decreasing the video playback speed in vlc, the audio can still be heard, with matching speed, astonishingly few distortions, and most importantly unchanged and constant pitch.) So I'd suggest to put it on the Unity wish list.
EDIT: If you are not interested in dynamically changing the tempo, but instead just change it once and then keep that new tempo, I'd suggest to use some audio software to modify your audio sample. ("Audacity" is a frelly available tool to do that and other stuff)
its always good to find answers without needing to ask any questions, thanks.
Answer by RayRay26 · Oct 31, 2018 at 12:32 AM
I know this question is old, but this thread appears near the top of most Google results about music speed, and times have changed over the last 8 years. 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 var pitchBendGroup = Resources.Load("Pitch Bend Mixer"); 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: audioSource.pitch = 1.5f; 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').
I tried this method it does not work.
changing the audio pitch by =1.5 and then changing its mixer's pitch by =1/1.5 makes the pitch equal to what it was previously (1.5/1.5 = 1). There is a small sound distortion because it has been processed, but the overall pitch and tempo are both unchanged.
Had not heard of mixers before this, so was pleased to find something valuable out of this post, but again this is not the solution.
A note that there are two $$anonymous$$ch sliders. The bottom one is the one you want, because if you change the top one you effectively cancel out the AudioSource pitch with the mixer's. The bottom one generates an actual pitch bend. I also updated the post for an extra step about what to do after exposing the bottom pitch in the code (as it has the default name $$anonymous$$yExposedParam).
Aaha, I had the same issue before because of exposing the wrong pitch parameter! Thanks for your update!
Just wanted to say thanks RayRay26 and this is still working in 2019.3. For future users just be sure to pay attention to the part rena$$anonymous$$g the exposed parameter in the Audio $$anonymous$$ixer - edited hint at the bottom of the answer. :)
I would say this master pitch works much better. It displays max value as 1000, but should be assigned max as 10.
audioSource.outputAudio$$anonymous$$ixerGroup.audio$$anonymous$$ixer.SetFloat("$$anonymous$$ain$$anonymous$$ch", 10f);
@Achoo doesn't the master pitch change the speed also so it negates the effect?
The other slider seems to keep speed the same and just changes pitch. It doesn't give a perfect result, but better than nothing.
This solution seems to work, not perfect but noticeable difference. Thanks a lot! I was almost about to give up and this seems to be the best way currently.
Answer by KnightChatX · Apr 30, 2011 at 05:33 PM
You can use an audio editor such as Audacity here to edit the sound file and change it's speed: http://audacity.sourceforge.net
thanks, I was asking this inside of unity but seems like that's another good way.
Answer by Maltus · Aug 16, 2010 at 06:39 PM
The AudioSource Pitch handles the speed of playback. Value of 1 is normal playback.
and how I can do to not change the pitch, just change the speed to sound like the song but faster or slower
The $$anonymous$$ch is the speed. Put ypor song in an Audio Source Component set it to play on awake put the pitch to 2 and press play on your game and voila chipmunks will be singing.
I take it you are looking at just changing the tempo of audio and not doing a cassette-fast-forward/lp-on-wrong-speed-setting. I haven't seen anything like that in Unity where you can strictly change the tempo.
$$anonymous$$altus replied while I was typing...I agree with $$anonymous$$altus that the pitch and tempo go hand-in-hand.
so change the speed of the song is to difficult but in that case its there a way to change the speed of the game, I mean to be all slow including the speed of the song???????????
Answer by adom86 · Nov 12, 2012 at 05:07 PM
Hey guys
I have an FBX animation that was animated to a specific audio file.. this was done at 30FPS in 3ds max. After importing to unity and plying the audio along with the animation it is some what off... is there a workflow to fix this?
Many Thanks