Skip to main content

getSpriteConfig

Get the sprite ranges recorded on a sound. Useful for building a list of available effects, or checking a key exists before you play it.

getSpriteConfig(id: string): { [key: string]: [number, number] } | undefined;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound the sprites were set on.

Returns​

{ [key: string]: [number, number] } | undefined: every key with its [start, end] in seconds, or undefined when no sprites are recorded on the sound or the id is not loaded.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('game-sounds', '/audio/8-bit-game-sounds.mp3');

soundHub.setSoundSprite('game-sounds', { jump: [4.5, 5.5], fail: [6, 8.5] });

const config = soundHub.getSpriteConfig('game-sounds');
if (config && 'jump' in config) {
soundHub.playSprite('game-sounds', 'jump');
}

Object.keys(config ?? {}); // ['jump', 'fail']

Good to know​

  • Since 6.3.2 the config grows with every setSoundSprite call, and removeSpriteSound takes the key out again.
  • You get the live object the hub keeps, not a copy. Changing it changes what the hub has recorded, but does not create or move any sprite sound. Use setSoundSprite for that.
  • A key whose range was empty or outside the file is listed here even though no sprite sound was made for it.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​