Gapless looping in unity3d

2019-04-10 01:10发布

I've been struggling with this for some days already now ... :(

I've read these threads: Gapless-MP3-playback, Gapless looping on iOS, Gapless looping on iOS Producing seamless looping mp3s. Also a bit of theory here: mp3Loops

I am doing as instructed: Import WAV files, select MPEG encoding, check the "gapless looping" box

  • just a note for future googlers. In very old versions of Unity, there was a "gapless looping" button (removed since about 2014). In fact, that button was completely broken and did nothing.

and make it loop:

    var gameObject2 = new GameObject("MyObject");
    audioSource2 = gameObject2.AddComponent<AudioSource>();

    var filename = "test01_1152x";
    audioClip2 = Resources.Load (filename) as AudioClip;
    audioSource2.clip = audioClip2;
    audioSource2.loop = true;
    audioSource2.Play();

However the "pop" sound is terribly obvious.

I wonder what am I missing?

Checking "gapless looping" is just a bit better but it does not avoid the "pop" sound 100%. If the file given is wrong or not best for this situation, what conditions should the file fullfill in order to have a nice gapless looping bgm?

These are the pictures of various settings I've tried, including the original wave file

Original Wave: Loops nicely, because it is WAV. enter image description here

Compressed as MPEG (Non gapless looping selected), "pop" sound is super obvious while listeninig it. enter image description here

Compressed as MPEG (Gapless looping selected), "pop" sound is still obvious, just a bit better than setting. enter image description here

标签: audio unity3d
2条回答
一纸荒年 Trace。
2楼-- · 2019-04-10 01:37

it is easy to see that the clips are not well suited for gapless looping. I don't need to play those clips to listen to the clic and the pop you mention, it is pretty obvious from the waveform you posted. Think of audio clips as repeatable textures or seamless patterns, the start and end needs to match, and the easiest way to achieve this is by cutting the clip when the amplitude of the wave is 0.

See how smooth is the "sin" wave on the WAV format compared to the other formats? The reason is that compressed format like MP3 loss quality and information, deteriorating the quality. See that the compressed waves don't start smooth, they start with a straight line which will render as noise.

So if you increase the kbps (less compression) you will have far better results. But still I strongly suggest that you go with the WAV format for better results, this is what Unity states for short sounds:

"As a general rule of thumb, Compressed audio (or modules) are best for long files like background music or dialog, while Native is better for short sound effects"

Audio Clip - Importing Audio Assets

And regarding "have a nice gapless bgm" you need to use a good tool for that, one that allows you to cut, trim, stretch and test the audio loop before you export it to a wav file. I personally use Cakewalk SONAR. Here is an image of you compose a nice looping sound, see how I cut the sound when the amplitude is near "0".

One last thing, if you have several sounds playing simultaneously you won't be able to take advantage of hardware decoding of MP3, most mobile devices only allow for one sound to be decompressed by hardware, the other sounds will be rendered by the CPU.

Loop Construction on SONAR X1

In this other picture I show how to properly cut a loopable sound from a longer piece that does not fade like in the previous example.

enter image description here

查看更多
三岁会撩人
3楼-- · 2019-04-10 01:43

You can't loop an mp3 in Unity.

It is clearly explained in the manual.

Simply convert to Ogg or Wav.

That's all there is to it.

Note that for this very purpose, the display at the bottom of the Inspector clearly shows you that you cannot loop an mp3.

Here's the same sound as mp3 versus wav:

enter image description here

Obviously it is trivial to convert audio files between formats; use any software on your Mac or PC to do so. (Example, TwistedWave.)

(Note, the explanations here from audio engineers, about how to make the two ends of a file loop smoothly in an artistic sense, are completely unrelated to the question of Unity3D / mp3.)

查看更多
登录 后发表回答