Player

Player

Main player class. Contains methods to load files, start, stop.

Constructor

new Player(eventHandler, buffer)

Source:
Parameters:
Name Type Description
eventHandler function Callback to fire for each MIDI event. Can also be added with on('midiEvent', fn)
buffer array Array buffer of MIDI file (optional).

Methods

bytesProcessed() → {number}

Number of bytes processed in the loaded MIDI file.
Source:
Returns:
Type:
number

disableTrack(trackNumber) → {Player}

Disables a track for playing.
Source:
Parameters:
Name Type Description
trackNumber number Track number
Returns:
Type:
Player

dryRun() → {Player}

Plays the loaded MIDI file without regard for timing and saves events in this.events. Essentially used as a parser.
Source:
Returns:
Type:
Player

emitEvent(event) → {Player}

Sends MIDI event out to listener.
Source:
Parameters:
Name Type Description
event object
Returns:
Type:
Player

enableTrack(trackNumber) → {Player}

Enables a track for playing.
Source:
Parameters:
Name Type Description
trackNumber number Track number
Returns:
Type:
Player

endOfFile() → {boolean}

Determines if the player pointer has reached the end of the loaded MIDI file. Used in two ways: 1. If playing result is based on loaded JSON events. 2. If parsing (dryRun) it's based on the actual buffer length vs bytes processed.
Source:
Returns:
Type:
boolean

eventsPlayed() → {number}

Number of events played up to this point.
Source:
Returns:
Type:
number

fileLoaded() → {Player}

Sets default tempo, parses file for necessary information, and does a dry run to calculate total length. Populates this.events & this.totalTicks.
Source:
Returns:
Type:
Player

getCurrentTick() → {number}

Gets the current tick number in playback.
Source:
Returns:
Type:
number

getDivision() → {Player}

Gets quarter note division of loaded MIDI file.
Source:
Returns:
Type:
Player

getEvents() → {array}

Gets an array of events grouped by track.
Source:
Returns:
Type:
array

getFilesize() → {number}

Get filesize of loaded file in number of bytes.
Source:
Returns:
Type:
number
- The filesize.

getFormat() → {Player}

Gets MIDI file format for loaded file.
Source:
Returns:
Type:
Player

getSongPercentRemaining() → {number}

Gets remaining percent of playback.
Source:
Returns:
Type:
number

getSongTime() → {number}

Gets song duration in seconds.
Source:
Returns:
Type:
number

getSongTimeRemaining() → {number}

Gets remaining number of seconds in playback.
Source:
Returns:
Type:
number

getTotalEvents() → {number}

Gets total number of events in the loaded MIDI file.
Source:
Returns:
Type:
number

getTotalTicks() → {number}

Gets total number of ticks in the loaded MIDI file.
Source:
Returns:
Type:
number

getTracks() → {Player}

Parses out tracks, places them in this.tracks and initializes this.pointers
Source:
Returns:
Type:
Player

isPlaying() → {boolean}

Checks if player is playing
Source:
Returns:
Type:
boolean

loadArrayBuffer(arrayBuffer) → {Player}

Load an array buffer into the player.
Source:
Parameters:
Name Type Description
arrayBuffer array Array buffer of file to be loaded.
Returns:
Type:
Player

loadDataUri(dataUri) → {Player}

Load a data URI into the player.
Source:
Parameters:
Name Type Description
dataUri string Data URI to be loaded.
Returns:
Type:
Player

loadFile(path) → {Player}

Load a file into the player (Node.js only).
Source:
Parameters:
Name Type Description
path string Path of file.
Returns:
Type:
Player

on(playerEvent, fn) → {Player}

Subscribes events to listeners
Source:
Parameters:
Name Type Description
playerEvent string Name of event to subscribe to.
fn function Callback to fire when event is broadcast.
Returns:
Type:
Player

pause() → {Player}

Pauses playback if playing.
Source:
Returns:
Type:
Player

play() → {Player}

Start playing loaded MIDI file if not already playing.
Source:
Returns:
Type:
Player

playLoop(dryRun) → {undefined}

The main play loop.
Source:
Parameters:
Name Type Description
dryRun boolean Indicates whether or not this is being called simply for parsing purposes. Disregards timing if so.
Returns:
Type:
undefined

resetTracks() → {Player}

Resets play pointers for all tracks.
Source:
Returns:
Type:
Player

setStartTime(startTime) → {Player}

Setter for startTime.
Source:
Parameters:
Name Type Description
startTime number UTC timestamp
Returns:
Type:
Player

setTempo(tempo)

Setter for tempo.
Source:
Parameters:
Name Type Description
tempo number Tempo in bpm (defaults to 120)

skipToPercent(percent) → {Player}

Skips player pointer to specified percentage.
Source:
Parameters:
Name Type Description
percent number Percent value in integer format.
Returns:
Type:
Player

skipToSeconds(seconds) → {Player}

Skips player pointer to specified seconds.
Source:
Parameters:
Name Type Description
seconds number Seconds to skip to.
Returns:
Type:
Player

skipToTick(tick) → {Player}

Skips player pointer to specified tick.
Source:
Parameters:
Name Type Description
tick number Tick to skip to.
Returns:
Type:
Player

stop() → {Player}

Stops playback if playing.
Source:
Returns:
Type:
Player

triggerPlayerEvent(playerEvent, data) → {Player}

Broadcasts event to trigger subscribed callbacks.
Source:
Parameters:
Name Type Description
playerEvent string Name of event.
data object Data to be passed to subscriber callback.
Returns:
Type:
Player

validate() → {boolean}

Validates file using simple means - first four bytes should == MThd.
Source:
Returns:
Type:
boolean