• This repository has been archived on 16/May/2023
  • Stars
    star
    436
  • Rank 99,877 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Quick soundfont loader and player for browser

soundfont-player npm

Build Status js-standard-style license

⚠️ Archived. There are better alternatives. This is one of it: https://github.com/danigb/smplr Thanks for the fish! ⚠️

A soundfont loader/player to play MIDI sounds using WebAudio API.

It loads Benjamin Gleitzman's package of pre-rendered sound fonts by default with no server setup. Just a few lines of javascript:

Soundfont.instrument(new AudioContext(), 'acoustic_grand_piano').then(function (piano) {
  piano.play('C4')
})

It is a much simpler and lightweight replacement for MIDI.js soundfont loader (MIDI.js is much bigger, capable of play midi files, for example, but it weights an order of magnitude more).

Features

  • Load soundfont files in MIDI.js format or json format.
  • Unlimited poliphony (and stop all sounds with a single function call)
  • Use midi note numbers. Accepts decimal points to detune.
  • Easily connect to a Web MIDI API MidiInput
  • Schedule a list of notes

It uses audio-loader to load soundfont files and sample-player to play the sounds.

Install

Via npm: npm install --save soundfont-player

Or download the minified code and include it in your html:

<script src="soundfont-player.js"></script>
<script>
  Soundfont.instrument(new AudioContext(), 'marimba').then(function (marimba) {
  })
</script>

Usage

The soundfont loader

Out of the box are two Soundfonts available: MusyngKite and FluidR3_GM (MusyngKite by default: has more quality, but also weights more). You can load them with instrument function:

Soundfont.instrument(ac, 'clavinet').then(function (clavinet) {
  clavinet.play('C4')
})
// or use FluidR3_GM
Soundfont.instrument(ac, 'clavinet', { soundfont: 'FluidR3_GM' }).then(function (clavinet) {
  clavinet.play('C4')
})

You can load your own Soundfont files passing the .js path or url:

Soundfont.instrument(ac, '/soundfonts/clavinet-mp3.js').then(...)
// or
Soundfont.instrument(ac, 'clavinet-mp3.js', { from: 'server.com/soundfonts/' })

The soundfont player

Once you have an instrument you can:

// The first step is always create an instrument:
Soundfont.instrument(ac, 'clavinet').then(function (clavinet) {
  // Then you can play a note using names or midi numbers:
  clavinet.play('C4')
  clavinet.play(69)
  // float point midi numbers are accepted (and notes are detuned):
  clavinet.play(60.5) // => 500 cents over C4

  // you can stop all playing notes
  clavinet.stop()
  // or stop only one
  clavinet.play('C4').stop(ac.currentTime + 0.5)
  // or pass a duration argument to `play`
  clavinet.play('C4', ac.currentTime, { duration: 0.5})


  // You can connect the instrument to a midi input:
  window.navigator.requestMIDIAccess().then(function (midiAccess) {
    midiAccess.inputs.forEach(function (midiInput) {
      clavinet.listenToMidi(midiInput)
    })
  })

  // Or schedule events at a given time
  clavinet.schedule(ac.currentTime + 5, [ { time: 0, note: 60}, { time: 0.5, note: 61}, ...])
})

API

< 0.9.x users: The API in the 0.9.x releases has been changed and some features are going to be removed (like oscillators). While 0.9.0 adds warnings to the deprecated API, the 1.0.0 will remove the support.

instrument(ac, name, options) β‡’ Promise

Load a soundfont instrument. It returns a promise that resolves to a instrument object.

The instrument object returned by the promise has the following properties:

  • name: the instrument name
  • play: A function to play notes from the buffer with the signature play(note, time, duration, options)

The valid options are:

  • format: can be 'mp3' or 'ogg'
  • soundfont: can be 'FluidR3_GM' or 'MusyngKite'
  • nameToUrl: a function to convert from instrument names to URL
  • destination: by default Soundfont uses the audioContext.destination but you can override it.
  • gain: the gain (volume) of the player (1 by default)
  • attack: the attack time of the amplitude envelope
  • decay: the decay time of the amplitude envelope
  • sustain: the sustain gain value of the amplitude envelope
  • release: the release time of the amplitude envelope
  • adsr: the amplitude envelope as array of [attack, decay, sustain, release]. It overrides other options.
  • loop: set to true to loop audio buffers
  • notes: an array of the notes to decode. It can be an array of strings with note names or an array of numbers with midi note numbers. This is a performance option: since decoding mp3 is a cpu intensive process, you can limit limit the number of notes you want and reduce the time to load the instrument.
Param Type Description
ac AudioContext the audio context
name String the instrument name. For example: 'acoustic_grand_piano'
options Object (Optional) the same options as Soundfont.loadBuffers

Example

