Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gapless MP3 playback?

Discussion in 'iOS and tvOS' started by MikaMobile, Nov 5, 2010.

  1. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    Anyone had any luck getting gapless MP3 playback to work in Unity iPhone, such as for looping music? I've stumbled on some half-baked solutions so far, but nothing ideal just yet. Any pro tips?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Did you ensure that the track ends on the end of the audio frame (I think thats how its called?)

    From what I've been told by someone more savy on audio related programming, the issue there potentially roots to not ending on the right point which basically results in a "gap" that fills until the correct end.
    But my background knowledge on the matter (and how the frames etc in mp3 work) is insufficient to verify this meaningfully.
     
  3. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    I've imported test files that were supposedly prepared in this manner, only to find that they get silence added to them, or are cropped in an unusual way upon import. I have found a slightly hacky solution though which I'll be using unless someone comes up with a brilliant alternative.

    My first attempt at a hack solution was to have a script that simply re-started my music after X seconds elapsed, so I could essentially force the track to start over and ignore any bogus silence on the tail end. Unfortunately, this generated an irritating click/pop sound.

    So instead, I tried having 2 separate audio sources, each with the same mp3 track, and modified the above script to simply play them alternately after X seconds. So basically the song plays once, and then a copy of the song interrupts it at the very last moment, and then back to the first one, etc. etc. It pretty much achieves the same result, though without ever technically "stopping" the track, and thus avoiding any nasty clicks or pops. To my surprise, it loops seamlessly regardless of whether the mp3 is told to decompress via hardware or not. However, "decompress on load" must be checked if hardware decoding is used, otherwise the nasty pop returns.

    Testing on various hardware to make sure it's bulletproof, so far so good.

    Edit: kinda breaks when your framerate takes a dive (i.e. busted old iPhone 3G's)
     
    Last edited: Nov 5, 2010
  4. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    My advice is to find a better audio engineer/source audio. I'm not a programmer... but my looped music track dropped in with no issues, with seamless loop.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Any information on what tool your audio engineer / you use and what MikaMobile is using?

    Just in case "its the pipeline prior the Unity Pipeline" :)
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I find the lack of a UT response for this question disturbing. :(
     
  7. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Garageband! Actually, any old audio tool will do.... you just have to ensure there's a clean loop in the audio file itself before exporting it and throwing into Unity. It's not really the tool, it's the user ;)
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That is both rude and false. Please demonstrate your skills in a video; I haven't seen any evidence from this community to cause me to believe you.
     
  9. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    What? lol I don't see how it's rude. Wow. I really don't get what the big deal is. Looping music seamlessly? Erm, that's a very easy task for any audio engineer, so I don't understand. Your response is very terse and unnecessary, Jessy, and for that you get zip.


    p.s @ Mikamobile I can take a look at the audio file for you if you wish just send me a P.M. and I'll see how I can help you.
     
    Last edited: Nov 5, 2010
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I understand that you don't understand. You haven't read the thread and have nothing to contribute. Why are you even posting here if you don't actually use Unity iOS?
     
    Last edited: Nov 5, 2010
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I've heard of this issue cropping up before with fmod. I think they fixed it though, so it may be that unity uses an older build of fmod for iphone.
     
  12. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    I'm afraid Jessy is right groovfruit, you're misunderstanding the issue at hand. Making a looping source file is trivial, yes - getting a compressed MP3 to loop seamlessly in Unity is complicated due to the nature of the format and how Unity imports assets. Here's some reading that should clarify a bit.

    http://www.compuphase.com/mp3/mp3loops.htm

    In theory, authoring a file who's length is a multiple of exactly 1152 samples (the size of an MP3 "frame") should make a file that requires no additional padding to be added by the encoder, and yet Unity appears to be re-encoding my source files upon import, thus adding a tiny sliver of silence to the front end of files that shouldn't need it. I'm in contact with the Unity folks about this issue.
     
  13. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    @ jessy btw i do use unity for iphone. The thread started with mika saying he couldn't loop his music on iphone and I was following dreamora's lead in regards to the correct editing of audio files.
     
    Last edited: Nov 6, 2010
  14. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    All i've been trying to say Mika is that I don't understand, as my music loops quite fine using Unity for iphone. Sorry if I've misunderstood anything along the way, but I only found it odd when I haven't had an issue.

    However, can I mention that over the years, we've found FMOD to be a very buggy unstable audio engine in general (for console game development). I don't know why Unity would choose to go with it, apart from being popular, doesn't mean it's good.
     
    Last edited: Nov 6, 2010
  15. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Jessy is a very helpful guy and you have misunderstood the issue. Anyone can make looping mp3 tracks. The problem is looping mp3 track playback in unity on iPhone.
     
  16. sealiongames

    sealiongames

    Joined:
    Oct 6, 2008
    Posts:
    98
    >> MikaMobile - However, "decompress on load" must be checked if hardware decoding is used, otherwise the nasty pop returns.

    doesn't that just make it behave like an uncompressed track / wav?
     
  17. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    yes it does
    but its the only way as there is a problem with the hw decoder in the fmod version used when it comes to streaming.
     
  18. jerrodputman

    jerrodputman

    Joined:
    Jun 4, 2008
    Posts:
    181
    I'm seeing this as well. Ironically, I get better results by just importing the uncompressed AIFF from GarageBand and then having Unity perform the compression (there's still a minuscule gap when it loops though... so close).

    Personally, I don't think we should have to worry about "MP3 frames" and whatnot. If Unity can import the uncompressed file, then compress into a format compatible with the iPhone, it should be able to get it right itself.

    edit: Additionally, after Unity performs the compression, it introduces a gap in the Editor as well. I mean, really...
     
  19. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Just thought of a suggestion... I'm not sure how could this be achieved in code....

    If you're having issues with a gap at the end of your loop music, can you set up a hack by playing (via code) the song, then waiting x seconds, before the code kicks off the song again just before the first version of the song ends, thereby filling the gap...the repeat infinitely. erm does this make sense???

    Basically what I'm suggesting is creating a sort of layering effect. You would have to time number of frames/seconds to the point where you want song version 2 to kick in.
     
  20. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    That's similar to my hack solution that I tried (from my second post in this thread). It works as long as the framerate is perfect, but unexpected performance hitches at the loop point can cause this technique to fail. If I were only authoring for 3GS and higher I'd say its a satisfactory workaround, but it breaks often enough on 3G that I can't ship with it.
     
  21. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    This isn't really Unity's fault, MP3 files simply have to adhere to this rule. Theoretically, when I tell an audio clip to "loop" in Unity, Unity's encoding process would automagically crop the source file to the closest sample duration it needs to make a seamless loop, but this could cause undesirable edits (should it shave off the front, end, or both?). I'd rather be responsible for providing a precise file than having Unity take its best guess as to how to chop the waveform to fit nicely into a multiple of 1152 samples.

    I'm still waiting on some explanation for why Unity's encoder seems to want to add a frame of silence no matter what the length of the source file though.
     
  22. jerrodputman

    jerrodputman

    Joined:
    Jun 4, 2008
    Posts:
    181
    Well, would using AAC files produce a different result on the iPhone then, since (according to this Q&A http://developer.apple.com/library/ios/#qa/qa2009/qa1636.html) it looks like the audio player can be set up to skip the priming and remainder frames. It even says AVAudioPlayer does all of that automatically.

    edit: By the way, this is all "discussion" type stuff. The real issue, as you said, is that Unity is adding a frame of silence, even on properly created MP3 files.
     
    Last edited: Nov 6, 2010
  23. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    They said AAC was too much trouble and dropped it for 3.0.
     
  24. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    AAC / M4A files aren't supported by U3 as far as I recall
     
  25. jerrodputman

    jerrodputman

    Joined:
    Jun 4, 2008
    Posts:
    181
    Well, that's fun.

    In any case, I just tried an MP3 exported directly from GarageBand (with no "frame fixing" performed on it) in a sample app that uses AVAudioPlayer, and it looped properly (both in Simulator and on device). Meanwhile, the same file doesn't loop properly in Unity (either in the editor or on the device).

    I was thinking I may just write the music player in Objective-C, except that we actually intended our next game to be multi-platform. :| Plus, the music wouldn't be playing in the editor while we're making the game.
     
  26. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Not that much fun, was pointed out XX postings above that it is an FMOD thing
     
  27. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,551
    That's weird, both in the desktop and iPhone version of the game I'm making, I never had any trouble with seamlessly looping the music. I never notice any silent gaps in the music in-game.

    EDIT: I take that back, I just noticed now a short gap on the music in my game's iPhone build. I just wasn't paying attention to it before.
     
    Last edited: Nov 8, 2010
  28. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257

    Glad I'm not the only one! :p
     
  29. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    On the desktop you don't have it, on mobile you don't have it if you don't use compressed audio (or more than 1 in which case no hardware decoding takes place anymore at all - but to pull this off you need a new device with enough cpu power and RAM to handle the load, a 3G etc stands no chance)
     
  30. thimothy

    thimothy

    Joined:
    Nov 9, 2010
    Posts:
    5
    I fully support Jessy in this regard. He has always been helpful to each one of us by providing accurate answers to our queries. So I would request everyone to leave him alone. Regarding Mika’s query I would like to point out that it is possible to loop music on iPhone. I have got a unity iPhone and I have been regularly doing this. I really wonder why Mika is not able to do that in his phone. I would advise him to seek professional help.
     
    Last edited: Nov 15, 2010
  31. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,551
    I pictured in my mind being advised to go to a shrink when I read this.
     
  32. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Gapless mp3 playback now exists in 3.2

    Yay! :D
     
  33. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921

    I know this is an old thread, but I'm on Unity 3.4:

    Audio set up:

    WAV file imported into Unity (Large Background File)
    Audio Format: Compressed (MPEG)
    3d Sound: OFF
    Force to mono: ON
    Load Type: Stream from disk
    Hardware Decoding: ON
    Gapless Looping: ON

    I get a popping noise when the MPEG loops. Can someone tell me what I'm doing wrong or is this issue not fixed?

    I also tried this:
    Audio Format: Compressed (MPEG)
    3d Sound: OFF
    Force to mono: ON
    Load Type: Compressed in memory
    Hardware Decoding: ON
    Gapless Looping: ON

    No popping noise, but a gap.

    I opened Angrybots to see how unity handled sound. All but one sound is native WAV (not compressed at all) and most are Compressed in memory and Decompress on load, but it's hit and miss. The sound length vary.


    Thanks so much.
     
    Last edited: Dec 17, 2011
  34. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    That's weird, I've been using pretty much the exact setup you list first above (uncompressed source file, etc.) and have had no problems with my looping music. I've used the gapless looping feature for my last two games, so it definitely works as advertised. The only thing I see different with your settings vs. mine is that I don't force to mono. Do you still get a pop if you turn that off?
     
  35. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Yup, it still pops.

    Does compression really matter (pertaining to performance) if it's streaming from disk?

    Hmmm... Seems some of the sound files pop or have a gap and others don't.
     
    Last edited: Dec 17, 2011
  36. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    I'm not sure of the performance hit, but at a minimum your app would be ridiculously huge. Uncompressed wav files that are a couple minutes long are going to be gigantic.

    Are you 100% sure your source files are clean and uncompressed?
     
  37. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    "Sounds" to me like the pop probably exists in the source (audio file) itself.... Put the file into an audio editor and expand it right out - check the end - make sure the wave is clean with no weird spikes
     
  38. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    I'll check in to this tonight!

    Thanks guys!
     
  39. MariuszKowalczyk

    MariuszKowalczyk

    Joined:
    Nov 29, 2011
    Posts:
    301
    When I check the "Gapless looping" I hear cracks. It's the same in the editor and on the actual device. I have tried every combination of options in the Audio importer. Any ideas how to fix this?
     
    Last edited: Dec 21, 2011
  40. todeskurve

    todeskurve

    Joined:
    Oct 25, 2015
    Posts:
    1
    Mariusz, did you manage to fix it?