Search Unity

Using TimeLine for Audio Beats

Discussion in 'Timeline' started by Sangemdoko, Sep 22, 2019.

  1. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    222
    Hi,

    I am new to using the TimeLine feature in Unity. I was hoping I could use it to Create tracks for a very simple rhythm game.
    In the editor I want to have my signals/markers on the beats but in game I want the audio to be delayed by x seconds so that I can use the signals/markers to spawn objects that will arrive at a certain position after x seconds (exactly on the beat)

    What's the best way to do this? Should I create a class that extends Playable Director (although it's not a monobehavior so it would be a bit of a pain) or TimeLineAsset?

    If I understand correctly I need to have some customized code at the moment the PlayableAsset is instantiated in the scene by the playable director or when the playable graph is being set?

    Or maybe the better idea is to create my own AudioClip track that has a parameter for audio delay only in play mode?

    I mean the easiest thing I could do is have two audio track with one that's shifted and muted in editor... but that's not really nice plus if I have mutliple audioclips with blends I would need to have the exact same ones on the other track.

    I'm looking forward to your insights.
     
  2. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    222
    Ah I forgot something that's unrelated to the first question but is there a way to add the custom markers in a submenu?
    I just want to organize my markers in sections like the signal Emitter. If there is a way to hide some of them that would also be great, as I can create them directly from an editor script
    upload_2019-9-22_11-54-11.png
     
  3. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    222
    Right now My approach is to delay the audio tracks before the timeLine is played

    So turns out you can't really duplicate a TimeLineAsset at run time because it references other assets.
    So what I do is that I delay all the audio tracks on awake, then I rebuild the playable graph and play the director

    Then on OnDestroy I put back the audiotrack to its original place. Not the best but it works
     
  4. ewanuno

    ewanuno

    Joined:
    May 11, 2017
    Posts:
    58
    this is one of those problems that has already been solved by other timeline based tools in the past.
    In Audio Daw programs this is called "latency compensation"
    basically every track (audio or midi) has a positive or negative delay in (often in ms.) associated with it.
    In the past this was super important because soundcards often has large buffer sizes that sometimes represented more than a second of audio, also since different synths and drum machines are all little embeded computers, they all have slightly different latencies between receiving a midi note message and starting to play the sound.
    latency compensation is also used creatively, for example to bring a sound with a slow attack forward in time so that the envelope peaks on the downbeat.
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The playable director has an option to use the DSP clock to drive the time of the timeline. The purpose is to keep the timeline in sync with the audio it is playing, and avoid linear slip that can happen with long audio sequences.

    Have you tried using that? Timeline schedules audio clips against the DSP clock, so I would expect any perceivable delay to be consistent.
     
  6. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    222
    Yes I'm using DSP clock and that does give me good timings for my events.

    I'm actually interested in how it works under the hood.
    Can I use the same clock to call functions outside of timeline? I know that I can use AudioSetting.DSPTime, but I'm asking for a way to have a function listen to that timer being updated.

    Is DSP clock faster than the UpdateClock? Or rather, can it update faster/slower than update or is it updating at the same frequency with micro delays to adjust the timing to stay consistent with the digital signal processing?

    I'm just wondering if it would be worth comparing the input I could get from a custom thread with the events coming from the timeline if its updating at DSP clock. The goal is to get extreme precision for the delta between the timing of the player pressing a button and the timing of the note reaching the perfect hit position.

    Whether that amount of precision is worth it or not I'm not quite sure yet...probably not.
     
  7. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The way it works is the DSP Clock is sampled at the beginning of the player update loop, and the delta time is for timeline update is based off that. Because the DSP clock sampling isn't nearly as precise as the regular clock, an estimate using the unscaled time is used to help smooth it out.

    So it progresses _about_ the same as unscaled time, but not exactly and the difference between the two can increase over time.

    Since timeline always schedules audio against the DSP clock, for short clips, the difference between DSP clock and Unscaled time should be negligible. For longer audio clips, DSP will be more accurate.

    So timeline is still sampled/played back in the player loop, but it uses the delta in the DSP clock.
     
  8. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    222
    Thank you! that's really useful to know