setPan
Set the stereo pan of one sound. Reach for it to place a sound left or right, such as footsteps on one side of the screen.
setPan(id: string, pan: number): void;
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream to pan. |
pan | number | required | Pan position: -1 is full left, 0 is centre, 1 is full right. Values outside that range are clamped. |
Returns
Nothing.
Example
import { SoundHub } from 'soundhub';
const soundHub = new SoundHub();
await soundHub.loadSound('rain', '/audio/rain.mp3');
soundHub.play('rain', { loop: true });
soundHub.setPan('rain', -0.5); // halfway to the left
soundHub.setPan('rain', 0.8); // mostly right
soundHub.setPan('rain', 0); // centre
Try it
Move a sound left and right
IdlePlay the helicopter and drag the slider. Headphones make the movement easy to follow.
Code
Your clicks show up here as soundhub calls
Good to know
- If the sound was positioned in 3D,
setPanremoves the spatial panner and moves the sound back to a stereo pan. Its spatial position goes back to0, 0, 0without aspatial_position_changedevent. - The value is stored in the sound's play options, so the next
play()of that sound starts at the same pan. - Every call dispatches
SoundEventsEnum.PAN_CHANGED('pan_changed'). The class method takes an optional third argument,skipDispatchEvent, to leave the event out. It is not onSoundHubInterface. - Works on streams from
loadStreamtoo. For an id that is not loaded, the error is logged, stored forgetLastErrorand dispatched as anerrorevent. - This pans one sound. The master stereo panner set by
setGlobalPansits after it, so both apply.
See also
resetPan: put a sound back in the centre.removePan: take the stereo panner off a sound.setGlobalPan: pan the whole mix at once.setSpatialPosition: place a sound in 3D instead.