Skip to main content

SoundPanType

SoundPanType says how a sound is panned: stereo, between the left and right channel, or spatial, at a position in a 3D scene. A sound uses one or the other, never both.

export enum SoundPanType {
Stereo = 'stereo',
Spatial = 'spatial'
}
ValueStringDescription
SoundPanType.Stereo'stereo'A stereo panner node, from -1 (full left) to 1 (full right). The default for every sound.
SoundPanType.Spatial'spatial'A 3D panner node at an x, y, z position, heard from the listener. See Spatial audio.

Import it from the package:

import { SoundPanType } from 'soundhub';

Where it is used​

  • panType in the play options, next to pan for stereo and panSpatialPosition for spatial.
  • defaultPanType in the hub config, SoundPanType.Stereo by default.

Example​

import { SoundHub, SoundPanType } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('rain', '/audio/rain.mp3');

// Stereo: shifted to the left
soundHub.play('rain', {
loop: true,
panType: SoundPanType.Stereo,
pan: -0.8,
});

// Switch to spatial: ten units to the right, fifteen ahead
soundHub.setSpatialPosition(10, 0, -15, 'rain');
soundHub.isSpatialAudioActive('rain'); // true
soundHub.isStereoPanActive('rain'); // false

// And back to stereo
soundHub.setPan('rain', 0.5);
soundHub.isStereoPanActive('rain'); // true

Try it​

Move the sound between the left and right channel to hear stereo panning at work. Use headphones for the clearest effect.

Try it

Move a sound left and right

Idle
centre
LR

Play 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​

  • The methods switch the type for you. setSpatialPosition removes the stereo panner and sets Spatial. setPan removes the 3D panner, sets Stereo and puts the spatial position back to 0, 0, 0.
  • For a sound played without overlap, panType: SoundPanType.Spatial on its own does not make it spatial. A non-zero panSpatialPosition does. For an overlapping instance, panType is taken as given.
  • updateSoundOptions only stores new options for the next play(). It does not rewire a sound that is already playing, so use setSpatialPosition or setPan to switch live.
  • Streams from loadStream are always stereo.

See also​