jsfxr Documentation

repository·master·Indexed 19 days ago

https://github.com/chr15m/jsfxr

A JavaScript port of the sfxr tool for generating 8-bit retro-style game sound effects using the Web Audio API. Version 1.4.0 supports Node.js and browser environments, providing capabilities to generate sounds via presets, JSON definitions, or Base58 strings. It includes the RIFFWAVE class for encoding audio data into WAV files and a CLI tool, sfxr-to-wav, for converting sound definitions to .wav files.

Tokens
4.2K
Snippets
20
Records
23
Agent score
65%

What's inside jsfxr

  1. Include jsfxr in the Browser

    master

    To use jsfxr directly in a web page without a module bundler, include the following scripts. This will expose the sfxr namespace globally:

    <script src="https://sfxr.me/riffwave.js"></script>
    <script src="https://sfxr.me/sfxr.js"></script>
  2. Initialize the jsfxr library with RIFFWAVE

    master

    The jsfxr library requires RIFFWAVE to be available in the global scope (globalThis or window) before the main sfxr.js logic is loaded. If you are using the ESM wrapper sfxr.mjs, this initialization is handled automatically. However, if you are manually integrating the components, you must ensure RIFFWAVE is attached to the global object before calling SoundEffect.generate().

    import RIFFWAVE from './riffwave.js';
    
    // Ensure RIFFWAVE is global before loading sfxr
    if (typeof globalThis !== 'undefined') globalThis.RIFFWAVE = RIFFWAVE;
    if (typeof window !== 'undefined') window.RIFFWAVE = RIFFWAVE;
    
    import jsfxr from './sfxr.js';
  3. Access low-level SoundEffect and Params classes

    master
    For advanced usage, such as caching internal representations for efficiency or mutating sound parameters using params.mutate(), you can access the lower-level SoundEffect and Params classes.
  4. Encode and decode sounds between JSON and Base58

    master

    You can convert between the full JSON sound definition and the compressed Base58 string format using sfxr.b58encode and sfxr.b58decode.

    var b58string = sfxr.b58encode(sound);
    var sound = sfxr.b58decode(b58string);
  5. Generate and play sounds using presets

    master

    You can quickly generate sound effects using built-in preset algorithms. Use sfxr.generate(preset) to create the sound object and sfxr.play(sound) to play it immediately via the Web Audio API.

    Available presets: pickupCoin, laserShoot, explosion, powerUp, hitHurt, jump, blipSelect, synth, tone, click, and random.

    import { sfxr } from "jsfxr";
    
    const preset = "pickupCoin";
    const sound = sfxr.generate(preset);
    
    sfxr.play(sound);
  6. Play sounds from JSON definitions

    master

    You can use the sfxr.me web application to design sounds, click the "serialize" button, and copy the resulting JSON. Use sfxr.toAudio(sound) to convert this JSON object into an audio object that you can play.

    var sound = {
      "oldParams": true,
      "wave_type": 1,
      // ... other parameters
      "sample_rate": 44100,
      "sample_size": 8
    };
    
    var a = sfxr.toAudio(sound);
    a.play();
  7. Play sounds from compressed Base58 strings

    master

    To save space, you can use the short URL compressed version of a sound. Pass the Base58 string directly to sfxr.toAudio() to create a playable audio object.

    var a = sfxr.toAudio("5EoyNVSymuxD8s7HP1ixqdaCn5uVGEgwQ3kJBR7bSoApFQzm7E4zZPW2EcXm3jmNdTtTPeDuvwjY8z4exqaXz3NGBHRKBx3igYfBBMRBxDALhBSvzkF6VE2Pv");
    a.play();
  8. Convert sounds to Buffers or WAV DataURIs

    master

    Use the following methods to extract raw audio data from a sound object:

    • sfxr.toBuffer(sound): Returns an array of samples. By default, the buffer contains audio rendered at a sample rate of 44100.
    • sfxr.toWave(sound): Returns an object containing a dataURI for a WAV file of the sound.
    // Get audio buffer
    var buffer = sfxr.toBuffer(sound);
    
    // Get WAV DataURI
    var a = sfxr.toWave(sound);
    console.log(a.dataURI);
  9. Play sound effects with sfxr.play()

    master

    The sfxr.play(synthdef) method is a high-level helper that converts a sound definition into audio and plays it immediately.

    In a browser environment, it uses the Web Audio API if available, otherwise it falls back to an HTML5 Audio object using a Data URI. It returns an object containing a play method and a setVolume method for controlling the playback.

    // synthdef can be a Params object or a B58 encoded string
    sfxr.play(mySynthDef);
  10. Generate sound effects with sfxr.generate()

    master

    Use sfxr.generate(algorithm, options) to quickly create a sound effect using one of the built-in preset algorithms. The algorithm string must correspond to a method name on the Params prototype (e.g., 'pickupCoin', 'explosion', 'laserShoot').

    Options:

    • sound_vol: Volume (default: 0.25)
    • sample_rate: Sample rate in Hz (default: 44100)
    • sample_size: Bit depth (default: 8)

    Returns a SoundEffect instance configured with the randomized preset.

    // Generate a random explosion sound
    const explosion = sfxr.generate('explosion', { sound_vol: 0.5 });
    
    // Play it immediately (if in a browser environment)
    sfxr.play(explosion);