resetSound
Stop one sound and put its settings back. Use it when one sound has been bent out of shape (sped up, panned, moved in 3D) and you want it clean again without resetting the whole hub.
resetSound(id: string, options?: SoundResetOptions): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of a loaded buffered sound, or an instance id like 'click:2'. |
options | SoundResetOptions | {} | What to keep. With {} everything below is reset. |
What happens to the sound:
| Step | Skipped by |
|---|---|
Stop it if it is playing, and set its position back to 0 | Always runs |
| Take the stereo panner off | keepPanning: true |
| Take the 3D panner off and switch the sound back to stereo | keepSpatial: true |
Set the playback rate to 1 | keepPlaybackRate: true |
| Set the volume to the volume stored in its play options | keepVolumes: true |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('rain', '/audio/rain.mp3');
soundHub.play('rain', { loop: true, pan: -0.8, playbackRate: 1.5 });
// Put the rate and pan back, keep the volume
soundHub.resetSound('rain', { keepVolumes: true });
soundHub.getPlaybackRate('rain'); // 1
soundHub.isPlaying('rain'); // false, a reset always stops
Try it
Change a sound, then reset it
IdleMove the sliders while it plays, then reset. To keep some settings, pass options such as resetSound(id, { keepVolumes: true }).
Code
Your clicks show up here as soundhub calls
Good to know
- The volume goes back to
playOptions.volume, falling back to the config'sdefaultVolume. Bothplay()with avolumeandsetSoundVolumewrite that value, so in practice the level usually stays the same. What the step does undo is a mute or a fade in progress. - Removing the stereo panner does not clear the
panstored in the play options, so the nextplay()pans the sound again. Play withpan: 0or callresetPanif it should stay centred. - A sound positioned in 3D always gets its spatial position reset to
0, 0, 0, even withkeepSpatial: true. The option only keeps the 3D panner in place. - The playback rate goes to
1, not to the config'sdefaultPlaybackRate, and that value is written to the play options. - Dispatches a
resetevent (SoundEventsEnum.RESET) withsoundIdandresetOptions. The steps above can dispatch their own events too, such asstoppedandvolume_changed. - Streams from
loadStreamand unknown ids are ignored. In debug mode a line is logged, nothing else happens.
See also
reset: reset the whole hub, every sound included.SoundResetOptions: every option and what it keeps.resetPan: centre a sound without stopping it.resetSpatialPosition: move a 3D sound back to the origin.