Skip to content

Play SoundFont ​

WARNING

Node.js does not implement AudioContext, which requires monkey patching.

Start Example Synth

With smplr ​

First parse the soundfont into smplr, which must be installed.

TypeScript
import { Soundfont2Sampler } from "smplr"
import { SoundFont3 } from "soundfont3"

const context = new AudioContext()
const sampler = new Soundfont2Sampler(context, {
  // For file data, use base64 data url
  url: "https://smpldsnds.github.io/soundfonts/soundfonts/galaxy-electric-pianos.sf2",
  createSoundfont: (data) => new SoundFont3(data),
})

Then load an instrument.

TypeScript
sampler.load.then(() => {
  // Loads first instrument
  sampler.loadInstrument(sampler.instrumentNames[0])
})

Play a note.

TypeScript
midiCode = 60 // Middle C MIDI code
durationSeconds = 1
sampler.start({
  note: midiCode,
  duration: durationSeconds
})

AudioContext on Node.js ​

DANGER

There are many 'AudioContext' reimplementations for Node.js. This solution uses the most popular reimplementation, which is not even in alpha. A stable solution is unlikely to appear soon.

Install web-audio-api.

Bash
npm i web-audio-api

Then monkey patch AudioContext.

TypeScript
import { AudioContext } from 'node-web-audio-api'

// @ts-expect-error AudioContext undefined in Node.js
global.AudioContext = AudioContext

Reading from a file may require conversion to base64 data url.

TypeScript
import { readFileSync } from 'fs'

const bufferToDataUrl = (buffer: Buffer) => {
  return `data:;base64,${buffer.toString('base64')}`
}
const soundFontBuffer = readFileSync('soundFontFile.sf3')
const soundFontDataUrl = bufferToDataUrl(soundFontBuffer)