Skip to content

Commit

Permalink
Publish a functional API.
Browse files Browse the repository at this point in the history
  • Loading branch information
chr15m committed May 25, 2022
1 parent 3110fc0 commit c8dd592
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
38 changes: 15 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,20 @@ You can then directly use the `SoundEffect`, `Params` etc.

## API

Generate a sound effect using a preset algorithm.
Generate a sound effect using a preset algorithm and play it using webaudio API.

```javascript
const Params = require("jsfxr").Params;
const sfxr = require("jsfxr").sfxr;

const preset = "pickupCoin";
const sound = sfxr.generate(preset);

const p = new Params();
p.sound_vol = SOUND_VOL;
p.sample_rate = SAMPLE_RATE;
p.sample_size = SAMPLE_SIZE;
const sound = p[preset]();

const s = new SoundEffect(sound).generate();
s.getAudio().play();
sfxr.play(sound);
```

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

You can also use the interface at https://sfxr.me to find the sound you want and then get the sound definition.
You can also use the interface at https://sfxr.me to find the sound you want and then use the sound definition.
Click the "serialize" button and copy the JSON code for the sound you want.
You will get a datastructure that you can use like this:

Expand Down Expand Up @@ -98,29 +92,27 @@ var sound = {
"sample_size": 8
};

var s = new SoundEffect(sound).generate();
// returns a webaudio object if supported, or an Audio object
s.getAudio().play();
var a = sfxr.toAudio(sound);
a.play();
```

You can also use the short URL compressed version of the sound::

```javascript
var s = new SoundEffect("5EoyNVSymuxD8s7HP1ixqdaCn5uVGEgwQ3kJBR7bSoApFQzm7E4zZPW2EcXm3jmNdTtTPeDuvwjY8z4exqaXz3NGBHRKBx3igYfBBMRBxDALhBSvzkF6VE2Pv").generate();
s.getAudio().play();
var a = sfxr.toAudio("5EoyNVSymuxD8s7HP1ixqdaCn5uVGEgwQ3kJBR7bSoApFQzm7E4zZPW2EcXm3jmNdTtTPeDuvwjY8z4exqaXz3NGBHRKBx3igYfBBMRBxDALhBSvzkF6VE2Pv");
a.play();
```

You can also access an array of samples if you want to use the WebAudio API to play the sound. By default the audio is rendered at a sample rate of `44100`.
You can also access an array of samples.
By default the buffer contains audio rendered at a sample rate of `44100`.

```
console.log(s.buffer)
var buffer = sfxr.toBuffer(sound);
console.log(buffer);
```

You can find more example code for using the WebAudio API on lines 55 and 81 of the index.html file.

* 55: https://github.com/chr15m/jsfxr/blob/master/index.html#L55
* 81: https://github.com/chr15m/jsfxr/blob/master/index.html#L81
* index.html: https://github.com/chr15m/jsfxr/blob/master/index.html
You can also access the lower level classes `SoundEffect` and `Params` if you need to.
This can be useful for caching the internal representation for efficiency, or mutating the sound with `params.mutate()`.

# links

Expand Down
15 changes: 14 additions & 1 deletion sfxr.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ sfxr.toWave = function(synthdef) {
};

sfxr.toAudio = function(synthdef) {
return (new SoundEffect(synthdef)).generate().getAudio();
return sfxr.toWave(synthdef).getAudio();
}

sfxr.play = function(synthdef) {
return sfxr.toAudio(synthdef).play();
}

sfxr.b58decode = function(b58encoded) {
Expand All @@ -510,6 +514,15 @@ sfxr.b58decode = function(b58encoded) {
return result;
}

sfxr.generate = function(algorithm, options) {
const p = new Params();
const opts = options || {};
p.sound_vol = opts["sound_vol"] || 0.25;
p.sample_rate = opts["sample_rate"] || 44100;
p.sample_size = opts["sample_size"] || 8;
return p[algorithm]();
}

/*** Main entry point ***/

function SoundEffect(ps) {
Expand Down

0 comments on commit c8dd592

Please sign in to comment.