Jazz-Soft.net
We make it sound!
Home » Documentation » JZZ.js » Common calls

Common calls

or()

object.or(arg) - executes if the previous operation on the object failed.

If arg is function, it will execute in the current object's context, otherwise, it will be printed via the console.log.

and()

object.and(arg) - executes when the object is ready.

If arg is function, it will execute in the current object's context, otherwise, it will be printed via the console.log.

Example

engine = JZZ().or('Cannot start MIDI engine!').and('MIDI engine is ready!');

wait()

object.wait(delay) - returns a "delayed reference" of the object.

delay is the timeout in microseconds.

Example

Both code snippets below do the same timing:

port.wait(100).send(0x90,60,127)
    .wait(100).send(0x90,64,127)
    .wait(100).send(0x90,67,127);
 
port.wait(100).send(0x90,60,127);
port.wait(200).send(0x90,64,127);
port.wait(300).send(0x90,67,127);

log()

object.log() - returns the object's error log.

Example

JZZ().openMidiOut('MyMidiOut')
     .or(function(){ alert('Cannot open MyMidiOut!\n' + this.log()); })
     ...

Asynchronous calls

Most JZZ calls return Promise-s, and can be used in asynchronous context.

then()

promise.then(onSuccess, onFail) - handle the Promise returned by the previous call.

onSuccess(self) - if defined, is a function that receives the underlying object, usually, MIDI engine or MIDI port;

onFail(error) - if defined, is a function that receives the error caused by the previous call.

async/await

async/await style is available in modern environments.

Example

JZZ().openMidiOut().then(function(port) {
    port.noteOn(0,'C5',127).wait(500).noteOff(0,'C5').close();
}, function(err) {
    console.log('Cannot open port:', err.message);
});

async function playNote() {
    var midi = await JZZ();
    var port = await midi.openMidiOut();
    await port.noteOn(0,'C5',127);
    await port.wait(500);
    await port.noteOff(0,'C5');
    await port.close();
}

async function playAnotherNote() {
    var port = await JZZ().openMidiOut();
    await port.noteOn(0,'C5',127).wait(500).noteOff(0,'C5').close();
}

See also