- Home /
Adjust audio playback rate with Time.timeScale
Hey,
I currently have a scene set up that utilizes Time.timeScale. It works perfectly for achieving the slow motion effect I was after. The only thing is that It does not effect the audio playback speed, so it feels kinda awkward. Is there some thing similar to Time.timeScale, but for audio playback speed? Or is there a way to sync the audio with the video so that it matches the time scale?
Thanks in advance!
Answer by gregzo · Apr 15, 2012 at 09:14 AM
Indeed there is! audio.pitch it is. A value of 1 is normal, .5 is twice slower. Negative values read the audioclip in reverse. Won't affect PlayOneShot, of course...
That works great, but it only works for one audio source at a time. Is there a way to effect all of the game audio at once, or will I just have to attach audio.pitch to every sound source?
Yes, pitch is a property of audioSources, so changing one will (thankfuly in most cases) not affect the others. Two solutions: 1)Reference or do a find on all your playing sources to change their pitch properties all together (easy peasy).
2)Write your own pitch shifting filter which you can implement directly on the AudioListener with OnAudioFilterRead (tricky!)
Thanks so much! I decided to just adjust all the audio sources separately because there are only a few, and it makes things a bit easier.
Negative value not playing audio reversely, audio is muted that time.
Thank you, Ben
Answer by manuelsilverio01 · Jun 07, 2013 at 12:18 AM
What I think you could do with the Audio is using one single function to play audio, and in that function make audio.pitch = Time.timeScale... as you'll be using that function to play all the audio then problem solved
function PlaySound (soundName, soundDelay)
{
if(!audio.isPlaying && Time.time>soundRate)
{
audio.pitch = Time.timeScale;
soundRate = Time.time + soundDelay;
audio.clip = soundName;
audio.Play();
yield WaitForSeconds(audio.clip.length);
}
}