Minimal Example using Csound for WebAssembly

This is a completely self-contained example, all in one file, and without any unnecessary features, showing how to play a Csound piece in a Web browser using Csound for WebAssembly. When the JavaScript file csound.js is loaded, it asynchronously loads the WebAssembly Csound library, then loads other scripts that create JavaScript wrappers for the library, thus producing a csound object with selected methods of the Csound API. A callback named moduleDidLoad is called once the csound object is ready to be used.

Csound automatically appends some messages to the text of the first HTML element named console. The Csound library prints its regular messages to stderr, which WebAssembly maps to window.console.warn. By defining a function handleMessage and assigning it to console.warn, all Csound messages are appended to a text area that is updated by handleMessage.

The complete CSD file of the Xanadu example from the Csound repository is stored in the text area below. Clicking on the Play button performs this CSD. Clicking on the Stop button ends the performance and resets Csound for another performance.