Small JavaScript synthesizer library
JavaScript HTML
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example-songs
.gitignore
Gruntfile.js
LICENSE
README.md
bower.json
package.json
sonantx.js
test.html

README.md

Sonant-X

A very small JavaScript synthesizer library, suitable for small JavaScript demos or video games.

Sonant-X was created with video games' sound effects and music in mind, as well as size-restrained demos. It supports both the HTML5 Audio object and the Web Audio API.

Songs for Sonant-X can be composed using Sonant-X Live.

Sonant-X is a fork of js-sonant by Marcus Geelnard.

Example

Just test one of the available songs for Sonant-X Live, like this one: Synth 4k by m / Bits'n'Bites.

Installation

Using Bower:

bower install sonantx

Or just download the latest stable version.

Usage

Song Generation

To generate a whole song, you'll first need a song in JSON format generated by Sonant-X Live. An example can be found by clicking here and choosing "Export JSON" in the menu on the left.

With that JSON song, we will be able to create a MusicGenerator object:

var songGen = new sonantx.MusicGenerator(jsonSong);

Then it depends if you prefer to use the Web Audio API or the HTML5 Audio class.

Web Audio API

songGen.createAudioBuffer(function(buffer) {
    var source = audioCtx.createBufferSource();
    source.buffer = buffer;
    source.connect(audioCtx.destination);
    source.start();
});

Note that the buffer object can be stored and re-used any number of times.

Audio Class

songGen.createAudio(function(audio) {
    audio.play();
});

Note that, like all Audio objects, this one is not re-usable. If you want to be able to re-create Audio objects with minimum delay, you can use the following syntax:

songGen.getAudioGenerator(function(audioGenerator) {
    var audio = audioGenerator.getAudio();
    audio.play();
});

Here the audioGenerator can be used to create any number of Audio instances without the need to re-calculate the whole song. Remember that, even if the song is not re-calculated, each creation of an HTML5 Audio object provoke an impossible to avoid delay in most browsers. It can be as short as 0.1 second but it's still a burden for applications like video games. This is why using the Web Audio API is recommended.

Single sounds generation

To generate single sound (suitable for video games sound effects) you'll first need an instrument description in JSON format generated by Sonant-X Live. To get an example one, you can click here then choose one of the tracks of the sample song and click on the "Export" button on the top right of the application. You will then get a JSON description of the instrument used for the selected track.

With that instrument, we will be able to create a SoundGenerator object:

var soundGen = new sonantx.SoundGenerator(jsonInstrument);

Then it depends if you prefer to use the Web Audio API or the HTML5 Audio class.

Web Audio API

soundGen.createAudioBuffer(147, function(buffer) {
    var source = audioCtx.createBufferSource();
    source.buffer = buffer;
    source.connect(audioCtx.destination);
    source.start();
});

The first argument of createAudioBuffer is the number of the note to generate. In this example we use 147 which corresponds to C5. 148 will correspond to C#5, 149 to D5, etc...

Note that the buffer object can be stored and re-used any number of times.

Audio Class

soundGen.createAudio(147, function(audio) {
    audio.play();
});

Note that, like all Audio objects, this one is not re-usable. If you want to be able to re-create Audio objects with minimum delay, you can use the following syntax:

soundGen.getAudioGenerator(147, function(audioGenerator) {
    var audio = audioGenerator.getAudio();
    audio.play();
});

Here the audioGenerator can be used to create any number of Audio instances without the need to re-calculate the whole sound. Remember that, even if the sound is not re-calculated, each creation of an HTML5 Audio object provoke an impossible to avoid delay in most browsers. It can be as short as 0.1 second but it's still a burden for applications like video games. This is why using the Web Audio API is recommended.