getSpatialPosition
Get the stored 3D position of a sound. Useful when you want to move a sound relative to where it is now.
getSpatialPosition(soundId: string): { x: number; y: number; z: number } | null;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
soundId | string | required | ID of a loaded sound or of one of its instances. |
Returns
{ x: number; y: number; z: number } | null: the position last stored for the sound, or null when the id is unknown.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('rain', '/audio/rain.mp3');
soundHub.play('rain', { loop: true });
soundHub.setSpatialPosition(10, 0, -20, 'rain');
const position = soundHub.getSpatialPosition('rain'); // { x: 10, y: 0, z: -20 }
if (position) {
// Nudge it one unit closer
soundHub.setSpatialPosition(position.x, position.y, position.z + 1, 'rain');
}
Good to know
- A loaded sound that was never positioned returns
defaultPanSpatialPositionfrom the hub config,{ x: 0, y: 0, z: 0 }by default, notnull. - The values are the rounded ones (2 decimals) that
setSpatialPositionstored. setPanandremoveSpatialEffectput the stored position back to0, 0, 0.- Streams are not looked up, so a stream id returns
null.
See also
setSpatialPosition: place a sound in 3D.getSpatialOrientation: the direction a sound points in.getMasterSpatialPosition: the position of the master panner.