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.
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.
engine = JZZ().or('Cannot start MIDI engine!').and('MIDI engine is ready!');
object.wait(delay) - returns a "delayed reference" of the object.
delay is the timeout in microseconds.
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);
object.log() - returns the object's error log.
JZZ().openMidiOut('MyMidiOut')
.or(function(){ alert('Cannot open MyMidiOut!\n' + this.log()); })
...
Most JZZ calls return Promise-s, and can be used in asynchronous context.
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 style is available in modern environments.
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();
}