Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nicolas-van/sonant-x

Small JavaScript synthesizer library
https://github.com/nicolas-van/sonant-x

audio javascript sound synthesizer

Last synced: 4 days ago
JSON representation

Small JavaScript synthesizer library

Awesome Lists containing this project

README

        

Sonant-X
========

[![Github-sponsors](https://img.shields.io/badge/sponsor-30363D?logo=GitHub-Sponsors&logoColor=#EA4AAA)](https://github.com/sponsors/nicolas-van) [![GitHub Repo stars](https://img.shields.io/github/stars/nicolas-van/sonant-x?style=social)](https://github.com/nicolas-van/sonant-x/stargazers) [![Node.js CI](https://github.com/nicolas-van/sonant-x/actions/workflows/node.js.yml/badge.svg)](https://github.com/nicolas-van/sonant-x/actions/workflows/node.js.yml) [![npm](https://img.shields.io/npm/v/sonantx.svg)](https://www.npmjs.com/package/sonantx)

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 uses the Web Audio API.

Songs for Sonant-X can be composed using [Sonant-X Live](https://github.com/nicolas-van/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](http://nicolas-van.github.io/sonant-x-live/#N4Igzg9gdg5gMgUyiAXARgEwGYA05owAiAhgC7GoDaoEYAxmgPoR2moCcetDjAJgmxQAGLvSb9SAVygJUIkNyYAPJADc5onqogAbVGgBsaTUwDuxVQgBmEAE4BbDQvoZmrDptcSn3LwOmyKAAsnowqUOrCodp66AAcGKHmljYOTlAQAJZgCIxWxPy2TmqMZOR0ANb6QvIlYJJg5JnI8WhxeCW2CDoIxDn6cW3GICX2faQIRejsiSBWSnmZOhNT8vN5XQCOTutdkFDEUHSBGACsIXML%2FDrEAJ6MpJn2gQZ469d3pfaCaELtl4wAA6HDYIbYoXAA4FQL6CIx4HQ2ZhiUHg%2BSIiB5BZWLZODGo1CQ%2FHEb4cC745LWOyOFCz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7JrAOSzEpxnNVa69oYU72VsBAUwiAWhMSEMxJAvAAAKZRJgtFrJaIAAAA%3D) by m / Bits'n'Bites.

Installation
------------

```
npm install --save sonantx
```

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](http://nicolas-van.github.io/sonant-x-live/#N4Igzg9gdg5gMgUyiAXAZgOwBpzRgEQEMAXQ1AbVAjAGMBGAfQhuNWxGvoYBMFWUADDk6NexAK5QEqIR1qMAHkgBuM4fIbKIAG1R0AnACZ1XAO6FlCAGYQATgFtUaE4aYs2LnnzVyarsZLSKOycrkpQqoKeWroohgJ0nuaWNg5OOFAQAJZgCAxWhLy2PioMJKQ0ANZsABz6OKVg4mCkWcgodIZoACwNEQy2CNoIhLl6aABsk33KDPajxAjFHfqJIFYK%2BVnai8vG65tWgwCOqACsNWj1BwMIkFCEUDRBhme9N7zahACeDMRZ9iCExwGy8X1%2BhHs%2FE6a1BAAdHvkTqhgTcEVAylC9N12NobEwNEcEKcoiA8RB8odkR0cOSkcSUbT8ZDofozkyKclrHZHKS4RREsYhEJBVhBQBdHA0CigdrkOhvLAixXKoVinFKzXCrU65W9PWahVqhXYbXK4FmrXigC%2BWFlApVYsdRsNGst7p1Cv1Tu9LuVps9Z3qHoENptdt8jGY%2FBCGjEPhEXgkUgTGnCkVkiZiqFe7MjDC5qV5%2B1Cbn4NU88dJpYCKY6Fd8YRUemiOj0qySFm5aTiGWyuXyhSWJX65UIVSc%2Bn07EazVa7TOUymM1uw1GQToAi3W5X8xaw7iZzzoKs212OZBVIZ6CMl9u90ezxzR7vnx%2BfwBQQboLfEKxKAma54URIkSWPTZ0UxfgJlxfFE1Anw6RPak1iQ6l3jpFlzlgzkuyLdIQH5FB5SwYxRXI0jJRAaViPtYi6DQENdWYpjWJY9jhXDOj5S6Di%2BLYgT%2BKta0uPzaNUAbRMq0zOM%2BECVMuHTBTGGzasNELHkFNccSUEk2h%2FG8NS%2FCTeSjKbCItM0NszIL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KAUMq6%2FTQwiQ%2Fs7MGT03oEPgcmT2%2BZkFZj9kjRMjj0DBzBnpYyYdSFEJsss0hiM8ylg2bIFOGNGGdA4WUFh448aGH2LnYmukc79H4ZFK404q77j2EjZmaVljTwsbXGCd5uYPG7nEHoWj%2B4aPeD%2BQWtV0AgoYFLak8jWr%2FhqLvCkpiSRqxXhY1C6trGax0Z1PeidgaHwoOtE%2BLiPHcXiT%2Fb0rKAGOnmt0Z%2B7LImcr5eMiMa0Blcp6etMVADNqANGQK5igCkn0tFYK2VgYVUXUOpK1VrFIFiW2fMxBByfZkIskZX2sRjXnK2eWCGczQ4IMOeQ2OlkrV0KcgwhmSNmGsOxAIHotyuH5yMHQVExdyYCKSj0MevzErxG%2FIcSRF4m6Yp8qLSFvM6pwvUS8LNI8X6GMnhJAtbV4gmMXmY5elJC2WLJSSfYI1%2BBnFJbhGlTilUiiNp2xaUozZeLvoJAd%2F9%2BK3SorYCAphEALibY3JA3AAAK5QljtH0NaIAAA%3D) and choosing "Export JSON" in the menu on the left.

With that JSON song, we will be able to use the `generateSong()` function:

```javascript
import * as sonantx from 'sonantx'
import mySong from './mySong.json'

const audioCtx = new AudioContext()

sonantx.generateSong(mySong, audioCtx.sampleRate).then((audioBuffer) => {
const audioBufferSource = audioCtx.createBufferSource()
audioBufferSource.buffer = audioBuffer
audioBufferSource.connect(audioCtx.destination)
audioBufferSource.start()
})
```

The `generateSong()` function can take multiple seconds before returning, depending on the length of your song. It is recommended to initialize your music at the startup of the application and keep a reference to the audio buffer.

### 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](http://nicolas-van.github.io/sonant-x-live/#N4Igzg9gdg5gMgUyiAXAZgOwBpzRgEQEMAXQ1AbVAjAGMBGAfQhuNWxGvoYBMFWUADDk6NexAK5QEqIR1qMAHkgBuM4fIbKIAG1R0AnACZ1XAO6FlCAGYQATgFtUaE4aYs2LnnzVyarsZLSKOycrkpQqoKe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Q2OlkrV0KcgwhmSNmGsOxAIHotyuH5yMHQVExdyYCKSj0MevzErxG%2FIcSRF4m6Yp8qLSFvM6pwvUS8LNI8X6GMnhJAtbV4gmMXmY5elJC2WLJSSfYI1%2BBnFJbhGlTilUiiNp2xaUozZeLvoJAd%2F9%2BK3SorYCAphEALibY3JA3AAAK5QljtH0NaIAAA%3D) 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 use the `generateSound()` function:

```javascript
import * as sonantx from 'sonantx'
import myInstrument from './myInstrument.json'

const audioCtx = new AudioContext()

const midiNote = 67
const bpm = 120

sonantx.generateSound(myInstrument, midiNote, audioCtx.sampleRate, bpm).then((audioBuffer) => {
const audioBufferSource = audioCtx.createBufferSource()
audioBufferSource.buffer = audioBuffer
audioBufferSource.connect(audioCtx.destination)
audioBufferSource.start()
})
```

Single sound generation can be very fast but sounds can still be generated ahead of time if precise timing is very
important for you.