Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio I'm trying to make background music loop onto itself with correct timing, delay in first method loop

Discussion in 'Audio & Video' started by UNTL1, May 17, 2020.

  1. UNTL1

    UNTL1

    Joined:
    Mar 30, 2020
    Posts:
    71
    I have music that reverbs away when the loop finishes, and I want the first note of the music fall in rhythm before the loop actually finishes so you can hear the reverb mixing with the new music loop.

    The method I tried was calling on a method that plays a One Shot clip from the Start method and repeats every certain amount of seconds. The first loop of the method supposedly needs more time than it's told before the second loop is called if I invoke the method with 0 delay. If I add an adequate amount of delay to its invoke the sync between the first and second loop is fine, but for example the adequate amount for my PC is not enough for my Android phone.

    It seems like it starts playing the audio, but the timestamp it takes for timing the method repeats is only after all of the other start methods have completed loading? Does that make sense? Is there any chance to overcome that other than inducing enough delay to the invoke? I can see other situations where it can cause issues other than looping audio.
     
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    137
    There are a couple of options for doing this.

    The easiest way to do it is to just bake the reverb tail into the loop. This is what I do with every looping track that I make. I design it in a way where the reverb isn't too pronounced when you first play the track but makes the transition seamless when it loops round. Then I add very short fades to the start and end so it doesn't pop on start (see this article on seamless loops for an example of that).

    The other option is to use two Audio Sources and schedule the second audio source to play using PlayScheduled when the first clip ends (but minus the time of the reverb tail). This is a technique I've used a lot to build dynamic music systems where one part needs to be played after another, but with the reverb still playing out afterwards.

    This is useful for playing different parts one after another when you can't know what reverb tail is going to be needed, unlike a single loop.

    Essentially, to do this, I'm using the technique I wrote about in this article: https://gamedevbeginner.com/ultimate-guide-to-playscheduled-in-unity/#queue_clips except that, instead of queuing a different clip to play at the end of first, I'm adding an offset to account for the reverb. It helps to know exactly how long the offset will be so usually when I make the file I'll add a tail that is, for example, 1 bar or 2 beats so that I can calculate an exact time figure. Then I can get the exact length with 60d / bpm * number of beats.

    To keep it running forever I usually record the start time and then have a look-ahead of a couple of seconds and calculate the next start time when it gets close.

    Hope that helps. I realise it's not a copy/paste answer but, hopefully, the article can show you some of your options.
     
  3. UNTL1

    UNTL1

    Joined:
    Mar 30, 2020
    Posts:
    71
    Thanks for the suggestion, though the main thing I'm asking here is basically why doesn't that first invoke of the method, when called from the Start method, run for the correct amount of time if I don't invoke it with enough delay?

    And regarding "baking" the fade in reverb into the audio clip, the music in this case has staccatoed guitar note and drums, and the sound that would be played over the repeat audio is cymbals ringing out and some reverbed guitar notes, so having that playing right from the start would muddy the sound.