Skip to main content

Variations

Added in 6.5.0

The same footstep a hundred times in a row sounds like a machine. Record three or four takes, give them one name, and let the hub pick one each time:

soundHub.createVariations('footstep', ['step1', 'step2', 'step3'], {
pitch: [0.95, 1.05],
volume: [0.8, 1],
});

soundHub.play('footstep'); // never the same take twice in a row

Try it​

Try it

Four takes, one name

Idle

Play the footstep a few times, then walk. Set the spread to 0 and the order to cycle to hear it turn into a loop.

order
0.94 to 1.06
Plays0
Same take twice in a row0
The takes it picks show up here

The four takes are rendered in the page and added with addBuffer, so this demo loads no files.

Code
Your clicks show up here as soundhub calls

How it works​

  1. Load the takes as ordinary sounds, from separate files with loadSounds or from one file with setSoundSprite.
  2. Give them a name with createVariations. The name is not a sound itself, it only points at the takes.
  3. play(name) picks a take by the order you chose, draws a playback rate and a volume factor from the ranges, and plays that take. What it returns is the take's Sound.

The takes overlap by default, so a quick run of footsteps does not cut each step off. stop(name) stops every take the name started.

Where the name works​

MethodWith a variation name
playPlays one take
stopStops every take and its instances
duckWorks as a target and as a trigger
everything elseUse the ids of the takes

Events come from the take, so a started event carries 'step2' as originalId, not 'footstep'. To follow every play of the set, listen once per take with an originalId filter.

The methods​

MethodWhat it does
createVariationsGive several takes one name.
removeVariationsForget the name, keep the takes.
getVariationsThe takes behind a name.

The options are described on the VariationOptions page.

Good to know​

  • A variation name cannot be the id of a sound or stream, and addBuffer refuses a variation name as its id.
  • destroy forgets every variation name.
  • A random order with two takes alternates between them, since a take never follows itself.