var Soundfont = require('soundfont-player')
var ac = new AudioContext()
Soundfont.instrument(ac, 'marimba', { soundfont: 'MusyngKite' }).then(function (marimba) {
  marimba.play('C4')
})

The player

The player object returned by the promise has the following functions:

player.play

An alias for player.start

player.start(name, when, options) β‡’ AudioNode

Start a sample buffer. The returned object has a function stop(when) to stop the sound.

Valid options are:

  • gain: float between 0 to 1
  • attack: the attack time of the amplitude envelope
  • decay: the decay time of the amplitude envelope
  • sustain: the sustain gain value of the amplitude envelope
  • release: the release time of the amplitude envelope
  • adsr: an array of [attack, decay, sustain, release]. Overrides other parameters.
  • duration: set the playing duration in seconds of the buffer(s)
  • loop: set to true to loop the audio buffer

player.stop(when, nodes) β‡’ Array

Stop some or all samples

player.on(event, callback) β‡’ player

Adds a listener of an event

player.connect(destination) β‡’ AudioPlayer

Connect the player to a destination node

player.schedule(when, events) β‡’ Array

Schedule a list of events to be played at specific time.

player.listenToMidi(input, options) β‡’ player

Connect a player to a midi input

See soundfont-player for more information.

nameToUrl(name, soundfont, format) β‡’ String

Given an instrument name returns a URL to to the Benjamin Gleitzman's package of pre-rendered sound fonts

Returns: String - the Soundfont file url

Param Type Description
name String instrument name
soundfont String (Optional) 'FluidR3_GM' or 'MusyngKite' ('MusyngKite' by default)
format String (Optional) Can be 'mp3' or 'ogg' (mp3 by default)

Example

var Soundfont = require('soundfont-player')
Soundfont.nameToUrl('marimba', null, 'ogg') // => http://gleitz.github.io/midi-js-soundfonts/FluidR3_GM/marimba-ogg.js

##Β Run the tests, examples and build the library distribution file

First clone this repo and install dependencies: npm i

To run tests use npm: npm test

The dist folder contains ready to use file for browser. You can use the dist file from the repo, but if you want to build you own run: npm run dist

To run the html example start a local http server. For example:

npm install -g http-server
http-server

And open http://localhost:8080/examples

To run pure javascript examples npm install -g beefy then beefy examples/marimba.js and navigate to http://localhost:9966/

Available instruments

By default it loads Benjamin Gleitzman's pre-rendered SoundFonts.

Instrument names

You can download the names of the instruments as a .json file:

Or require them:

var fluidNames = require('soundfont-player/names/fuildR3.json')
var musyngNames = require('soundfont-player/names/musyngkite.json')

Resources

License

MIT License

More Repositories

1

smplr

A web audio sampler instrument
TypeScript
148
star
2

music-scale

Music scales made easy
JavaScript
63
star
3

note-parser

Parse notes with javascript
JavaScript
55
star
4

timestretch

Simple audio timestretch OLA algorithm implemented in javascript
JavaScript
39
star
5

scorejs

Create musical scores with javascript
JavaScript
35
star
6

sample-player

A web audio audio sample player
JavaScript
30
star
7

audio-contour

A 5 stage audio envelope generator
JavaScript
20
star
8

music-pitch

Note names, midi, frequencies
JavaScript
19
star
9

music-chord

Music chords made easy
JavaScript
14
star
10

in-seconds

time calculator for music applications
JavaScript
14
star
11

synth-kit

A (web audio) synth construction kit
JavaScript
13
star
12

web-audio-assembler

JavaScript
10
star
13

polytone

Create polyphonic instruments from audio sources (Web Audio API)
JavaScript
8
star
14

rock-corpus

A Corpus Study of Rock Music
Perl
8
star
15

samples

Repository to audio samples served from github pages
HTML
8
star
16

all-that-chords

A javascript music chords library
JavaScript
7
star
17

interval-parser

Music interval parser for javascript
JavaScript
6
star
18

music.kit

Your javascript music kit
JavaScript
6
star
19

chord.dictionary

A music chord dictionary
JavaScript
5
star
20

midi-freq

Get frequency of a midi note
JavaScript
5
star
21

rust-synth

Rust
4
star
22

music.note

Tiny library to convert from midi and frequency to note names
JavaScript
4
star
23

elm-twelve-tone

Simple tutorial to setup a test driven project in elm-lang
Elm
4
star
24

yuml_rails

generate yuml class diagram from rails plugin
Ruby
3
star
25

music-interval

Musical intervals in javascript
JavaScript
3
star
26

neural-networks-and-deep-learning-es-lang

3
star
27

tonal-extensions

A multi-package repo with tonal module extensions
JavaScript
3
star
28

pitch-parser

Music pitch parser for javascript
JavaScript
3
star
29

tonal-hyperapp-app

