Using the Web Workers API to offload computations from the UI thread

As you might have noticed from the previous example when you use the Sample GANSynth note for the lead synth button, the audio freezes (you won't hear any sound coming from MusicVAE) while GANSynth generates its first sample.

This is because JavaScript's concurrency is built on the event loop pattern, meaning that JavaScript is not multithreaded, and everything is executed in a single thread called the UI thread. This works well because JavaScript uses non-blocking I/O, meaning most of its costly operations complete immediately, and return their values using events and callbacks. Nonetheless, if a long computation is synchronous, it will block the UI thread while it executes, ...

Get Hands-On Music Generation with Magenta now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.