MidiPlayer(configuration?)
parameter | type | description |
---|---|---|
configuration |
object? | |
configuration.eventLogger |
function
(default undefined )
|
The function that receives event payloads. |
configuration.logging |
boolean
(default false )
|
Turns ON or OFF logging to the console. |
configuration.patchUrl |
string
(default https://cdn.jsdelivr.net/npm/midi-instrument-patches@latest/ )
|
The public path where MIDI instrument patches can be found. |
configuration.volume |
number
(default 80 )
|
Set playback volume when initializing the player. |
property | type | description |
---|---|---|
context | object | : The AudioContext instance. |
eventLogger | function | : The function that is called to emit events. |
logging | boolean | : Whether console logging is ON or OFF. |
isFirstInstance | boolean | : Whether this is the first instance of the Midi Player or |
midiFileArray | arrayBuffer | : A typed array that represents the content of the MIDI. |
midiFileBuffer | any | : The buffer with the MIDI data. |
patchUrl | string | : The URL used to load MIDI instrument patches. |
playerId | string | : ID of this instance of Midi Player. |
sampleRate | number | : The sample rate of the AudioContext. |
source | object | : The source that plays the audio signal. |
startTime | number | : The time when MIDI playback started. |
stream | number | : The MIDI stream. |
volume | number | : Playback volume. |
waveBuffer | any | : The buffer with the MIDI data converted to WAV. not. |
object
:
A
MidiPlayer
instance.
import MidiPlayer from 'web-midi-player';
const eventLogger = (payload) => {
console.log('Received event:', payload.event);
}
const midiPlayer = new MidiPlayer({ eventLogger });
formatMidiName(name)
Formats the name of a MIDI for display purposes.
parameter | type | description |
---|---|---|
name |
String | Name of the MIDI song. |
String
:
preload(parameters)
Loads instrument patches for a list of MIDI input.
Please note that you can not use parameters.items.arrayBuffer
and parameters.items.url
concurrently.
parameter | type | description |
---|---|---|
parameters |
object
= {}
|
|
parameters.items |
object?
(default [] )
|
An array. |
parameters.items.arrayBuffer | arrayBuffer? | An array buffer containing MIDI data to play. |
parameters.items.url | string? | The URL where the MIDI file to play is located. |
parameters.audioContext | object? | An instance of the Web Audio API AudioContext interface. |
boolean
:
Whether instrument patches were successfully preloaded or not.
const name1 = 'My MIDI file from URL';
const url = 'media/file.midi';
const name2 = 'My MIDI file from ArrayBuffer';
const arrayBuffer = new ArrayBuffer();
midiPlayer.preload({
items: [
{ url, name: name1 },
{ arrayBuffer, name: name2 }
]
});
play(input)
Starts playback of MIDI input.
Please note that you can not use input.arrayBuffer
and input.url
concurrently.
parameter | type | description |
---|---|---|
input |
object
= {}
|
|
input.arrayBuffer | arrayBuffer? | An array buffer containing MIDI data to play. |
input.url | string? | The URL where the MIDI file to play is located. |
input.name | string? | A human-friendly name for the song. |
input.audioContext | object? | An instance of the Web Audio API AudioContext interface. |
boolean
:
Whether playback was successfully initiated or not.
const name1 = 'My MIDI file from URL';
const url = 'media/file.midi';
midiPlayer.play({ url, name: name1 });
const name2 = 'My MIDI file from ArrayBuffer';
const arrayBuffer = new ArrayBuffer();
midiPlayer.play({ arrayBuffer, name: name2 });
pause()
Pauses playback of MIDI input.
boolean
:
Whether playback was successfully paused or not.
midiPlayer.pause();
resume()
Resumes playback of MIDI input.
boolean
:
Whether playback was successfully ressumed or not.
midiPlayer.resume();
stop()
Stops playback of MIDI input.
boolean
:
Whether playback was successfully stopped or not.
midiPlayer.stop();
getVolume()
Gets the current volume of the playback.
number
:
The current volume.
const volume = midiPlayer.getVolume();
setVolume(input)
Sets the current volume of the playback.
parameter | type | description |
---|---|---|
input |
object | |
input.volume | number | The new value for the volume (also known as gain). Typically, a whole number between 0 and 100 but can actually be negative, greater, or even a decimal number. |
midiPlayer.setVolume({ volume: 80 });
emitEvent(payload)
Send custom payloads to the event logger.
parameter | type | description |
---|---|---|
payload |
object | |
payload.event | string? | The name of the event. |
payload.message | string? | A message that described the event. |
const event = 'MIDI_CUSTOM_EVENT';
const message = 'Something happened.';
midiPlayer.emitEvent({ event, message });
setLogger(configuration?)
Updates the configuration of the logger.
parameter | type | description |
---|---|---|
configuration |
object? | |
configuration.eventLogger |
function
(default undefined )
|
The function that receives event payloads. |
configuration.logging |
boolean
(default false )
|
Turns ON or OFF logging to the console. |
const eventLogger = (payload) => {
console.log('Received event:', payload.event);
}
midiPlayer.setLogger({ eventLogger });
LibTiMidity()
alignMemoryPage(x)
parameter | type | description |
---|---|---|
x |
Number |
exit(status)
Exits the script.
parameter | type | description |
---|---|---|
status |
string | The message to display. |
abort(text)
Throws an error that aborts the execution of the script.
parameter | type | description |
---|---|---|
text |
string | The message to display. |
assert(condition, text)
Aborts the execution of the script if assertion fails.
parameter | type | description |
---|---|---|
condition |
boolean | The assertion to test. |
text |
string | The message to display if the assertion fails. |
call(ident, returnType, argTypes, args)
A wrapper to call functions.
parameter | type | description |
---|---|---|
ident |
string | The name of the function to call. |
returnType |
string | The return type of the function, one of the JS types 'number', 'string' or 'array' (use 'number' for any C pointer, and 'array' for JavaScript arrays and typed arrays; note that arrays are 8-bit). |
argTypes |
array | An array of the types of arguments for the function (if there are no arguments, this can be ommitted). Types are as in returnType, except that 'array' is not possible (there is no way for us to know the length of the array) |
args |
array | An array of the arguments to the function, as native JS values (as in returnType). Note that string arguments will be stored on the stack (the JS string will become a C string on the stack). |
any
:
Native JS value (as in returnType)
setValue(ptr, value, type)
Dynamically sets a value in memory at runtime. Only does aligned writes. This is a lower-level operation.
parameter | type | description |
---|---|---|
ptr |
any | Pointer. |
value |
any | |
type |
string | LLVM type ('i8', , 'i16', 'i32', 'i64', 'float', 'double') |
getValue(ptr, type)
Dynamically gets a value in memory at runtime. Only does aligned reads. This is a lower-level operation.
parameter | type | description |
---|---|---|
ptr |
any | Pointer. |
type |
string | LLVM type ('i8', , 'i16', 'i32', 'i64', 'float', 'double') |
allocate(slab, types, allocator)
This is for internal use. Optimized for multiple syntaxes to save space in generated code. You should allocate memory using _malloc(), initialize it with setValue(), and so forth.
parameter | type | description |
---|---|---|
slab |
any | An array of data or a number. If a number, then the size of the block to allocate in bytes (note that this is sometimes confusing: the next parameter does not affect this). |
types |
any | Either an array of types, one for each byte (or 0 if no type at that position), or a single type which is used for the entire block. This only matters if there is initial data. If slab is a number, then this does not matter at all and is ignored. |
allocator |
any | How to allocate memory, see ALLOC_* |
Pointer_stringify(ptr, length?)
parameter | type | description |
---|---|---|
ptr |
any | Pointer. |
length |
any? |
UTF16ToString(ptr)
parameter | type | description |
---|---|---|
ptr |
any | Pointer to a null-terminated UTF16LE-encoded string in the emscripten HEAP. |
stringToUTF16(str, outPtr)
Copies a JavaScript string to the emscripten HEAP. The copy is null-terminated and encoded in UTF16LE form. The copy will require at most (str.length2+1)2 bytes of space in the HEAP.
parameter | type | description |
---|---|---|
str |
string | JavaScript string. |
outPtr |
any | HEAP address (pointer). |
UTF32ToString(ptr)
parameter | type | description |
---|---|---|
ptr |
any | Pointer to a null-terminated UTF32LE-encoded string in the emscripten HEAP. |
stringToUTF32(str, outPtr)
Copies a JavaScript string to the emscripten HEAP. The copy is null-terminated and encoded in UTF32LE form. The copy will require at most (str.length+1)*4 bytes of space in the HEAP.
parameter | type | description |
---|---|---|
str |
string | JavaScript string. |
outPtr |
any | HEAP address (pointer). |
callRuntimeCallbacks(callbacks)
parameter | type | description |
---|---|---|
callbacks |
any |
addOnPreRun(cb)
parameter | type | description |
---|---|---|
cb |
any | Callback. |
addOnInit(cb)
parameter | type | description |
---|---|---|
cb |
any | Callback. |
addOnPreMain(cb)
parameter | type | description |
---|---|---|
cb |
any | Callback. |
addOnExit(cb)
parameter | type | description |
---|---|---|
cb |
any | Callback. |
addOnPostRun(cb)
parameter | type | description |
---|---|---|
cb |
any | Callback. |
intArrayFromString(stringy, dontAddNull, length)
parameter | type | description |
---|---|---|
stringy |
any | |
dontAddNull |
any | |
length |
any |
intArrayToString(array)
parameter | type | description |
---|---|---|
array |
any |
writeStringToMemory(string, buffer, dontAddNull)
parameter | type | description |
---|---|---|
string |
any | |
buffer |
any | |
dontAddNull |
any |
writeArrayToMemory(array, buffer)
parameter | type | description |
---|---|---|
array |
any | |
buffer |
any |
writeAsciiToMemory(str, buffer, dontAddNull)
parameter | type | description |
---|---|---|
str |
any | |
buffer |
any | |
dontAddNull |
any |
addRunDependency(id)
parameter | type | description |
---|---|---|
id |
any |
removeRunDependency(id)
parameter | type | description |
---|---|---|
id |
any |
createPath(parent, path, throwError)
Creates a directory.
parameter | type | description |
---|---|---|
parent |
string | The parent folder. |
path |
string | The path to create. |
throwError |
boolean
= true
|
If directory creation failed, throw an error. |
createFile(parent, name, canRead, canWrite)
Creates a file.
parameter | type | description |
---|---|---|
parent |
string | The parent folder. |
name |
string | The path to create. |
canRead |
boolean | |
canWrite |
boolean |
createDataFile(parent, name, data, canRead, canWrite, canOwn?, throwError)
Creates a data file and writes data to it.
parameter | type | description |
---|---|---|
parent |
string | |
name |
string | |
data |
array | |
canRead |
boolean | |
canWrite |
boolean | |
canOwn |
boolean? | |
throwError |
boolean
= true
|
If file creation failed, throw an error. |
loadPatchFromUrl(baseUrl, filename)
Creates a file in the patch directory and writes the instrument data to it.
parameter | type | description |
---|---|---|
baseUrl |
string | The public URL where all patches can be found as a group. |
filename |
string | The name of the instrument patch to load (including subfolder for drums). |
init(throwError)
Initializes the library.
parameter | type | description |
---|---|---|
throwError |
boolean
= true
|
Throw an error if file/folder creation failed. |
EventHandler(configuration?)
parameter | type | description |
---|---|---|
configuration |
object? | |
configuration.playerId |
string
(default undefined )
|
The ID of the Midi Player that sends events. |
configuration.eventLogger |
function
(default undefined )
|
The function that receives event payloads. |
configuration.logging |
boolean
(default false )
|
Turns ON or OFF logging to the console. |
object
:
An
EventHandler
instance.
emitInit()
Emits an event that indicates that the MIDI player is initialized.
emitLoadFile(payload)
Emits an event that indicates that a MIDI file is being loaded.
parameter | type | description |
---|---|---|
payload |
object | |
payload.message |
string?
(default 'Loading MIDI file...' )
|
A message about the MIDI file being loaded. |
emitLoadPatch(payload)
Emits an event that indicates that a MIDI instrument patch is being loaded.
parameter | type | description |
---|---|---|
payload |
object | |
payload.message |
string?
(default 'Loading instrument patch...' )
|
A message about the instrument patch being loaded. |
emitPlay(payload)
Emits an event that indicates that the MIDI player currently playing a MIDI file.
parameter | type | description |
---|---|---|
payload |
object | |
payload.time | string? | The playback position. |
emitPause(payload)
Emits an event that indicates that the MIDI player has paused the playback of a file.
parameter | type | description |
---|---|---|
payload |
object | |
payload.time | string? | The playback position. |
emitResume(payload)
Emits an event that indicates that the MIDI player has resumed the playback of a file.
parameter | type | description |
---|---|---|
payload |
object | |
payload.time | string? | The playback position. |
emitStop()
Emits an event that indicates that the MIDI player has stopped the playback of a file.
emitEnd(payload)
Emits an event that indicates that the MIDI player has reached the end of a file.
parameter | type | description |
---|---|---|
payload |
object | |
payload.time | string? | The playback position. |
emitError(payload)
Emits an event that indicates that an error prevented the MIDI player to continue.
parameter | type | description |
---|---|---|
payload |
object | |
payload.message |
string?
(default 'An error occurred.' )
|
A message that indicates that an error occurred. |
payload.error |
string?
(default '' )
|
A message that describes the details of the error. |
emitEvent(payload)
Send payloads to the event logger.
parameter | type | description |
---|---|---|
payload |
object | |
payload.event | string? | The name of the event. |
payload.message | string? | A message that describes the event. |
setLogger(configuration?)
Updates the configuration of the logger.
parameter | type | description |
---|---|---|
configuration |
object? | |
configuration.eventLogger |
function
(default undefined )
|
The function that receives event payloads. |
configuration.logging |
boolean
(default false )
|
Turns ON or OFF logging to the console. |