A JavaScript SoundFont 2 parser
JavaScript HTML
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
tests
.gitignore
Gruntfile.js
MIT-LICENSE.txt
README.md
bower.json
package.json

README.md

SoundFont 2 (and 3) Parser

A library for parsing SoundFont (SF2, SF3) files in JavaScript.

License and Credits

This library is based on the SoundFont 2 parser from sf2synth.js, a SoundFont Synthesizer for WebMidiLink. It was written by imaya / GREE Inc and licensed under the MIT license. Modifications to the original are by Colin Clark.

How to Use It

  1. Load your SoundFont file using XHR's arraybuffer responseType and wrap it as a Uint8Array
  2. Instantiate a new parser instance
  3. Call the parse() method to parse the SoundFont file
  4. Use the getPresets() and getInstruments() methods to access preset and instrument data
  5. Sample data is stored in the parser's sample member variable

Example Code

// Utility function to load a SoundFont file from a URL using XMLHttpRequest.
// The same origin policy will apply, as for all XHR requests.
function loadSoundFont(url, success, error) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.responseType = "arraybuffer";
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                success(new Uint8Array(xhr.response));
            } else {
                if (options.error) {
                    options.error(xhr.statusText);
                }
            }
        }
    };
    xhr.send();
}

// Load and parse a SoundFont file.
loadSoundFont("sf_GMbank.sf2", function (sfData) {
    var parser = new sf2.Parser(sf2Data);
    parser.parse();

    // Do something with the parsed SoundFont data.
});

Summary of Changes from the Original

  • Separated out the SoundFont parsing library from the rest of the sf2synth synthesizer
  • Removed the dependence on Google Closure
  • Renamed the namespace to sf2 for brevity
  • Added boilerplate to support most JS module loaders, including CommonJS, AMD, Node.js and browser globals
  • Added a Grunt-based build process
  • Created Bower and npm packages

To Do

  • Improve the API of the parser by making it stateless
  • Add support for parsing in a Web Worker so that the main browser thread doesn't block
  • Massively extend the unit test coverage
  • Add support for running the unit tests in Node.js