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. Dismiss Notice

Moving to precise position audio track

Discussion in 'Scripting' started by BrainMelter, Jul 19, 2014.

  1. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    I have a compressed audio clip that's a few minutes long. Its load type is set to 'Compressed in memory'.

    If I try to set the audio's position using AudioSource.time, a delay of about 70 ms is introduced. I need very precise timing in my game, so this won't work for me. The problem only occurs when when the clip is compressed.

    The Unity documentation for AudioSource.time talks a bit about this, and points to AudioSource.timeSamples as a way of getting more accuracy. Unfortunately, when I use timeSamples, I get the exact same delay of about 70ms.

    I read somewhere that AudioSource.PlayScheduled might help, but I again get the same delay problems.

    Is this possible with Unity? Or am I just doing something wrong?
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi,

    Compressed audio will not allow for the kind of accuracy you are looking for. You'll need to have your load settings set to decompress on load for lowest latency and accurate position playback / scheduling. Of course, for long tracks decompress on load is sometimes simply not feasible - you might not be able to afford the memory.

    Cheers,

    Gregzo
     
  3. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    For the audio I have now, it will fit into memory. But there is a good chance I'll have to add more later, so it might not fit then. Plus there is a pretty good time hit for decompression when you load up the app.

    Another strange thing is this: if I select 'stream from disk' instead of 'compressed in memory', the accuracy gets a lot better. This makes no sense to me ...
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Can you seek to 70 ms later to compensate?
     
  5. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    I could if I always knew it would exactly be 70 ms, but this could vary across tracks and devices.

    Investigating some more, it seems track format makes some difference. I was using Ogg Vorbis compressed in memory, and it gave pretty bad results. But Ogg Vorbis streamed from disk is better. MP3 compressed in memory is better too. So this sort of solves my problem, or at least gives me acceptable accuracy.