# Stuttering In Unity WebGL

**URL:** <https://qa.fmod.com/t/stuttering-in-unity-webgl/21757>\
**Category:** Unity\
**Created:** [June 13, 2024, 7:22am UTC](https://qa.fmod.com/t/stuttering-in-unity-webgl/21757 "2024-06-13T07:22:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MisakaCirno](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@MisakaCirno](https://qa.fmod.com/u/MisakaCirno)\
**Post date:** [June 13, 2024, 7:22am UTC](https://qa.fmod.com/t/stuttering-in-unity-webgl/21757/1 "2024-06-13T07:22:20Z")

</div>

We’re building a WebGL project using Unity, but during some operations that perform some switching of interfaces, the music playing makes a noticeable lagging sound. I noticed that the documentation mentions something about this and gives some sample code.

> [CPU Overhead](https://fmod.com/docs/2.02/api/platforms-html5.html#cpu-overhead)  
> By default FMOD mixes at 48000hz. If the browser is not running at that rate, it will introduce a resampler to convert the rate, which consumes CPU time and adds a DSP block worth of latency.  
> This can be solved by querying the hardware’s rate before [System::init](https://fmod.com/docs/2.02/api/core-api-system.html#system_init), and calling [System::setSoftwareFormat](https://fmod.com/docs/2.02/api/core-api-system.html#system_setsoftwareformat) to the same rate as the output. Here is an example (from the PlaySound example)
> 
> ```auto
> var outval = {};
> result = gSystem.getDriverInfo(0, null, null, outval, null, null);
> CHECK_RESULT(result);
> result = gSystem.setSoftwareFormat(outval.val, FMOD.SPEAKERMODE_DEFAULT, 0)
> CHECK_RESULT(result);
> 
> ```

> [Audio Stability (Stuttering)](https://fmod.com/docs/2.02/api/platforms-html5.html#audio-stability-stuttering)  
> Some devices cannot handle the default buffer size of 4 blocks of 1024 samples (4096 samples) without stuttering, so to avoid this set the buffer size in your application to 2 blocks of 2048.  
> Here is an example
> 
> ```auto
> result = gSystem.setDSPBufferSize(2048, 2); 
> CHECK_RESULT(result);
> 
> ```

But how do I use this code? They look like JS code rather than C# code.

If I want to do these operations in C#, where should I perform them?

For example, according to the description I should execute this code before the `System.init` method call, but I have not searched for a reference to the `System.init` method in the entire C# project for Unity.

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [June 18, 2024, 7:36am UTC](https://qa.fmod.com/t/stuttering-in-unity-webgl/21757/2 "2024-06-18T07:36:31Z")

</div>

The equivalent calls to a Core API system in C# are:

```auto
FMOD.RESULT result;
result = system.getDriverInfo(0, out System.Guid guid, out int systemrate, out FMOD.SPEAKERMODE speakermode, out int speakermodechannels);
result = system.setSoftwareFormat(systemrate, speakermode, 0);
result = system.setDSPBufferSize(2048, 2);

```

If you were accessing the core system from RuntimeManager, it would be the following instead:

```auto
FMOD.RESULT result;
result = FMODUnity.RuntimeManager.CoreSystem.getDriverInfo(0, out System.Guid guid, out int systemrate, out FMOD.SPEAKERMODE speakermode, out int speakermodechannels);
result = FMODUnity.RuntimeManager.CoreSystem.setSoftwareFormat(systemrate, speakermode, 0);
result = FMODUnity.RuntimeManager.CoreSystem.setDSPBufferSize(2048, 2);

```

You may not need all of the `out` arguments, so remove as needed.

These functions all need to be called before system initialization; the recommended way to do so with the Unity integration would be to use a custom handler that receives a callback before system initialization occurs. For more information on how to do this, see the [Callback Handler scripting example in our Unity documentation](https://www.fmod.com/docs/2.02/unity/examples-callback-handler.html).
