/ Examples / MusicAPI / FirstJS

Right-click to copy examples to your workspace


Here's the simplest example of combining JavaScript with our musicAPI. The example relies on the presence of Vital on your system. You can modify your script to employ any CLAP synth in your kit including Hz.Syntho, Hz.Samplo or FluidSynth.

The Code

let ascene = await Ascene.BeginFiber(this);
let id = `t${this.GetId()}:`;
console.log(`${id} Audio scene is ready to go.`);

let noteDur = ascene.Seconds(.05 + .2 * Math.random());
let synth = "Vital"; // "Hz.Syntho", "FluidSynth", "Hz.Samplo"
console.log(`${id} using ${synth} -------------`);
let inst = ascene.NewAnode(synth);
let dac = ascene.GetDAC();
ascene.Connect(inst, dac);
await ascene.Sync();
for(let j=0;j<10;j++)
{
    console.log(`${id}loop ${j}`);
    for(let key=45; key<=65;key++)
    {
        inst.NoteOn(key, .8);
        await ascene.Wait(noteDur);
        inst.NoteOff(key, .8);
        yield;
    }
}
console.log(`${id}done!`);

More Examples

We've included a few additional small examples in the firstjs subdirectory for your perusal. Some of them may assume the presence of a MIDI input device or one or multiple third-party CLAP plugins.

home .. topics .. interface .. reference .. examples .. tipjar