Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MIDIPlayer

A NoteSequence player that uses a MIDI output for playing. Note that WebMIDI is not supported in all browsers. A polyfill exists, but it too requires a plugin to be installed on the user's computer, so it might not work in all cases.

If you want to use a particular MIDI output port, you must update the output property before calling start, otherwise a message will be sent to all connected MIDI outputs:

Example (easy mode):

   const player = new mm.MIDIPlayer();
   player.requestMIDIAccess().then(() => {
     // For example, use only the first port. If you omit this,
     // a message will be sent to all ports.
     player.outputs = [player.availableOutputs[0]];
     player.start(seq);
   })

You can also explicitly request MIDI access outside of the player, in your application, and just update the output property before playing:

Example (advanced mode):

   navigator.requestMIDIAccess().then((midi) => {
      // Get all the MIDI outputs to show them in a <select> (for example)
      const availableOutputs = [];
      const it = midi.outputs.values();
      for (let o = it.next(); o && !o.done; o = it.next()) {
         availableOutputs.push(o.value);
      }
      // Populate the <select>
      const el = document.querySelector('select');
      el.innerHTML = availableOutputs.map(i =>
          `<option>${i.name}</option>`).join('');

      // Use the selected output port.
      player = new mm.MIDIPlayer();
      player.outputs = [availableOutputs[el.selectedIndex]];
      player.start(seq)
    });

Hierarchy

Index

Constructors

constructor

Properties

availableOutputs

availableOutputs: MIDIOutput[] = []

Protected callbackObject

callbackObject: BasePlayerCallback

Protected currentPart

currentPart: any

Protected desiredQPM

desiredQPM: number

outputs

outputs: MIDIOutput[] = []

Protected playClick

playClick: boolean

Protected scheduledStop

scheduledStop: number

Methods

getPlayState

  • getPlayState(): any

isPlaying

  • isPlaying(): boolean
  • Returns true iff the player is completely stopped. This will only be false after calling stop(), and will be true after calling start(), pause() or unpause().

    Returns boolean

pause

  • pause(): void

Protected playNote

  • playNote(time: number, note: INote): void

playNoteDown

  • playNoteDown(note: INote): void

playNoteUp

  • playNoteUp(note: INote): void

requestMIDIAccess

  • requestMIDIAccess(): Promise<Object>
  • Requests MIDI access from the user, and stores all available MIDI outputs.

    Returns Promise<Object>

resume

  • resume(): void
  • Pause playing the currently playing sequence right away. Call resume() to resume playing the sequence.

    Returns void

resumeContext

  • resumeContext(): void
  • Resumes the Audio context. Due to autoplay restrictions, you must call this function in a click handler (i.e. as a result of a user action) before you can start playing audio with a player. This is already done in start(), but you might have to call it yourself if you have any deferred/async calls.

    Returns void

setTempo

  • setTempo(qpm: number): void

start

  • start(seq: INoteSequence, qpm?: number): Promise<void>
  • Starts playing a NoteSequence (either quantized or unquantized), and returns a Promise that resolves when it is done playing.

    Parameters

    • seq: INoteSequence

      The NoteSequence to play.

    • Optional qpm: number

      (Optional) If specified, will play back at this qpm. If not specified, will use either the qpm specified in the sequence or the default of 120. Only valid for quantized sequences.

    Returns Promise<void>

    a Promise that resolves when playback is complete.

stop

  • stop(): void

Generated using TypeDoc