sonantx

    2.0.1 • Public • Published

    Sonant-X

    Build Status npm

    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.

    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

    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 and choosing "Export JSON" in the menu on the left.

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

    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 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:

    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.

    Install

    npm i sonantx

    DownloadsWeekly Downloads

    22

    Version

    2.0.1

    License

    ISC

    Unpacked Size

    83.8 kB

    Total Files

    13

    Last publish

    Collaborators

    • nicolas-van