Skip to content

Latest commit

 

History

History
160 lines (126 loc) · 4.59 KB

JAVASCRIPT.md

File metadata and controls

160 lines (126 loc) · 4.59 KB

ReceiveMIDI JavaScript support

ReceiveMIDI supports running JavaScript code for each received MIDI message. By using the individual show MIDI message commands, the messages can be filtered out beforehand.

Note that this not fully standards-compliant, and is not as fast as the fancy JIT-compiled engines that you get in browsers, but this is an extremely compact, low-overhead JavaScript interpreter that comes with JUCE.

JavaScript code can be provided directly on the command line by using js or javascript, but the code can also be read from a file by using the jsf and javascript-file commands.

Only one script can be active, the last code that is provided will be the code that is executed.

The engine that is running the code is stateful and processes all the MIDI messages with the same code. This can be used to make more complex decisions about what to do by looking at multiple MIDI messages and keeping track of the state in global variables.

For instance, using the file osc.js:

receivemidi dev linnstrument jsf /path/to/osc.js

In osc.js an OSC connection is established and re-used for each message, and a count of note on messages is maintained:

if (osc === undefined)
{
    osc = OSC.connect('127.0.0.1', 12800);
}

if (MIDI.isNoteOn())
{ 
    if (count === undefined)
    {
        count = 0;
    }
    count++;

    osc.send('/note-on', count, MIDI.noteNumber(), MIDI.velocity());
}
else if (MIDI.isNoteOff())
{ 
    osc.send('/note-off', MIDI.noteNumber(), MIDI.velocity());
}

Additionally to all the usual JavaScript primitives and commands, ReceiveMIDI provides the following JavaScript functions:

General Utilities

Util.command('/full/path/to/executable arguments');
Util.command('/full/path/to/executable', 'argument one', 'argument two');
Util.print('some text');
Util.println('some text with newline');
Util.sleep(<milliseconds>);

Util.command with a single string splits it on whitespace, honoring double quotes: Util.command('say "two words"') passes two words as one argument, without the quotes. With several arguments each one reaches the program exactly as given, which avoids quoting altogether. It waits for the program to finish and prints its output, so it blocks just like Util.sleep does, for as long as the program runs.

Because the script runs synchronously for each received message, Util.sleep and Util.command hold up every message that arrives behind them, including the pass-through port. They're fine for occasional triggers, but a long-running program will delay the incoming stream for its duration.

Sending OSC messages

osc_sender = OSC.connect("hostname", port);
osc_sender.send("/osc/path", arg1, arg2, ...);

Whole numbers and booleans are sent as OSC int32 arguments, other numbers as float32 and everything else as strings. A number that happens to be whole is sent as an int32 even when it came from MIDI.floatVelocity().

Checking and retrieving data from the current MIDI message

MIDI.getRawData();        // array
MIDI.getRawDataSize();
MIDI.rawData();           // array
MIDI.rawDataSize();

MIDI.output();            // outputs the message to stdout like ReceiveMIDI formats it

MIDI.getDescription();
MIDI.description();

MIDI.getTimeStamp();
MIDI.getChannel();
MIDI.timeStamp();
MIDI.channel();

MIDI.isSysEx();
MIDI.getSysExData();      // array
MIDI.getSysExDataSize();
MIDI.sysExData();         // array
MIDI.sysExDataSize();

MIDI.isNoteOn();
MIDI.isNoteOff();
MIDI.isNoteOnOrOff();
MIDI.getNoteNumber();
MIDI.getVelocity();
MIDI.getFloatVelocity();
MIDI.noteNumber();
MIDI.velocity();
MIDI.floatVelocity();

MIDI.isSustainPedalOn();
MIDI.isSustainPedalOff();
MIDI.isSostenutoPedalOn();
MIDI.isSostenutoPedalOff();
MIDI.isSoftPedalOn();
MIDI.isSoftPedalOff();

MIDI.isProgramChange();
MIDI.getProgramChangeNumber();
MIDI.programChange();

MIDI.isPitchWheel();
MIDI.isPitchBend();
MIDI.getPitchWheelValue();
MIDI.getPitchBendValue();
MIDI.pitchWheel();
MIDI.pitchBend();

MIDI.isAftertouch();
MIDI.isPolyPressure();
MIDI.getAfterTouchValue();
MIDI.getPolyPressureValue();
MIDI.afterTouch();
MIDI.polyPressure();

MIDI.isChannelPressure();
MIDI.getChannelPressureValue();
MIDI.channelPressure();

MIDI.isController();
MIDI.getControllerNumber();
MIDI.getControllerValue();
MIDI.controllerNumber();
MIDI.controllerValue();

MIDI.isAllNotesOff();
MIDI.isAllSoundOff();
MIDI.isResetAllControllers();

MIDI.isActiveSense();
MIDI.isMidiStart();
MIDI.isMidiContinue();
MIDI.isMidiStop();
MIDI.isMidiClock();
MIDI.isSongPositionPointer();
MIDI.getSongPositionPointerMidiBeat();
MIDI.songPositionPointerMidiBeat();