/ Examples / MIDI / MIDI Play

Right-click to copy examples to your workspace


// This simple example assumes a MIDI device is present on port 0.
// When invoked, we route notes and other MIDI events to the
// FluidSynth plugin.  Note that this simple example supports
// chords, pitch bending, etc.  The cancel trick affords the
// opportunity to "gracefully" shutdown using x in the Fibers Control Panel

let id = `t${this.GetId()}`;
let scene = await Ascene.BeginFiber(this);
let inst = scene.NewAnode("FluidSynth");
let dac = scene.GetDAC()
scene.verbose = 0;
scene.Chain(inst, dac);
await scene.Sync();

let port = 0;
let midiInput = new MidiIn(port);
let ok = await midiInput.Open();
midiInput.verbose = 1;
midiInput.preferClap = true;
while(true)
{
    let msg = yield; // allows for external cancellation
    if(msg == "_cancel_") break;
    let data = await midiInput.Recv(); // "blocks" until activity
    midiInput.Perform(inst, data);
}

console.log(`${id} done`);
home .. topics .. interface .. reference .. examples .. tipjar