Skip to content

MidiPlayer

MidiPlayer reads MIDI files/URLs, feeds a MIDI bus, and optionally listens to Clock/Start/Stop inputs for transport control. Its render props surface playback controls, metadata, position tracking, and MIDI file state so you can build MIDI transport UIs.

Props

PropTypeDefaultDescription
outputModStreamRefRequiredMIDI output bus that other modules (e.g. Fluidsynth) can subscribe to
startInputModStreamRefOptional CV gateway to trigger the module via a Start pulse
stopInputModStreamRefOptional CV input for Stop pulses
clockInputModStreamRefOptional clock gate for tick synchronization
labelstring'midi-player'Metadata label
midiFileNamestring''Name of the currently loaded MIDI file
onMidiFileNameChange(name: string) => voidCallback when MIDI file name updates
midiFileDataUrlstring''Embedded MIDI data URL
onMidiFileDataUrlChange(dataUrl: string) => voidCallback when the data URL changes
midiUrlstring''Remote MIDI file URL
onMidiUrlChange(url: string) => voidCallback when the MIDI URL changes
bpmnumber120Manual tempo applied when no clock is connected
onBpmChange(value: number) => voidCallback when the BPM changes
children(props: MidiPlayerRenderProps) => ReactNodeRender prop that exposes all playback controls

Render Props

PropertyTypeDescription
midiFileNamestringName of the loaded MIDI file
setMidiFileName(name: string) => voidUpdate the file name
midiFileDataUrlstringEmbedded data URL
setMidiFileDataUrl(dataUrl: string) => voidUpdate the embedded data
midiUrlstringRemote MIDI URL
setMidiUrl(url: string) => voidChange the fetched URL
loadMidiFile(file: File) => voidLoad MIDI via a File object
bpmnumberCurrent tempo value
setBpm(value: number) => voidAdjust the BPM
isPlayingbooleanWhether the player is running
isLoadedbooleantrue when MIDI data is parsed
isClockConnectedbooleantrue when a clock stream is wired
isStartConnectedbooleantrue if a start CV input exists
isStopConnectedbooleantrue if a stop CV input exists
play() => voidStart playback
pause() => voidPause playback
stop() => voidStop playback and reset playback position
positionnumberCurrent playback position (seconds or ticks)
durationnumberMIDI duration in seconds
positionUnit`'seconds''ticks'`
positionStepnumberSlider step size (1 for ticks, 0.01 for seconds)
positionMaxnumberSlider maximum value
setPosition(value: number) => voidSeek to an absolute position
metadataMidiMetadata | nullParsed metadata such as tracks/tempo
errorstring | nullLoad errors, if any

Usage

tsx
import { MidiPlayer, Fluidsynth } from '@mode-7/mod';
import { useRef } from 'react';

function MidiPlayback() {
  const midiBus = useRef<null>(null);

  return (
    <div>
      <MidiPlayer output={midiBus} midiUrl="/samples/bach.mid" bpm={140}>
        {({ play, pause, metadata, isLoaded }) => (
          <div>
            <button onClick={play} disabled={!isLoaded}>Play</button>
            <button onClick={pause}>Pause</button>
            <p>{metadata ? `${metadata.tracks} tracks` : 'No metadata yet'}</p>
          </div>
        )}
      </MidiPlayer>
      <Fluidsynth output={useRef(null)} midiInput={midiBus} />
    </div>
  );
}

Released under the MIT License.