Scorr / RetroUnity

Unity frontend for the libretro API.
GNU General Public License v3.0
41 stars 23 forks source link

Sound delay fix #14

Closed Hokage3211 closed 4 years ago

Hokage3211 commented 4 years ago

Hey so I downloaded your project and ended up seeing the sound wasn't working (or rather, has a significant delay to it), and in case you're still around (or someone else needs this) the fix for that sound thing, for whatever reason, is to set the number "2" on line 347 of LibretroWrapper.cs to 1.9f, and it seems to stop all delay and keep the correct pitch of the audio.

No idea why this works, but it does.

supneo commented 4 years ago

Hello, interesting, thanks for the contribution.

Hokage3211 commented 4 years ago

Actually, that only was a temp fix as it breaks down over time, but this solution seems to work better if you simply delay to start reading audio

        bool readAudio = false;
        private unsafe void RetroAudioSampleBatch(short* data, uint frames) {
            if (!readAudio && Time.time > 1.5f)
                readAudio = true;
            if (readAudio)
            {
                for (int i = 0; i < frames * 2; ++i)
                {
                    float value = data[i] * 0.000030517578125f;
                    value = Mathf.Clamp(value, -1.0f, 1.0f); // Unity's audio only takes values between -1 and 1.
                    AudioBatch.Add(value);
                }
            }
        }
Hokage3211 commented 4 years ago

Also simply skipping the first thousand of reads (in case the game isn't initialized on load) also seems to fix it, and that's contained in my pull request