webmidi
Version:
WEBMIDI.js makes it easy to talk to MIDI instruments from a browser or from Node.js. It simplifies the control of external or virtual MIDI instruments with functions such as playNote(), sendPitchBend(), sendControlChange(), etc. It also allows reacting to
1,254 lines (1,165 loc) • 296 kB
TypeScript
// Type definitions for WEBMIDI.js v3.1.12
// Project: https://webmidijs.org
// Definitions by: Jean-Philippe Côté <https://github.com/djipco/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// The type definitions for the native 'Navigator' interface and 'WebMidiApi' namespace have been
// created by Toshiya Nakakura <https://github.com/nakakura>. I am copying them here until I figure
// out how to rename a TypeScript namespace upon import. The problem is that the original 'WebMidi'
// namespace collides with the 'WebMidi' object. So, I simply renamed the namespace to 'WebMidiApi'
// and adjusted the references accordingly.
interface Navigator {
/**
* When invoked, returns a Promise object representing a request for access to MIDI devices on the
* user's system.
*/
requestMIDIAccess(options?: WebMidiApi.MIDIOptions): Promise<WebMidiApi.MIDIAccess>;
}
declare namespace WebMidiApi {
interface MIDIOptions {
/**
* This member informs the system whether the ability to send and receive system
* exclusive messages is requested or allowed on a given MIDIAccess object.
*/
sysex: boolean;
}
/**
* This is a maplike interface whose value is a MIDIInput instance and key is its
* ID.
*/
type MIDIInputMap = Map<string, MIDIInput>;
/**
* This is a maplike interface whose value is a MIDIOutput instance and key is its
* ID.
*/
type MIDIOutputMap = Map<string, MIDIOutput>;
interface MIDIAccess extends EventTarget {
/**
* The MIDI input ports available to the system.
*/
inputs: MIDIInputMap;
/**
* The MIDI output ports available to the system.
*/
outputs: MIDIOutputMap;
/**
* The handler called when a new port is connected or an existing port changes the
* state attribute.
*/
onstatechange(e: MIDIConnectionEvent): void;
addEventListener(
type: 'statechange',
listener: (this: this, e: MIDIConnectionEvent) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
/**
* This attribute informs the user whether system exclusive support is enabled on
* this MIDIAccess.
*/
sysexEnabled: boolean;
}
type MIDIPortType = 'input' | 'output';
type MIDIPortDeviceState = 'disconnected' | 'connected';
type MIDIPortConnectionState = 'open' | 'closed' | 'pending';
interface MIDIPort extends EventTarget {
/**
* A unique ID of the port. This can be used by developers to remember ports the
* user has chosen for their application.
*/
id: string;
/**
* The manufacturer of the port.
*/
manufacturer?: string | undefined;
/**
* The system name of the port.
*/
name?: string | undefined;
/**
* A descriptor property to distinguish whether the port is an input or an output
* port.
*/
type: MIDIPortType;
/**
* The version of the port.
*/
version?: string | undefined;
/**
* The state of the device.
*/
state: MIDIPortDeviceState;
/**
* The state of the connection to the device.
*/
connection: MIDIPortConnectionState;
/**
* The handler called when an existing port changes its state or connection
* attributes.
*/
onstatechange(e: MIDIConnectionEvent): void;
addEventListener(
type: 'statechange',
listener: (this: this, e: MIDIConnectionEvent) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
/**
* Makes the MIDI device corresponding to the MIDIPort explicitly available. Note
* that this call is NOT required in order to use the MIDIPort - calling send() on
* a MIDIOutput or attaching a MIDIMessageEvent handler on a MIDIInputPort will
* cause an implicit open().
*
* When invoked, this method returns a Promise object representing a request for
* access to the given MIDI port on the user's system.
*/
open(): Promise<MIDIPort>;
/**
* Makes the MIDI device corresponding to the MIDIPort
* explicitly unavailable (subsequently changing the state from "open" to
* "connected"). Note that successful invocation of this method will result in MIDI
* messages no longer being delivered to MIDIMessageEvent handlers on a
* MIDIInputPort (although setting a new handler will cause an implicit open()).
*
* When invoked, this method returns a Promise object representing a request for
* access to the given MIDI port on the user's system. When the port has been
* closed (and therefore, in exclusive access systems, the port is available to
* other applications), the vended Promise is resolved. If the port is
* disconnected, the Promise is rejected.
*/
close(): Promise<MIDIPort>;
}
interface MIDIInput extends MIDIPort {
type: 'input';
onmidimessage(e: MIDIMessageEvent): void;
addEventListener(
type: 'midimessage',
listener: (this: this, e: MIDIMessageEvent) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: 'statechange',
listener: (this: this, e: MIDIConnectionEvent) => any,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): void;
}
interface MIDIOutput extends MIDIPort {
type: 'output';
/**
* Enqueues the message to be sent to the corresponding MIDI port.
* @param data The data to be enqueued, with each sequence entry representing a single byte of data.
* @param timestamp The time at which to begin sending the data to the port. If timestamp is set
* to zero (or another time in the past), the data is to be sent as soon as
* possible.
*/
send(data: number[] | Uint8Array, timestamp?: number): void;
/**
* Clears any pending send data that has not yet been sent from the MIDIOutput 's
* queue. The implementation will need to ensure the MIDI stream is left in a good
* state, so if the output port is in the middle of a sysex message, a sysex
* termination byte (0xf7) should be sent.
*/
clear(): void;
}
interface MIDIMessageEvent extends Event {
/**
* A timestamp specifying when the event occurred.
*/
receivedTime: number;
/**
* A Uint8Array containing the MIDI data bytes of a single MIDI message.
*/
data: Uint8Array;
}
interface MIDIMessageEventInit extends EventInit {
/**
* A timestamp specifying when the event occurred.
*/
receivedTime: number;
/**
* A Uint8Array containing the MIDI data bytes of a single MIDI message.
*/
data: Uint8Array;
}
interface MIDIConnectionEvent extends Event {
/**
* The port that has been connected or disconnected.
*/
port: MIDIPort;
}
interface MIDIConnectionEventInit extends EventInit {
/**
* The port that has been connected or disconnected.
*/
port: MIDIPort;
}
}
/**
* The `EventEmitter` class provides methods to implement the _observable_ design pattern. This
* pattern allows one to _register_ a function to execute when a specific event is _emitted_ by the
* emitter.
*
* It is intended to be an abstract class meant to be extended by (or mixed into) other objects.
*/
export declare class EventEmitter {
/**
* Identifier (Symbol) to use when adding or removing a listener that should be triggered when any
* events occur.
*
* @type {Symbol}
*/
static get ANY_EVENT(): Symbol;
/**
* Creates a new `EventEmitter`object.
*
* @param {boolean} [eventsSuspended=false] Whether the `EventEmitter` is initially in a suspended
* state (i.e. not executing callbacks).
*/
constructor(eventsSuspended?: boolean);
/**
* An object containing a property for each event with at least one registered listener. Each
* event property contains an array of all the [`Listener`]{@link Listener} objects registered
* for the event.
*
* @type {Object}
* @readonly
*/
eventMap: any;
/**
* Whether or not the execution of callbacks is currently suspended for this emitter.
*
* @type {boolean}
*/
eventsSuspended: boolean;
/**
* Adds a listener for the specified event. It returns the [`Listener`]{@link Listener} object
* that was created and attached to the event.
*
* To attach a global listener that will be triggered for any events, use
* [`EventEmitter.ANY_EVENT`]{@link #ANY_EVENT} as the first parameter. Note that a global
* listener will also be triggered by non-registered events.
*
* @param {string|Symbol} event The event to listen to.
* @param {EventEmitterCallback} callback The callback function to execute when the event occurs.
* @param {Object} [options={}]
* @param {Object} [options.context=this] The value of `this` in the callback function.
* @param {boolean} [options.prepend=false] Whether the listener should be added at the beginning
* of the listeners array and thus executed first.
* @param {number} [options.duration=Infinity] The number of milliseconds before the listener
* automatically expires.
* @param {number} [options.remaining=Infinity] The number of times after which the callback
* should automatically be removed.
* @param {array} [options.arguments] An array of arguments which will be passed separately to the
* callback function. This array is stored in the [`arguments`]{@link Listener#arguments}
* property of the [`Listener`]{@link Listener} object and can be retrieved or modified as
* desired.
*
* @returns {Listener} The newly created [`Listener`]{@link Listener} object.
*
* @throws {TypeError} The `event` parameter must be a string or
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}.
* @throws {TypeError} The `callback` parameter must be a function.
*/
addListener(event: string | Symbol, callback: EventEmitterCallback, options?: {
context?: any;
prepend?: boolean;
duration?: number;
remaining?: number;
arguments?: any[];
}): Listener | Listener[];
/**
* Adds a one-time listener for the specified event. The listener will be executed once and then
* destroyed. It returns the [`Listener`]{@link Listener} object that was created and attached
* to the event.
*
* To attach a global listener that will be triggered for any events, use
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} as the first parameter. Note that a
* global listener will also be triggered by non-registered events.
*
* @param {string|Symbol} event The event to listen to
* @param {EventEmitterCallback} callback The callback function to execute when the event occurs
* @param {Object} [options={}]
* @param {Object} [options.context=this] The context to invoke the callback function in.
* @param {boolean} [options.prepend=false] Whether the listener should be added at the beginning
* of the listeners array and thus executed first.
* @param {number} [options.duration=Infinity] The number of milliseconds before the listener
* automatically expires.
* @param {array} [options.arguments] An array of arguments which will be passed separately to the
* callback function. This array is stored in the [`arguments`]{@link Listener#arguments}
* property of the [`Listener`]{@link Listener} object and can be retrieved or modified as
* desired.
*
* @returns {Listener} The newly created [`Listener`]{@link Listener} object.
*
* @throws {TypeError} The `event` parameter must be a string or
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}.
* @throws {TypeError} The `callback` parameter must be a function.
*/
addOneTimeListener(event: string | Symbol, callback: EventEmitterCallback, options?: {
context?: any;
prepend?: boolean;
duration?: number;
arguments?: any[];
}): Listener | Listener[];
/**
* Returns `true` if the specified event has at least one registered listener. If no event is
* specified, the method returns `true` if any event has at least one listener registered (this
* includes global listeners registered to
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}).
*
* Note: to specifically check for global listeners added with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}, use
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} as the parameter.
*
* @param {string|Symbol} [event=(any event)] The event to check
* @param {function|Listener} [callback=(any callback)] The actual function that was added to the
* event or the {@link Listener} object returned by `addListener()`.
* @returns {boolean}
*/
hasListener(event?: string | Symbol, callback?: Function | Listener): boolean;
/**
* An array of all the unique event names for which the emitter has at least one registered
* listener.
*
* Note: this excludes global events registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} because they are not tied to a
* specific event.
*
* @type {string[]}
* @readonly
*/
get eventNames(): string[];
/**
* Returns an array of all the [`Listener`]{@link Listener} objects that have been registered for
* a specific event.
*
* Please note that global events (those added with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}) are not returned for "regular"
* events. To get the list of global listeners, specifically use
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} as the parameter.
*
* @param {string|Symbol} event The event to get listeners for.
* @returns {Listener[]} An array of [`Listener`]{@link Listener} objects.
*/
getListeners(event: string | Symbol): Listener[];
/**
* Suspends execution of all callbacks functions registered for the specified event type.
*
* You can suspend execution of callbacks registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} by passing
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} to `suspendEvent()`. Beware that this
* will not suspend all callbacks but only those registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}. While this may seem counter-intuitive
* at first glance, it allows the selective suspension of global listeners while leaving other
* listeners alone. If you truly want to suspends all callbacks for a specific
* [`EventEmitter`]{@link EventEmitter}, simply set its `eventsSuspended` property to `true`.
*
* @param {string|Symbol} event The event name (or `EventEmitter.ANY_EVENT`) for which to suspend
* execution of all callback functions.
*/
suspendEvent(event: string | Symbol): void;
/**
* Resumes execution of all suspended callback functions registered for the specified event type.
*
* You can resume execution of callbacks registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} by passing
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} to `unsuspendEvent()`. Beware that
* this will not resume all callbacks but only those registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}. While this may seem
* counter-intuitive, it allows the selective unsuspension of global listeners while leaving other
* callbacks alone.
*
* @param {string|Symbol} event The event name (or `EventEmitter.ANY_EVENT`) for which to resume
* execution of all callback functions.
*/
unsuspendEvent(event: string | Symbol): void;
/**
* Returns the number of listeners registered for a specific event.
*
* Please note that global events (those added with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}) do not count towards the remaining
* number for a "regular" event. To get the number of global listeners, specifically use
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} as the parameter.
*
* @param {string|Symbol} event The event which is usually a string but can also be the special
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} symbol.
* @returns {number} An integer representing the number of listeners registered for the specified
* event.
*/
getListenerCount(event: string | Symbol): number;
/**
* Executes the callback function of all the [`Listener`]{@link Listener} objects registered for
* a given event. The callback functions are passed the additional arguments passed to `emit()`
* (if any) followed by the arguments present in the [`arguments`](Listener#arguments) property of
* the [`Listener`](Listener) object (if any).
*
* If the [`eventsSuspended`]{@link #eventsSuspended} property is `true` or the
* [`Listener.suspended`]{@link Listener#suspended} property is `true`, the callback functions
* will not be executed.
*
* This function returns an array containing the return values of each of the callbacks.
*
* It should be noted that the regular listeners are triggered first followed by the global
* listeners (those added with [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}).
*
* @param {string} event The event
* @param {...*} args Arbitrary number of arguments to pass along to the callback functions
*
* @returns {Array} An array containing the return value of each of the executed listener
* functions.
*
* @throws {TypeError} The `event` parameter must be a string.
*/
emit(event: string, ...args: any[]): any[];
/**
* Removes all the listeners that match the specified criterias. If no parameters are passed, all
* listeners will be removed. If only the `event` parameter is passed, all listeners for that
* event will be removed. You can remove global listeners by using
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} as the first parameter.
*
* To use more granular options, you must at least define the `event`. Then, you can specify the
* callback to match or one or more of the additional options.
*
* @param {string | Symbol} [event] The event name.
* @param {EventEmitterCallback} [callback] Only remove the listeners that match this exact
* callback function.
* @param {Object} [options]
* @param {*} [options.context] Only remove the listeners that have this exact context.
* @param {number} [options.remaining] Only remove the listener if it has exactly that many
* remaining times to be executed.
*/
removeListener(
event?: string | Symbol,
callback?: EventEmitterCallback,
options?: {
context?: any;
remaining?: number;
}
): void;
/**
* The `waitFor()` method is an async function which returns a promise. The promise is fulfilled
* when the specified event occurs. The event can be a regular event or
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} (if you want to resolve as soon as any
* event is emitted).
*
* If the `duration` option is set, the promise will only be fulfilled if the event is emitted
* within the specified duration. If the event has not been fulfilled after the specified
* duration, the promise is rejected. This makes it super easy to wait for an event and timeout
* after a certain time if the event is not triggered.
*
* @param {string|Symbol} event The event to wait for
* @param {Object} [options={}]
* @param {number} [options.duration=Infinity] The number of milliseconds to wait before the
* promise is automatically rejected.
*/
waitFor(event: string | Symbol, options?: {
duration?: number;
}): Promise<any>;
/**
* The number of unique events that have registered listeners.
*
* Note: this excludes global events registered with
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT} because they are not tied to a
* specific event.
*
* @type {number}
* @readonly
*/
get eventCount(): number;
}
/**
* The `Listener` class represents a single event listener object. Such objects keep all relevant
* contextual information such as the event being listened to, the object the listener was attached
* to, the callback function and so on.
*
*/
export declare class Listener {
/**
* Creates a new `Listener` object
*
* @param {string|Symbol} event The event being listened to
* @param {EventEmitter} target The [`EventEmitter`]{@link EventEmitter} object that the listener
* is attached to.
* @param {EventEmitterCallback} callback The function to call when the listener is triggered
* @param {Object} [options={}]
* @param {Object} [options.context=target] The context to invoke the listener in (a.k.a. the
* value of `this` inside the callback function).
* @param {number} [options.remaining=Infinity] The remaining number of times after which the
* callback should automatically be removed.
* @param {array} [options.arguments] An array of arguments that will be passed separately to the
* callback function upon execution. The array is stored in the [`arguments`]{@link #arguments}
* property and can be retrieved or modified as desired.
*
* @throws {TypeError} The `event` parameter must be a string or
* [`EventEmitter.ANY_EVENT`]{@link EventEmitter#ANY_EVENT}.
* @throws {ReferenceError} The `target` parameter is mandatory.
* @throws {TypeError} The `callback` must be a function.
*/
constructor(event: string | Symbol, target: EventEmitter, callback: EventEmitterCallback, options?: {
context?: any;
remaining?: number;
arguments?: any[];
}, ...args: any[]);
/**
* An array of arguments to pass to the callback function upon execution.
* @type {array}
*/
arguments: any[];
/**
* The callback function to execute.
* @type {Function}
*/
callback: Function;
/**
* The context to execute the callback function in (a.k.a. the value of `this` inside the
* callback function)
* @type {Object}
*/
context: any;
/**
* The number of times the listener function was executed.
* @type {number}
*/
count: number;
/**
* The event name.
* @type {string}
*/
event: string;
/**
* The remaining number of times after which the callback should automatically be removed.
* @type {number}
*/
remaining: number;
/**
* Whether this listener is currently suspended or not.
* @type {boolean}
*/
suspended: boolean;
/**
* The object that the event is attached to (or that emitted the event).
* @type {EventEmitter}
*/
target: EventEmitter;
/**
* Removes the listener from its target.
*/
remove(): void;
}
/**
* The `Enumerations` class contains enumerations and arrays of elements used throughout the
* library. All properties are static and should be referenced using the class name. For example:
* `Enumerations.CHANNEL_MESSAGES`.
*
* @license Apache-2.0
* @since 3.0.0
*/
export class Enumerations {
/**
* Enumeration of all MIDI channel message names and their associated 4-bit numerical value:
*
* | Message Name | Hexadecimal | Decimal |
* |---------------------|-------------|---------|
* | `noteoff` | 0x8 | 8 |
* | `noteon` | 0x9 | 9 |
* | `keyaftertouch` | 0xA | 10 |
* | `controlchange` | 0xB | 11 |
* | `programchange` | 0xC | 12 |
* | `channelaftertouch` | 0xD | 13 |
* | `pitchbend` | 0xE | 14 |
*
* @enum {Object.<string, number>}
* @readonly
* @since 3.1
* @static
*/
static get CHANNEL_MESSAGES(): {
noteoff: number;
noteon: number;
keyaftertouch: number;
controlchange: number;
programchange: number;
channelaftertouch: number;
pitchbend: number;
};
/**
* A simple array of the 16 valid MIDI channel numbers (`1` to `16`):
*
* @type {number[]}
* @readonly
* @static
*/
static get CHANNEL_NUMBERS(): number[];
/**
* Enumeration of all MIDI channel mode message names and their associated numerical value:
*
*
* | Message Name | Hexadecimal | Decimal |
* |-----------------------|-------------|---------|
* | `allsoundoff` | 0x78 | 120 |
* | `resetallcontrollers` | 0x79 | 121 |
* | `localcontrol` | 0x7A | 122 |
* | `allnotesoff` | 0x7B | 123 |
* | `omnimodeoff` | 0x7C | 124 |
* | `omnimodeon` | 0x7D | 125 |
* | `monomodeon` | 0x7E | 126 |
* | `polymodeon` | 0x7F | 127 |
*
* @enum {Object.<string, number>}
* @readonly
* @static
*/
static get CHANNEL_MODE_MESSAGES(): {
allsoundoff: number;
resetallcontrollers: number;
localcontrol: number;
allnotesoff: number;
omnimodeoff: number;
omnimodeon: number;
monomodeon: number;
polymodeon: number;
};
/**
* An array of objects, ordered by control number, describing control change messages. Each object
* in the array can have up to 4 properties:
*
* * `number`: MIDI control number (0-127);
* * `event`: name of emitted event (eg: `bankselectcoarse`, `choruslevel`, etc) that can be
* listened to;
* * `description`: user-friendly description of the controller's purpose;
* * `position`: whether this controller's value should be considered an `msb` or `lsb` (if
* appropriate).
*
* Not all controllers have a predefined function. For those that don't, name is the word
* "controller" followed by the number (e.g. `controller112`).
*
* | Event name | Control Number |
* |--------------------------------|----------------|
* | `bankselectcoarse` | 0 |
* | `modulationwheelcoarse` | 1 |
* | `breathcontrollercoarse` | 2 |
* | `controller3` | 3 |
* | `footcontrollercoarse` | 4 |
* | `portamentotimecoarse` | 5 |
* | `dataentrycoarse` | 6 |
* | `volumecoarse` | 7 |
* | `balancecoarse` | 8 |
* | `controller9` | 9 |
* | `pancoarse` | 10 |
* | `expressioncoarse` | 11 |
* | `effectcontrol1coarse` | 12 |
* | `effectcontrol2coarse` | 13 |
* | `controller14` | 14 |
* | `controller15` | 15 |
* | `generalpurposecontroller1` | 16 |
* | `generalpurposecontroller2` | 17 |
* | `generalpurposecontroller3` | 18 |
* | `generalpurposecontroller4` | 19 |
* | `controller20` | 20 |
* | `controller21` | 21 |
* | `controller22` | 22 |
* | `controller23` | 23 |
* | `controller24` | 24 |
* | `controller25` | 25 |
* | `controller26` | 26 |
* | `controller27` | 27 |
* | `controller28` | 28 |
* | `controller29` | 29 |
* | `controller30` | 30 |
* | `controller31` | 31 |
* | `bankselectfine` | 32 |
* | `modulationwheelfine` | 33 |
* | `breathcontrollerfine` | 34 |
* | `controller35` | 35 |
* | `footcontrollerfine` | 36 |
* | `portamentotimefine` | 37 |
* | `dataentryfine` | 38 |
* | `channelvolumefine` | 39 |
* | `balancefine` | 40 |
* | `controller41` | 41 |
* | `panfine` | 42 |
* | `expressionfine` | 43 |
* | `effectcontrol1fine` | 44 |
* | `effectcontrol2fine` | 45 |
* | `controller46` | 46 |
* | `controller47` | 47 |
* | `controller48` | 48 |
* | `controller49` | 49 |
* | `controller50` | 50 |
* | `controller51` | 51 |
* | `controller52` | 52 |
* | `controller53` | 53 |
* | `controller54` | 54 |
* | `controller55` | 55 |
* | `controller56` | 56 |
* | `controller57` | 57 |
* | `controller58` | 58 |
* | `controller59` | 59 |
* | `controller60` | 60 |
* | `controller61` | 61 |
* | `controller62` | 62 |
* | `controller63` | 63 |
* | `damperpedal` | 64 |
* | `portamento` | 65 |
* | `sostenuto` | 66 |
* | `softpedal` | 67 |
* | `legatopedal` | 68 |
* | `hold2` | 69 |
* | `soundvariation` | 70 |
* | `resonance` | 71 |
* | `releasetime` | 72 |
* | `attacktime` | 73 |
* | `brightness` | 74 |
* | `decaytime` | 75 |
* | `vibratorate` | 76 |
* | `vibratodepth` | 77 |
* | `vibratodelay` | 78 |
* | `controller79` | 79 |
* | `generalpurposecontroller5` | 80 |
* | `generalpurposecontroller6` | 81 |
* | `generalpurposecontroller7` | 82 |
* | `generalpurposecontroller8` | 83 |
* | `portamentocontrol` | 84 |
* | `controller85` | 85 |
* | `controller86` | 86 |
* | `controller87` | 87 |
* | `highresolutionvelocityprefix` | 88 |
* | `controller89` | 89 |
* | `controller90` | 90 |
* | `effect1depth` | 91 |
* | `effect2depth` | 92 |
* | `effect3depth` | 93 |
* | `effect4depth` | 94 |
* | `effect5depth` | 95 |
* | `dataincrement` | 96 |
* | `datadecrement` | 97 |
* | `nonregisteredparameterfine` | 98 |
* | `nonregisteredparametercoarse` | 99 |
* | `nonregisteredparameterfine` | 100 |
* | `registeredparametercoarse` | 101 |
* | `controller102` | 102 |
* | `controller103` | 103 |
* | `controller104` | 104 |
* | `controller105` | 105 |
* | `controller106` | 106 |
* | `controller107` | 107 |
* | `controller108` | 108 |
* | `controller109` | 109 |
* | `controller110` | 110 |
* | `controller111` | 111 |
* | `controller112` | 112 |
* | `controller113` | 113 |
* | `controller114` | 114 |
* | `controller115` | 115 |
* | `controller116` | 116 |
* | `controller117` | 117 |
* | `controller118` | 118 |
* | `controller119` | 119 |
* | `allsoundoff` | 120 |
* | `resetallcontrollers` | 121 |
* | `localcontrol` | 122 |
* | `allnotesoff` | 123 |
* | `omnimodeoff` | 124 |
* | `omnimodeon` | 125 |
* | `monomodeon` | 126 |
* | `polymodeon` | 127 |
*
* @type {Object[]}
* @readonly
* @static
* @since 3.1
*/
static get CONTROL_CHANGE_MESSAGES():object[]
/**
* Enumeration of all MIDI registered parameters and their associated pair of numerical values.
* MIDI registered parameters extend the original list of control change messages. Currently,
* there are only a limited number of them:
*
*
* | Control Function | [LSB, MSB] |
* |------------------------------|--------------|
* | `pitchbendrange` | [0x00, 0x00] |
* | `channelfinetuning` | [0x00, 0x01] |
* | `channelcoarsetuning` | [0x00, 0x02] |
* | `tuningprogram` | [0x00, 0x03] |
* | `tuningbank` | [0x00, 0x04] |
* | `modulationrange` | [0x00, 0x05] |
* | `azimuthangle` | [0x3D, 0x00] |
* | `elevationangle` | [0x3D, 0x01] |
* | `gain` | [0x3D, 0x02] |
* | `distanceratio` | [0x3D, 0x03] |
* | `maximumdistance` | [0x3D, 0x04] |
* | `maximumdistancegain` | [0x3D, 0x05] |
* | `referencedistanceratio` | [0x3D, 0x06] |
* | `panspreadangle` | [0x3D, 0x07] |
* | `rollangle` | [0x3D, 0x08] |
*
* @enum {Object.<string, number[]>}
* @readonly
* @static
*/
static get REGISTERED_PARAMETERS(): {
pitchbendrange: number[];
channelfinetuning: number[];
channelcoarsetuning: number[];
tuningprogram: number[];
tuningbank: number[];
modulationrange: number[];
azimuthangle: number[];
elevationangle: number[];
gain: number[];
distanceratio: number[];
maximumdistance: number[];
maximumdistancegain: number[];
referencedistanceratio: number[];
panspreadangle: number[];
rollangle: number[];
};
/**
* Enumeration of all valid MIDI system messages and matching numerical values. WebMidi.js also
* uses two additional custom messages.
*
* **System Common Messages**
*
* | Function | Hexadecimal | Decimal |
* |------------------------|-------------|---------|
* | `sysex` | 0xF0 | 240 |
* | `timecode` | 0xF1 | 241 |
* | `songposition` | 0xF2 | 242 |
* | `songselect` | 0xF3 | 243 |
* | `tunerequest` | 0xF6 | 246 |
* | `sysexend` | 0xF7 | 247 |
*
* The `sysexend` message is never actually received. It simply ends a sysex stream.
*
* **System Real-Time Messages**
*
* | Function | Hexadecimal | Decimal |
* |------------------------|-------------|---------|
* | `clock` | 0xF8 | 248 |
* | `start` | 0xFA | 250 |
* | `continue` | 0xFB | 251 |
* | `stop` | 0xFC | 252 |
* | `activesensing` | 0xFE | 254 |
* | `reset` | 0xFF | 255 |
*
* Values 249 and 253 are relayed by the
* [Web MIDI API](https://developer.mozilla.org/en-US/docs/Web/API/Web_MIDI_API) but they do not
* serve any specific purpose. The
* [MIDI 1.0 spec](https://www.midi.org/specifications/item/table-1-summary-of-midi-message)
* simply states that they are undefined/reserved.
*
* **Custom WebMidi.js Messages**
*
* These two messages are mostly for internal use. They are not MIDI messages and cannot be sent
* or forwarded.
*
* | Function | Hexadecimal | Decimal |
* |------------------------|-------------|---------|
* | `midimessage` | | 0 |
* | `unknownsystemmessage` | | -1 |
*
* @enum {Object.<string, number>}
* @readonly
* @static
*/
static get SYSTEM_MESSAGES(): {
sysex: number;
timecode: number;
songposition: number;
songselect: number;
tunerequest: number;
tuningrequest: number;
sysexend: number;
clock: number;
start: number;
continue: number;
stop: number;
activesensing: number;
reset: number;
midimessage: number;
unknownsystemmessage: number;
};
/**
* Array of channel-specific event names that can be listened for. This includes channel mode
* events and RPN/NRPN events.
*
* @type {string[]}
* @readonly
*/
static get CHANNEL_EVENTS(): string[];
}
/**
* The `Forwarder` class allows the forwarding of MIDI messages to predetermined outputs. When you
* call its [`forward()`](#forward) method, it will send the specified [`Message`](Message) object
* to all the outputs listed in its [`destinations`](#destinations) property.
*
* If specific channels or message types have been defined in the [`channels`](#channels) or
* [`types`](#types) properties, only messages matching the channels/types will be forwarded.
*
* While it can be manually instantiated, you are more likely to come across a `Forwarder` object as
* the return value of the [`Input.addForwarder()`](Input#addForwarder) method.
*
* @license Apache-2.0
* @since 3.0.0
*/
export class Forwarder {
/**
* Creates a `Forwarder` object.
*
* @param {Output|Output[]} [destinations=\[\]] An [`Output`](Output) object, or an array of such
* objects, to forward the message to.
*
* @param {object} [options={}]
* @param {string|string[]} [options.types=(all messages)] A MIDI message type or an array of such
* types (`"noteon"`, `"controlchange"`, etc.), that the specified message must match in order to
* be forwarded. If this option is not specified, all types of messages will be forwarded. Valid
* messages are the ones found in either
* [`SYSTEM_MESSAGES`](Enumerations#SYSTEM_MESSAGES)
* or [`CHANNEL_MESSAGES`](Enumerations#CHANNEL_MESSAGES).
* @param {number|number[]} [options.channels=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]
* A MIDI channel number or an array of channel numbers that the message must match in order to be
* forwarded. By default all MIDI channels are included (`1` to `16`).
*/
constructor(destinations?: Output | Output[], options?: {
types?: string | string[];
channels?: number | number[];
});
/**
* An array of [`Output`](Output) objects to forward the message to.
* @type {Output[]}
*/
destinations: Output[];
/**
* An array of message types (`"noteon"`, `"controlchange"`, etc.) that must be matched in order
* for messages to be forwarded. By default, this array includes all
* [`Enumerations.SYSTEM_MESSAGES`](Enumerations#SYSTEM_MESSAGES) and
* [`Enumerations.CHANNEL_MESSAGES`](Enumerations#CHANNEL_MESSAGES).
* @type {string[]}
*/
types: string[];
/**
* An array of MIDI channel numbers that the message must match in order to be forwarded. By
* default, this array includes all MIDI channels (`1` to `16`).
* @type {number[]}
*/
channels: number[];
/**
* Indicates whether message forwarding is currently suspended or not in this forwarder.
* @type {boolean}
*/
suspended: boolean;
/**
* Sends the specified message to the forwarder's destination(s) if it matches the specified
* type(s) and channel(s).
*
* @param {Message} message The [`Message`](Message) object to forward.
*/
forward(message: Message): void;
}
/**
* The `Input` class represents a single MIDI input port. This object is automatically instantiated
* by the library according to the host's MIDI subsystem and does not need to be directly
* instantiated. Instead, you can access all `Input` objects by referring to the
* [`WebMidi.inputs`](WebMidi#inputs) array. You can also retrieve inputs by using methods such as
* [`WebMidi.getInputByName()`](WebMidi#getInputByName) and
* [`WebMidi.getInputById()`](WebMidi#getInputById).
*
* Note that a single MIDI device may expose several inputs and/or outputs.
*
* **Important**: the `Input` class does not directly fire channel-specific MIDI messages
* (such as [`noteon`](InputChannel#event:noteon) or
* [`controlchange`](InputChannel#event:controlchange), etc.). The [`InputChannel`](InputChannel)
* object does that. However, you can still use the
* [`Input.addListener()`](#addListener) method to listen to channel-specific events on multiple
* [`InputChannel`](InputChannel) objects at once.
*
* @fires Input#opened
* @fires Input#disconnected
* @fires Input#closed
* @fires Input#midimessage
*
* @fires Input#sysex
* @fires Input#timecode
* @fires Input#songposition
* @fires Input#songselect
* @fires Input#tunerequest
* @fires Input#clock
* @fires Input#start
* @fires Input#continue
* @fires Input#stop
* @fires Input#activesensing
* @fires Input#reset
*
* @fires Input#unknownmidimessage
*
* @extends EventEmitter
* @license Apache-2.0
*/
export class Input extends EventEmitter {
/**
* Creates an `Input` object.
*
* @param {WebMidiApi.MIDIInput} midiInput [`MIDIInput`](https://developer.mozilla.org/en-US/docs/Web/API/MIDIInput)
* object as provided by the MIDI subsystem (Web MIDI API).
*/
constructor(midiInput: WebMidiApi.MIDIInput);
private _forwarders;
private _midiInput;
private _octaveOffset;
private _onMidiMessage;
private _onStateChange;
private _parseEvent;
/**
* Array containing the 16 [`InputChannel`](InputChannel) objects available for this `Input`. The
* channels are numbered 1 through 16.
*
* @type {InputChannel[]}
*/
channels: InputChannel[];
/**
* Adds a forwarder that will forward all incoming MIDI messages matching the criteria to the
* specified [`Output`](Output) destination(s). This is akin to the hardware MIDI THRU port, with
* the added benefit of being able to filter which data is forwarded.
*
* @param {Output|Output[]} output An [`Output`](Output) object, or an array of such
* objects, to forward messages to.
* @param {object} [options={}]
* @param {string|string[]} [options.types=(all messages)] A message type, or an array of such
* types (`noteon`, `controlchange`, etc.), that the message type must match in order to be
* forwarded. If this option is not specified, all types of messages will be forwarded. Valid
* messages are the ones found in either
* [`SYSTEM_MESSAGES`](Enumerations#SYSTEM_MESSAGES) or
* [`CHANNEL_MESSAGES`](Enumerations#CHANNEL_MESSAGES).
* @param {number|number[]} [options.channels=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]
* A MIDI channel number or an array of channel numbers that the message must match in order to be
* forwarded. By default all MIDI channels are included (`1` to `16`).
*
* @returns {Forwarder} The [`Forwarder`](Forwarder) object created to handle the forwarding. This
* is useful if you wish to manipulate or remove the [`Forwarder`](Forwarder) later on.
*/
addForwarder(output: Output | Output[], options?: {
types?: string | string[];
channels?: number | number[];
}): Forwarder;
/**
* Adds an event listener that will trigger a function callback when the specified event is
* dispatched. The event usually is **input-wide** but can also be **channel-specific**.
*
* Input-wide events do not target a specific MIDI channel so it makes sense to listen for them
* at the `Input` level and not at the [`InputChannel`](InputChannel) level. Channel-specific
* events target a specific channel. Usually, in this case, you would add the listener to the
* [`InputChannel`](InputChannel) object. However, as a convenience, you can also listen to
* channel-specific events directly on an `Input`. This allows you to react to a channel-specific
* event no matter which channel it actually came through.
*
* When listening for an event, you simply need to specify the event name and the function to
* execute:
*
* ```javascript
* const listener = WebMidi.inputs[0].addListener("midimessage", e => {
* console.log(e);
* });
* ```
*
* Calling the function with an input-wide event (such as
* [`"midimessage"`]{@link #event:midimessage}), will return the [`Listener`](Listener) object
* that was created.
*
* If you call the function with a channel-specific event (such as
* [`"noteon"`]{@link InputChannel#event:noteon}), it will return an array of all
* [`Listener`](Listener) objects that were created (one for each channel):
*
* ```javascript
* const listeners = WebMidi.inputs[0].addListener("noteon", someFunction);
* ```
*
* You can also specify which channels you want to add the listener to:
*
* ```javascript
* const listeners = WebMidi.inputs[0].addListener("noteon", someFunction, {channels: [1, 2, 3]});
* ```
*
* In this case, `listeners` is an array containing 3 [`Listener`](Listener) objects.
*
* Note that, when adding channel-specific listeners, it is the [`InputChannel`](InputChannel)
* instance that actually gets a listener added and not the `Input` instance. You can check that
* by calling [`InputChannel.hasListener()`](InputChannel#hasListener()).
*
* There are 8 families of events you can listen to:
*
* 1. **MIDI System Common** Events (input-wide)
*
* * [`songposition`]{@link Input#event:songposition}
* * [`songselect`]{@link Input#event:songselect}
* * [`sysex`]{@link Input#event:sysex}
* * [`timecode`]{@link Input#event:timecode}
* * [`tunerequest`]{@link Input#event:tunerequest}
*
* 2. **MIDI System Real-Time** Events (input-wide)
*
* * [`clock`]{@link Input#event:clock}
* * [`start`]{@link Input#event:start}
* * [`continue`]{@link Input#event:continue}
* * [`stop`]{@link Input#event:stop}
* * [`activesensing`]{@link Input#event:activesensing}
* * [`reset`]{@link Input#event:reset}
*
* 3. **State Change** Events (input-wide)
*
* * [`opened`]{@link Input#event:opened}
* * [`closed`]{@link Input#event:closed}
* * [`disconnected`]{@link Input#event:disconnected}
*
* 4. **Catch-All** Events (input-wide)
*
* * [`midimessage`]{@link Input#event:midimessage}
* * [`unknownmidimessage`]{@link Input#event:unknownmidimessage}
*
* 5. **Channel Voice** Events (channel-specific)
*
* * [`channelaftertouch`]{@link InputChannel#event:channelaftertouch}
* * [`controlchange`]{@link InputChannel#event:controlchange}
* * [`controlchange-controller0`]{@link InputChannel#event:controlchange-controller0}
* * [`controlchange-controller1`]{@link InputChannel#event:controlchange-controller1}
* * [`controlchange-controller2`]{@link InputChannel#event:controlchange-controller2}
* * (...)
* * [`controlchange-controller127`]{@link InputChannel#event:controlchange-controller127}
* * [`keyaftertouch`]{@link InputChannel#event:keyaftertouch}
* * [`noteoff`]{@link InputChannel#event:noteoff}
* * [`noteon`]{@link InputChannel#event:noteon}
* * [`pitchbend`]{@link InputChannel#event:pitchbend}
* * [`programchange`]{@link InputChannel#event:programchange}
*
* Note: you can listen for a specific control change message by using an event name like this:
* `controlchange-controller23`, `controlchange-controller99`, `controlchange-controller122`,
* etc.
*
* 6. **Channel Mode** Events (channel-specific)
*
* * [`allnotesoff`]{@link InputChannel#event:allnotesoff}
* * [`allsoundoff`]{@link InputChannel#event:allsoundoff}
* * [`localcontrol`]{@link InputChannel#event:localcontrol}
* * [`monomode`]{@link InputChannel#event:monomode}
* * [`omnimode`]{@link InputChannel#event:omnimode}
* * [`resetallcontrollers`]{@link InputChannel#event:resetallcontrollers}
*
* 7. **NRPN** Events (channel-specific)
*
* * [`nrpn`]{@link InputChannel#event:nrpn}
* * [`nrpn-dataentrycoarse`]{@link InputChannel#event:nrpn-dataentrycoarse}
* * [`nrpn-dataentryfine`]{@link InputChannel#event:nrpn-dataentryfine}
* * [`nrpn-dataincrement`]{@link InputChannel#event:nrpn-dataincrement}
* * [`nrpn-datadecrement`]{@link InputChannel#event:nr