Limitations With Procedural streaming AudioClip on WebGL

I wanted to ask about what’s the limiting factor with AudioClips not allowing streaming PCM on the WebGL platform.

Below is a script that can be used to reproduce the issue.

using UnityEngine;

public class Test : MonoBehaviour
{
    public AudioSource audsrc = null;
    float x  = 0.0f;

    void Start()
    {
        // Self contained, creates is own GameObject, AudioSource and UI,
        // Only thing missing an AudioSource which is assumed to be somewhere
        // else in the scene (on the default Camera GameObject most likely).
        GameObject go = new GameObject("");
        this.audsrc = go.AddComponent<AudioSource>();
        this.audsrc.spatialBlend = 0.0f;
        this.audsrc.loop = true;
    }

    private void OnGUI()
    {
        if(GUILayout.Button("Play Mix Stream") == true)
            this.PlayProcClip(true);

        if(GUILayout.Button("Play Mix NoStream") == true)
            this.PlayProcClip(false);

        if(GUILayout.Button("Stop") == true)
        {
            this.audsrc.Stop();
        }
    }

    // Start an instance of the procedural audio with either the streaming
    // parameter as true or false.
    void PlayProcClip(bool stream)
    {
        x = 0.0f;
        this.audsrc.Stop();
        this.audsrc.clip = AudioClip.Create("", 4096, 1, 44100, stream, PCMReaderCallback );
        this.audsrc.Play();
    }

    // Create procedural PCM audio for a strobing sound effect
    void PCMReaderCallback(float[] data)
    {
        for(int i = 0; i < data.Length; ++i)
        {
            ++x;
            data[i] = Mathf.Sin(x * 0.05f) * Mathf.Sin(x * 0.001f);
        }
    }
}

With that sample code, on non-browser platforms, streaming works fine, but non-streaming stutters. Fair enough, I can just set streaming to true in those cases - BUT… on WebGL both streaming and non-streaming stutter. I do see an error in the browser console about streaming issues:

It only gives an error for streaming, but the issue happens regardless of what stream parameter is used. I also see there’s a vague mention of the streaming parameter on the WebGL audio page. (Unity - Manual: Audio in WebGL)

But, I was hoping to see if there was still a way to stream procedurally generated audio indefinitely. I was also hoping for more information on the issue - is this because of a limitation of Unity? FMOD? Browser implementations? the HTML5 spec? Emscripten?

Thanks.

this is a limitation of the webgl implementation. unfortunately the editor isn’t aware of the limitation and wont block you from doing this. FMOD isn’t used in the webgl. HTML can handle procedural audio.

1 Like