Tutorial: Implement 3D spatial audio

Implement 3D spatial audio

Overview

In scenarios such as the social metaverse, users need some stereo sound effects in conjunction with the scenario to allow users to perceive the presence of other users around the game character and their corresponding distance and orientation to improve the fun of voice calls, in addition to simple voice communication. In this article, we describe how to enable 3D spatial audio in developing TRTC applications.

Prerequisites

Platform Win Android iOS Mac
Supported ✅Chrome 86+ ✅Chrome 86+ ✅Chrome 86+

Feature Description

Reference Quick Start Call to implement a basic audio/video call process.

Integrated SDK

Since the spatial audio feature is not required by all applications, we have added a trtc-spatial.js in the node_modules/trtc-js-sdk directory to specifically implement 3D spatial audio.

// When your application does not need spatial audio
import TRTC from 'trtc-js-sdk';
// // When your application needs spatial audio
import TRTC from 'trtc-js-sdk/trtc-spatial.js';

Init

// create client
const client = TRTC.createClient({ mode: 'rtc', sdkAppId, userId, userSig });
const { detail } = await TRTC.checkSystemRequirements();
// If you need to use spatial audio, you should enable it before joining the room.
if (detail.isSpatialAudioSupported) {
  client.enable3DSpatialAudioEffect(true);
}
// join room
await client.join({ roomId: 8888 });
client.on('stream-added', event => {
  const remoteStream = event.stream;
  console.log('New remote stream:' + remoteStream.getId());
  // subscribe remote user
  client.subscribe(remoteStream);
});
client.on('stream-subscribed', event => {
  const remoteStream = event.stream;
  console.log('subscribe succrss:' + remoteStream.getId());
  // play
  remoteStream.play('remote_stream-' + remoteStream.getId());
});
// When you successfully subscribe a user, you can update their location information in real time.
client.updateRemote3DSpatialPosition('tom', [100, 200, 0]);
// You can also update your location information
client.updateSelf3DSpatialPosition([100, 0, 0], [2, 1, 0], [-1, 2, 0], [0, 0, 1]);

API

Client.enable3DSpatialAudioEffect(enabled): void

Enable 3D spatial audio effects.

If you need to use spatial audio, you should enable it before you join the room.

eg:

// enable
// Good:
client.enable3DSpatialAudioEffect(true);
await client.join({ roomId: 8888 });
// Bad:
await client.join({ roomId: 8888 });
client.enable3DSpatialAudioEffect(true);
client.enable3DSpatialAudioEffect(false);

Client.updateSelf3DSpatialPosition(position, axisRight, axisForward, axisUp): void

Update the position and orientation of the listener.

Parameters Type Description
position Number[] The coordinates of itself in the world coordinate system. The parameter is a number array of length 3, and the three values represent the right, front, and top coordinate values in that order.
axisRight Number[] The vector of the right axis in its own coordinate system. The argument is a number array of length 3, and the three values represent the right, front and top coordinate values.
axisForward Number[] The vector of the front axis in its own coordinate system. The argument is a number array of length 3, the three values represent the right, front and top coordinate values.
axisUp Number[] The upper axis vector of its own coordinate system. The argument is a number array of length 3, and the three values represent the right, front and top coordinate values.

eg:

client.updateSelf3DSpatialPosition([100, 0, 0], [2, 1, 0], [-1, 2, 0], [0, 0, 1])

Client.updateRemote3DSpatialPosition(userId, position): void

You can get the location information of remote users through your own synchronization service. and update the location information of the remote user.

Parameters Type Description
userId String The ID of the remote user.
position Number[] Their own coordinates in the world coordinate system. This parameter is an array of numbers of length 3, with three values representing the right, front and top coordinate values in that order.

eg:

client.updateRemote3DSpatialPosition('tom', [100, 200, 0]);

Client.set3DSpatialReceivingRange(range): void

Set the maximum range of audio reception distance.

Parameters Type Description
range Number The maximum range of the audio reception distance. Default range: 10000

eg:

client.set3DSpatialReceivingRange(10000);