Thealexbarney / VGAudio

A library for encoding, decoding, and manipulating audio files from video games.
MIT License
219 stars 37 forks source link

Super slow on .NET 5 WebAsm #123

Closed GEEKiDoS closed 1 year ago

GEEKiDoS commented 3 years ago

I'm building a web app for export acb for d4dj in one key with blazor wasm, but it's super slow, that takes lots of mins to encode a 2min audio file.

Even the WaveReader.Read() takes about 20 sec!

here's my code:

        async Task<(byte[], int)> EncodeHCA(byte[] audio)
        {
            var reader = new WaveReader();

            var writer = new HcaWriter
            {
                Configuration = new HcaConfiguration
                {
                    Bitrate = 128 * 1024,
                    Quality = CriHcaQuality.Highest,
                    TrimFile = false,
                    EncryptionKey = new CriHcaKey(0x59f449354d063308)
                }
            };

            var audioData = await Task.Run<AudioData>(() => reader.Read(audio));
            var format = audioData.GetFormat<Pcm16Format>();

            byte[] output = null;

            using (var ms = new MemoryStream())
            {
                await Task.Run(() => writer.WriteToStream(audioData, ms));
                await ms.FlushAsync();
                output = ms.ToArray();
            }

            return (output, (int)(format.SampleCount / (float)format.SampleRate * 1000));
        }

Or maybe it's a issue from dotnet5 idk

Thealexbarney commented 3 years ago

I'm not sure why that would happen. Is there any sort of profiling available for .NET wasm?

GEEKiDoS commented 3 years ago

I'm not sure why that would happen. Is there any sort of profiling available for .NET wasm?

I found a topic about this https://remibou.github.io/Profiling-in-Blazor/

Thealexbarney commented 3 years ago

What does it tell you when you profile the slow code?

GEEKiDoS commented 1 year ago

Problem solved! after upgrading to .NET 7 and enabling AOT, it is lighting fast