Tonal live docs and theory app
JavaScript
3
star
30

musical-note

Musical notes in javascript
JavaScript
3
star
31

music.chords

A dictionary of music chords
JavaScript
3
star
32

audiovm

Audio Virtual Machine
JavaScript
3
star
33

wecex

wecex web app
Ruby
3
star
34

node-gibberish

JavaScript
3
star
35

music.harmonizer

Simple and fast music harmonizer
JavaScript
2
star
36

step-seq

A tiny step sequencer for web audio
JavaScript
2
star
37

pages

calc pages second try
JavaScript
2
star
38

matematicas-abn

MatemΓ‘ticas ABM
TypeScript
2
star
39

calcalendar

calc calendar rails plugin
2
star
40

tonal.scale

Music scales made easy
HTML
2
star
41

nand-to-tetris

Assembly
2
star
42

elm-school-of-music

A port of HSoM to elm
Elm
2
star
43

pitch-fifths

Pitches and intervals expressed in fifths
JavaScript
2
star
44

audio-pack

audio-pack
JavaScript
2
star
45

emite-library

emite gwt xmpp library
Java
2
star
46

lox

TypeScript
2
star
47

jazzapp

my jazz app
JavaScript
2
star
48

comic

experimental-minimalistic rails cms
JavaScript
2
star
49

music.chord

Music chords made easy
JavaScript
2
star
50

achord

Simple chord detection with js
JavaScript
2
star
51

interval-notation

Parse and build musical intervals in shorthand notation
JavaScript
2
star
52

music-gamut

Manipulate notes made easy
JavaScript
2
star
53

pitch-transpose

Simple and fast pitch transposition
JavaScript
2
star
54

ts-ocarina

An onging port of `purescript-ocarina` audio library to typescript
TypeScript
2
star
55

music-parser

Parse music notation with javascript
JavaScript
2
star
56

music.interval

Music intervals
JavaScript
2
star
57

lasbuenasnoches

los datos de la web
JavaScript
2
star
58

mailservice

a simple REST webservice mail service with background task and management
2
star
59

chromatic

Chromatic scales
JavaScript
2
star
60

grow3

A rails web ide for personal use
Ruby
2
star
61

hablar

hablar emite widget
Java
2
star
62

impro-visor-js

Convert awesome impro-visor musical knowledge data to json format
LiveScript
2
star
63

n3

novaron web page
Ruby
2
star
64

lasbuenasnoches.com

Las Buenas Noches middleman site
HTML
1
star
65

Asambleas

JavaScript
1
star
66

tzatziki

eat your own cucumber
Ruby
1
star
67

pitch-array

A simple format to represent musical pitches and intervals
1
star
68

rhythmically

Create musical sequences with javascript
JavaScript
1
star
69

crestas

JavaScript
1
star
70

scale-names

Musical scale names
JavaScript
1
star
71

micro-blog

HTML
1
star
72

clox

C
1
star
73

bucolicas.cc

HTML
1
star
74

calcaxy.com

calcaxy.com middleman website
HTML
1
star
75

note-duration

Parse note durations
JavaScript
1
star
76

despachodepan.com

despachodepan.com
HTML
1
star
77

pitch-op

Music pitch operator
JavaScript
1
star
78

gwtbooka-old

booka gwt version
Java
1
star
79

gwtbooka

gwt booka
Java
1
star
80

biocordoba

biocordoba
Ruby
1
star
81

binary-scale

All the 2048 western well tempered scales in all its glory
JavaScript
1
star
82

zaszas

zaszas homepage
Ruby
1
star
83

BandInATab

Play along with me
JavaScript
1
star
84

music.note.height

Tiny library to convert from midi <-> note name <-> frequency
JavaScript
1
star
85

recortable

recortable
Ruby
1
star
86

rap

thor task to control apache and passenger locally
Ruby
1
star
87

music.note.transpose

Note transposition fast and easy
JavaScript
1
star
88

audio-player

A flexible audio-player
1
star
89

music.scale.build

Build musical scales
JavaScript
1
star
90

note-pitch

Note pitch manipulation in javascript
JavaScript
1
star
91

zap

zap projects!
JavaScript
1
star
92

scorejs-player

A simple player for score-js
JavaScript
1
star
93

clismon.com

Industrias Clismon
HTML
1
star
94

minimax

Another minimax implementation in JS
JavaScript
1
star
95

music.transpose

Transpose notes fast and easy
JavaScript
1
star
96

q2010

cuestionario.r08.es
Ruby
1
star
97

sampler.js

A simple web audio piano player using soundfont samples
JavaScript
1
star
98

a.pitch

Abstract pitch transformations
JavaScript
1
star
99

tonal.note

Musical notes for javascript
JavaScript
1
star
100

music.operator

Your music operator
JavaScript
1
star