Skip to content

Fluidsynth

Fluidsynth loads js-synthesizer + FluidSynth to render MIDI streams with configurable SoundFonts. It exposes render props for configuring the WASM asset path, swapping SoundFonts, and emitting MIDI events when MIDI data arrives through the provided midiInput stream.

Props

PropTypeDefaultDescription
outputModStreamRefRequiredThe Web Audio node that receives the synth output
midiInputModStreamRefOptional MIDI bus to listen for incoming MIDI events
labelstring'fluidsynth'Label included in ModStream metadata
wasmBaseUrlstring''URL prefix for js-synthesizer + FluidSynth assets; used when loadScript locates the files
onWasmBaseUrlChange(url: string) => voidCallback when the wasm base URL changes
soundFontUrlstring'/sf2/microgm.sf2'URL of the SoundFont file to load
onSoundFontUrlChange(url: string) => voidCallback when the SoundFont URL changes
soundFontFileNamestring''Optional name of a locally loaded SoundFont
onSoundFontFileNameChange(name: string) => voidCallback when the SoundFont name is updated
soundFontFileDataUrlstring''Data URL of an embedded SoundFont
onSoundFontFileDataUrlChange(dataUrl: string) => voidCallback when the embedded SoundFont data updates
gainnumber1.0Gain applied to the synth output
onGainChange(value: number) => voidCallback when the gain changes
children(props: FluidsynthRenderProps) => ReactNodeRender-prop function that exposes controls like setGain and loadSoundFontFile

Render Props

PropertyTypeDescription
wasmBaseUrlstringCurrent URL base used to load the js-synthesizer/FluidSynth scripts
setWasmBaseUrl(url: string) => voidUpdate the wasm asset base path
soundFontUrlstringCurrent SoundFont URL
setSoundFontUrl(url: string) => voidSwitch to a new SoundFont URL
soundFontFileNamestringName of the locally loaded SoundFont file
setSoundFontFileName(name: string) => voidUpdate the local SoundFont filename
soundFontFileDataUrlstringEmbedded SoundFont data URL
setSoundFontFileDataUrl(dataUrl: string) => voidUpdate the embedded SoundFont payload
loadSoundFontFile(file: File) => voidConvenience helper to load SoundFonts via a File object
gainnumberCurrent gain value
setGain(value: number) => voidAdjust the synth gain
isReadybooleantrue once the SoundFont has finished loading
isSoundFontLoadedbooleantrue when SoundFont data is available
isSoundFontLoadingbooleantrue while a SoundFont is fetching
errorstring \| nullLast loading error, if any
allNotesOff() => voidTrigger allNotesOff/allSoundsOff on the synth

Usage

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

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

  return (
    <Fluidsynth output={synthOut} midiInput={midiBus}>
      {({ loadSoundFontFile, isReady, isSoundFontLoaded }) => (
        <div>
          <input
            type="file"
            accept=".sf2"
            onChange={(event) => {
              const file = event.target.files?.[0];
              if (file) loadSoundFontFile(file);
            }}
          />
          <div>
            {isSoundFontLoaded ? 'Loaded' : isReady ? 'Ready' : 'Loading...'}
          </div>
        </div>
      )}
    </Fluidsynth>
  );
}

Released under the MIT License.