- Home /
how do i crossfade sound loops in unity?
how do i crossfade sound loops in unity?
Answer by Jessy · Nov 18, 2009 at 05:13 AM
There won't be a pop if you did a good job fading, making sure that the wave begins and ends at zero, with a positive or negative slope at both ends. Also, as with making tiling textures, you probably want to "undo your offset" after doing that editing, so the beginning is really the beginning again.
However, I assumed you were talking about real-time crossfading. With Unity 2.6, you can use the Animation Editor to make volume curves! Do keep in mind, though, that Unity uses a linear volume taper, so straight lines will probably sound pretty bad. Check this page out for a general-purpose curve adjustment. You may want to script a property like that instead. However, I bet it will be a lot better for performance if you just use keyframes.
Just keep in mind the curves won't make much visual sense. Use your ears! :-)
Answer by Design3.com · Nov 18, 2009 at 04:00 AM
If you're looping one sound over and over, I'd do it manually in a program like Audacity. You can fade out the end and fade in the beginning. If you want a transition without a fade to silence in the middle, you can:
- Split the clip in two.
- Move the first part so it comes after the second part.
- Crossfade between the end of the second part (which is now first) and the beginning of the first part (which is now last).
When Unity loops the clip, the new beginning and end should be seamless and the old beginning and end will be crossfaded nicely. I've only actually tried this method with wrapping textures during UV animation (and not with audio), so it's possible there might be a pop as the clip loops.
thanks. i was more thinking of turning down the volume of the one loop while turning up the volume of the other loop AT RUNTI$$anonymous$$$$anonymous$$ so i have the loops prepared upfront. i just want to deter$$anonymous$$e AT RUNTI$$anonymous$$E when to start the one loop and when to start the other. thus i'd like to crossfade from the one currently playing to another.
Answer by jashan · Nov 28, 2009 at 01:01 PM
In addition to what Jessy said: You can use the API provided by AudioSource to synchronize the two audio loops that you want to crossfade (or simply start them "simultanuously" - but synching them feels safer to me), and also to change the volume. Since Unity 2.6 this also provides sample-exact synchronization (that's done via the property AudioSource.timeSamples. And, also since Unity 2.6 this also works very reliably (I've had an implementation for this in 2.5 which was "off" a few milliseconds most of the time - since 2.6 it just works perfectly; without even changing my implementation).
So, either you do it "in code" changing the volume a little bit each frame (that would be done in Update); or you design your cross-fades in the AnimationEditor as Jessy suggested and then trigger those animations in whichever way is appropriate (e.g. on collisions, or when the user presses a certain key ... you name it - there's all possibilities at your fingertips ;-) ).
The "in-code" approach is probably best when you don't have a fixed way of cross-fading but for instance want to cross-fade by distance or any other funky thing you could think of (e.g. when you get closer to a certain object, loop A is increased and loop B decreased, and when you move away from that object, loop A is decreased and loop B increased).
The solution using the animation editor is preferable when you want a complete crossfade to happen based on some triggering event (reason is that the animation editor gives you more intuitive control on how the crossfade exactly should happen, you can literally loop the crossfade until you feel it's "perfect for the given purpose"). Using animation events, you could even stop the silent audio clip when the fade is done.
Oh, one final thing: You might also want to keep track of the position in the loop. If you have a beat, for instance, you may want the crossfade to start only when the beat starts which can be done easily by tracking the time (given your loops are prepared for doing this kind of thing). Usually, this doesn't have to be totally perfect with fades but "pretty close".
Your answer
![](https://koobas.hobune.stream/wayback/20220613162731im_/https://answers.unity.com/themes/thub/images/avi.jpg)