createVariations
Added in 6.5.0
Give several takes of a sound one name. From then on play(name) picks one of the takes, with a random pitch and volume inside the ranges you set. See Variations for when to use it.
createVariations(id: string, members: string[], options?: VariationOptions): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | The name to play the takes under. It must not be the id of a sound or stream. |
members | string[] | required | The ids of the takes. Any loaded sound, including a sprite such as 'ui_click'. |
options | VariationOptions | {} | How a take is picked and how much it varies, see below. |
VariationOptions
| Property | Type | Default | Description |
|---|---|---|---|
order | 'random' | 'shuffle' | 'cycle' | 'random' | 'random' picks any take except the one that just played. 'shuffle' plays every take once, in a random order, before any repeats. 'cycle' plays them in the order given. |
pitch | [number, number] | none | A random playback rate between the two, as [min, max]. |
volume | [number, number] | none | A random volume factor between the two, as [min, max]. |
overlap | boolean | true | Let the takes overlap each other. |
The full type is on the VariationOptions page.
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSounds([
{ id: 'step1', url: '/audio/step1.mp3' },
{ id: 'step2', url: '/audio/step2.mp3' },
{ id: 'step3', url: '/audio/step3.mp3' },
]);
soundHub.createVariations('footstep', ['step1', 'step2', 'step3'], {
pitch: [0.95, 1.05],
volume: [0.8, 1],
});
soundHub.play('footstep'); // one of the three, never the same one twice in a row
soundHub.play('footstep');
Takes from one sprite
await soundHub.loadSound('impacts', '/audio/impacts.mp3');
soundHub.setSoundSprite('impacts', {
hit1: [0, 0.4],
hit2: [0.5, 0.9],
hit3: [1, 1.4],
});
soundHub.createVariations('hit', ['impacts_hit1', 'impacts_hit2', 'impacts_hit3'], {
order: 'shuffle',
});
Play options still apply
// The pitch spread multiplies the rate you pass, the volume spread the volume
soundHub.play('footstep', { volume: 0.5, playbackRate: 1.2, pan: -0.3 });
Good to know
play(name)returns theSoundof the take it picked. With overlap on, that is an instance such as'step2:4'.- The
pitchfactor multiplies theplaybackRateyou pass toplay, or 1. Thevolumefactor multiplies thevolumeyou pass toplay, or 1. - Without a
volumerange, a take plays at its own volume. With one, the factor multiplies thevolumepassed toplay, or the take's own volume whenplaygets none. A take you turned down withsetSoundVolumestays down. - An
overlaporcreateNewInstanceyou pass toplaywins over theoverlapoption. stopon the name stops every take it started, instances included.- The name works as a target or a trigger in
duck. - Other methods, such as
pauseorsetSoundVolume, do not know the name. Use the ids of the takes for those. - A name that is already a sound or stream throws, and so does an empty list of takes.
- Calling it again with the same name replaces the set and starts the order over.
- The takes are not checked when you create the set. A take that is not loaded fails when it is picked, the way
playfails for any unknown id.
See also
- Variations: the overview.
removeVariations: forget a variation name.getVariations: the takes behind a name.setSoundSprite: cut the takes out of one file.