Skip to main content

Class: VoiceChat

Proximity voice for a world that has it (WorldConfig::voice_range on the server).

Register it on the network, call enable from a click, and call update every frame with the ear and a way to find a speaker:

const voice = new VOXELIZE.VoiceChat();
network.register(voice);
button.onclick = () => voice.enable();
// every frame
voice.update(
{ position, forward, up },
(id) => peers.getPeerById(id)?.position.toArray(),
);

It captures the microphone, sends it while the gate is open (push-to-talk or open mic), and plays every voice the server relays from where its player stands, fading with distance to silence at the world's range. It draws nothing: getState, getSpeakers and the peers' voice metadata are what a game's UI reads.

Implements​

Constructors​

constructor​

• new VoiceChat(options?): VoiceChat

Parameters​

NameType
optionsVoiceChatOptions

Returns​

VoiceChat

Properties​

packets​

• packets: MessageProtocol[] = []

Outgoing packets, sent by network.flush().

Implementation of​

NetIntercept.packets

Accessors​

inputLevel​

• get inputLevel(): number

The level of the player's own microphone, 0..1, for a meter.

Returns​

number


isAvailable​

• get isAvailable(): boolean

Whether the world this client joined has voice.

Returns​

boolean

Methods​

disable​

▸ disable(): void

Turn voice off: the microphone closes and nothing is heard.

Returns​

void


dispose​

▸ dispose(): void

Close everything. The instance is not usable afterwards.

Returns​

void


enable​

▸ enable(«destructured»?): Promise<void>

Turn voice on. Call it from a click or a key press: it is what opens the browser's microphone prompt and starts audio. With withMicrophone: false, or when the microphone is refused, the player listens without speaking (see micStatus).

Parameters​

NameTypeDefault value
«destructured»Object{}
› withMicrophonebooleantrue

Returns​

Promise<void>


getSpeakerVolume​

▸ getSpeakerVolume(speakerId): number

The player's volume for speakerId; 1 unless they changed it.

Parameters​

NameType
speakerIdstring

Returns​

number


getSpeakers​

▸ getSpeakers(): readonly VoiceSpeaker[]

The players heard right now, as of the last update, nearest first: speaking, and near enough that their voice has any volume. The array is replaced, never mutated, when who is in it changes; each entry's level and distance are refreshed in place every update.

Returns​

readonly VoiceSpeaker[]


getState​

▸ getState(): VoiceChatState

The current state. A new object whenever anything in it changes.

Returns​

VoiceChatState


getStats​

▸ getStats(): VoiceChatStats

Counters for a debug overlay: when a voice sounds choppy, these say whether frames are not arriving, arriving late, or arriving in bursts the page was too busy to play.

Returns​

VoiceChatStats


isSpeakerMuted​

▸ isSpeakerMuted(speakerId): boolean

Whether speakerId is muted by the player.

Parameters​

NameType
speakerIdstring

Returns​

boolean


joinWorld​

▸ joinWorld(ownId, voiceRange): void

Tell voice which world this client is in: its own id there and the world's voiceRange option. Registered on the network before the join, voice reads both from the INIT itself; this is for a voice chat made after it (pass what world.initialData kept).

Parameters​

NameType
ownIdstring
voiceRangenumber

Returns​

void


onMessage​

▸ onMessage(message): void

Parameters​

NameType
messageMessageProtocol

Returns​

void

Implementation of​

NetIntercept.onMessage


releaseMicrophone​

▸ releaseMicrophone(): void

Close the microphone and keep listening.

Returns​

void


resume​

▸ resume(): void

Start audio the browser is holding back. Voice turned on without a click (restored from a saved setting on join) may be given a suspended context; call this from the page's next click or key press.

Returns​

void


retryMicrophone​

▸ retryMicrophone(): Promise<void>

Open the microphone again after a refusal, or after enable without one.

Returns​

Promise<void>


setActivation​

▸ setActivation(activation): void

Parameters​

NameType
activationVoiceActivation

Returns​

void


setDeafened​

▸ setDeafened(isDeafened): void

Stop hearing everyone (and stop sending) without turning voice off.

Parameters​

NameType
isDeafenedboolean

Returns​

void


setInputDevice​

▸ setInputDevice(deviceId): Promise<void>

Pick a microphone (null for the browser's default).

Parameters​

NameType
deviceIdstring

Returns​

Promise<void>


setMicMuted​

▸ setMicMuted(isMicMuted): void

Parameters​

NameType
isMicMutedboolean

Returns​

void


setMutedSpeakers​

▸ setMutedSpeakers(speakerIds): void

Replace the whole muted list at once (a saved list, on join).

Parameters​

NameType
speakerIdsIterable<string, any, any>

Returns​

void


setSpeakerMuffle​

▸ setSpeakerMuffle(speakerId, muffle): void

How much stands between the ear and a speaker, 0 (clear air) to 1 (a wall): the game's to work out, with whatever it knows about its world.

Parameters​

NameType
speakerIdstring
mufflenumber

Returns​

void


setSpeakerMuted​

▸ setSpeakerMuted(speakerId, isMuted): void

Mute one speaker for this player. The server stops sending their voice here at all; nobody else is affected and the speaker is not told.

Parameters​

NameType
speakerIdstring
isMutedboolean

Returns​

void


setSpeakerVolume​

▸ setSpeakerVolume(speakerId, volume): void

The player's own volume for one speaker; 1 is as spoken, 2 is the most.

Parameters​

NameType
speakerIdstring
volumenumber

Returns​

void


setTalkKeyHeld​

▸ setTalkKeyHeld(isHeld): void

Push-to-talk: whether the talk key is down.

Parameters​

NameType
isHeldboolean

Returns​

void


setTesting​

▸ setTesting(isTesting): void

Hear your own microphone, through the same encoder and decoder everyone else gets, instead of sending it. In push-to-talk the gate is held open for the test; in open mic the threshold still applies, so it can be tuned by ear.

Parameters​

NameType
isTestingboolean

Returns​

void


setThreshold​

▸ setThreshold(threshold): void

Open mic: the level, 0..1, a frame must reach to be sent.

Parameters​

NameType
thresholdnumber

Returns​

void


setVolume​

▸ setVolume(volume): void

Volume of all voices together; 1 is as spoken, 2 is the most.

Parameters​

NameType
volumenumber

Returns​

void


subscribe​

▸ subscribe(listener): () => void

Be told when getState changes. Returns the way to stop.

Parameters​

NameType
listener() => void

Returns​

fn

▸ (): void

Returns​

void


subscribeSpeakers​

▸ subscribeSpeakers(listener): () => void

Be told when the set of people heard changes. Returns the way to stop.

Parameters​

NameType
listener() => void

Returns​

fn

▸ (): void

Returns​

void


update​

▸ update(listener, positionOf): void

Once a frame: place every voice. listener is the ear (null keeps the last one); positionOf finds a speaker's position by client id, and a speaker it cannot find is silent.

Parameters​

NameType
listenerVoiceListener
positionOf(speakerId: string) => VoiceVec3

Returns​

void