grimmdude / MidiWriterJS

♬ A JavaScript library which provides an API for programmatically generating and creating expressive multi-track MIDI files and JSON.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Browser: "Maximum call stack size exceeded error" when creating base64 with many events

christianhengst opened this issue · comments

I use MidiWriterJS in a practice app where MIDI files with a lot of events can occur due to a lot of repeats. I noticed that depending on the device and the number of events a Maximum call stack size exceeded error can occur when the "Writer" outputs the result as base64()

After taking a closer look at the code, I noticed that the problem occurs with String.fromCharCode.apply So the "bug" is actually there, but I have a suggested solution to work around the error in MidiWriterJS.

Here is a CodePen demonstrating the bug: https://codepen.io/christianhengst/pen/LYBWYGa?editors=0011
The result of base64() should be output to the console, but the page will stop responding. In the browser 10000 events work fine, CodePen already has problems with fewer events. Ideally, the bug should be reproduced in the browser as well.

As a solution for my app I split the buildFile into chunks, apply "String.fromCharCode.apply" to each chunk, and merge the string again afterward. In this way, the Maximum call stack size exceeded error is avoided and MIDI files with many many events are possible.

Here is a CodePen of my solution: https://codepen.io/christianhengst/pen/LYBWYOw?editors=0011

Maybe you have a better or different solution, just a suggestion.