x


how do i crossfade sound loops in unity?

how do i crossfade sound loops in unity?

more ▼

asked Nov 17 '09 at 11:59 PM

flexrails gravatar image

flexrails
706 29 35 55

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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! :-)

more ▼

answered Nov 18 '09 at 05:13 AM

Jessy gravatar image

Jessy
15.7k 72 95 198

(comments are locked)
10|3000 characters needed characters left

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".

more ▼

answered Nov 28 '09 at 01:01 PM

jashan gravatar image

jashan
10.3k 25 43 117

(comments are locked)
10|3000 characters needed characters left

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:

  1. Split the clip in two.
  2. Move the first part so it comes after the second part.
  3. 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.

more ▼

answered Nov 18 '09 at 04:00 AM

Design3.com gravatar image

Design3.com
640 1 1 10

thanks. i was more thinking of turning down the volume of the one loop while turning up the volume of the other loop AT RUNTIME. so i have the loops prepared upfront. i just want to determine AT RUNTIME 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.

Nov 18 '09 at 07:51 AM flexrails
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1058
x83

asked: Nov 17 '09 at 11:59 PM

Seen: 6280 times

Last Updated: Dec 10 '09 at 04:49 PM