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

Gapless looping of mp3 samples not gapless

Discussion in 'iOS and tvOS' started by AdbC99, Jul 11, 2012.

  1. AdbC99

    AdbC99

    Joined:
    Jan 18, 2012
    Posts:
    53
    I found myself trying to get samples to loop and finding for some reason that whenever anything (not just Unity) exports an mp3 it adds some stuff at the beginning so that the samples are not the same size. Gapless looping seems to remove some, but not all of that. If you play white noise through it then you can hear the breaks in the noise. Since unity on iOS is always mp3 then it means you can never actually loop anything in time, you will be somewhere around 0.05s out every loop (it seems to vary a bit). I tried adding a fudge factor to compensate, but even when you use the highly specific Play(ulong delay) command you still get gaps and variance. Anyone else worked a way around this? or have any kind of solution?
     
  2. AdbC99

    AdbC99

    Joined:
    Jan 18, 2012
    Posts:
    53
    I've just written some code to record the exact wav file coming out and I've found the problem. When the sample is converted to mp3 a gap is added to the beginning of the sample, this offsets any synchronised triggering you might want to do by that gap. When you loop the sample with gapless looping then it seems to recognise this gap and remove it every time it loops; but not from the beginning, it means exact sample triggering is not possible with compressed sounds. With uncompressed sounds then sample triggering to within a few samples can be achieved; however on iOS source.Play((ulong)delay) is not accurate enough. In which case, all samples that are synchronised together must be triggered together and loop together and then volume must be used to crossfade sample in and out, unfortunately that is cpu intensive having all these voices active at once.
     
  3. AdbC99

    AdbC99

    Joined:
    Jan 18, 2012
    Posts:
    53
    Actually it's worse than above, I've found that the feature genuinely just does not work
     
  4. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    The mp3 realtime decoding is handles by hardware, and Unity only has so much access to it (actually interfacing with it through Xcode doesn't expose many options), so it's rather arbitrary. It plays back when it plays back, that's about it.

    For more precise sound triggering, you can either play it without hardware decoding, or use wav, which will allow you much tighter control (with 10x the memory overhead).
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    My trick is to add a bit of empty space onto the end of the file. Yes it sucks, but it is what it tis.