Audio Loops in Unity still not being played right.

Hello guys,

I work as a music composer at a videogames company. As many of you already know, there are still issues with Unity, being able to play an audio loop, just the way you exported it. I have searched older post on the internet, that talked about the issue, and nowdays its still adding about 0.2 secs or so, before it starts the loop again, killing the sense of what a loop really is. I’ve seen this “jump” in recent good games etc. I’d like to have some light on the isssue, at least to understand why technically its being so difficult for the program, it process of whatever you know about it. Thank to all.

I can’t recreate your issue here. To try I created a simple sine wave in my DAW, making sure the number of cycles fit exactly into the time length of the loop. The loop amplitude starts and stops at 0. It plays back fine without any issue. Because I’m using such a simple wave form, I would expect to clearly hear any discontinuities if there were some milliseconds being added, but I don’t?

Just to make sure, I wrote the loop samples into an array and playing them back manually using the following code. Still no issues.

Note: this code is for diagnostic purposes only and does not check whether the number of channels in the audio clip matches the default speaker setup!

/*Note: this code is for diagnostic purposes only and does not check whether the number 
of channels in the audio clip matches the default speaker setup! */

void Start()
    {
        audioSource = GetComponent<AudioSource>();
        numberOfChannelsInClip = audioSource.clip.channels;
        clipData = new float[audioSource.clip.samples * numberOfChannelsInClip];
        audioSource.clip.GetData(clipData, 0);
    }

    void OnAudioFilterRead(float[] data, int channels)
    {
        if (clipData != null)
        {
            for (int i = 0; i < data.Length; i++, sampleIndex++)
            {
                if (sampleIndex >= clipData.Length)
                    sampleIndex = 0;

                data[i] = clipData[sampleIndex];
            }
        }
    }

And finally, to make absolutely sure everything was Ok, I created the same wave directly within my script and played that back (code below). Still no problems.

/*Note: this code is for diagnostic purposes only and does not check whether the number 
of channels in the audio clip matches the default speaker setup! */

void Start()
    {
        audioSource = GetComponent<AudioSource>();
        clipData = new float[44100];

        for(int i=0;i< 44100; i++)
        {
            clipData[i] = Mathf.Sin((220f* 2f * Mathf.PI * i)/44100f);
        }


    }

    void OnAudioFilterRead(float[] data, int channels)
    {
        if (clipData != null)
        {
            for (int i = 0; i < data.Length; i++, sampleIndex++)
            {
                //check we don't go outside array index
                if (sampleIndex >= clipData.Length)
                    sampleIndex = 0;
               // print(sampleIndex);

                data[i] = clipData[sampleIndex];
            }


        }
    }

I tried with both mp3’s and .ogg’s. I did read a few posts about issues with looping, but my tests seem fine. Can you post a sample loop that is creating problems for you so I can try it?

rorywalsh: Whoa, be careful with code that without verification assumes the channels in an AudioClip is guaranteed to match the number of actual audio channels for the current playback device. A mismatch can produce a very loud tone in the Nyquist frequency. This is very painful for the ears and can potentially cause hearing loss if the listener has turned the volume up a lot.

Dan Silva: If an AudioSource is set to loop, it’ll loop the clip nicely. If it doesn’t, the problem is in the audio file, not in Unity. Note that mp3 files doesn’t loop nicely by default because of a flaw with the file format. Since Unity recompresses audio assets you’ll want to use a lossless (e.g. wav) file anyway. If you’re looping your clips from code, look into PlayScheduled for how to play audio with more accurate timing.

@Nifflas_1 : My code doesn’t assume that the number of channels in the audio clip matches the number of channels in the default speaker mode. I will concede that using an audio clip with a different number of channels than the default speaker mode may produce some unwanted artifacts and repitch the audio clip, but hardly in a manner that might be damaging to one’s ears.

For what it’s worth, I will add a note to my previous post outlining that the code is merely for diagnostic purposes, and should be not be used for production purposes :wink:

Thank you both, I appreciate your answers.

About my audio file, is compressed as Vorbis by Unity after importing from Wav, and its checked to be perfect in the loop, checked in Logic. So the matter of the small delay, in the Loop is happening in Unity. However, it may have to be related to what you have mentioned. Your answers are good ones, and of course this I will pass all to the programmers. Many people will not pay attention to this, and the 0.2 seconds delay in looping is not the end of the world. But as a musician, I know you understand me exactly. So this thing is worth to be checked to take care of the details. Thank you guys for your approach, I will check all with the programmers. If you have additional insight on this, let me know. As you may know, this issue has been there for some time. Great info guys. thanks

Haven’t touched audio for some time, but I remember having this issue due to a mismatch of sample rates between Unity settings and my Audio files. Might want to look at that.

1 Like

Thank you, NA-RA-KU. good info. Will test today.