nativeformat / NFPlayerJS

A JavaScript/TypeScript audio engine for the Web and Server capable of multitrack time stretching, pitch shifting, declarative effects, faster than realtime processing, and more!
https://nativeformat.github.io/NFPlayerJS
Apache License 2.0
129 stars 9 forks source link

Worker Support #9

Open kirbysayshi opened 5 years ago

kirbysayshi commented 5 years ago

The player currently runs in the main thread. This is fine, as long as the quantum size is relatively large. When using a smaller quantum size, like 256, even shaking the mouse around will often cause dropouts.

There is very little to prevent the player from running in a WebWorker. The only issues are:

The ideal approach is probably something like this https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern#webaudio_powerhouse_audio_worklet_and_sharedarraybuffer. But SharedArrayBuffer is still not enabled in most browsers due to security concerns. And AudioWorklet is unimplemented everywhere but Chrome. In the meantime, an approach like the following might work:

It's questionable if, for the simple case, the above solution will be more resilient than running on the main thread (especially if we cut down on XAudioBuffer allocations). The power of the NFPlayer isn't really the realtime DSP aspect, more that composition, arrangement, and processing of existant audio. For anything other than immediate user-initiated triggers, a large latency window (8192 samples) is likely ok.