Analysing Audio Levels
Animating an avatar's mouth to match the audio levels of a microphone input, visualizing the beat of audio objects, or simply showing an audio meter - for all these use cases, the audio level of an audio source is needed.
For this the Engage SDK provides an RMS Analyser which regularly outputs/updates the audio level of audio objects and remote participants.
RMS stands for Root Mean
Square (opens in a new tab) and is a common
method to calculate the average audio level of a signal.
Audio level of remote participants
The
RemoteParticipant
class has a built-in RMS analyser which - when enabled -
regularly updates the RemoteParticipant.audioLevel
member. This value is a number, usually between 0 and 1, where 0 means no audio and 1 means maximum audio level.The analyser can be enabled and disabled using the
RemoteParticipant.setAnalyseAudioLevel
method.The update rate is 10 times per second. So polling this value more often
than that is not useful.
Audio level of audio objects
Audio objects do not have a built-in analyser.
In order to analyse the their level (or better: the level of the audio source they are playing), an analyser has to be created and attached to the audio object.
For that the SDK provides a factory function
createRMSAnalyser
which creates an RMSAnalyser
object.This class has two parameters:
smoothingTime
and updateInterval
. The first controls the smoothing of the audio level. The second controls the update rate at which the user-defined RMSAnalyser.callback
is called.A typical usage would be:
import { AudioFile, createRMSAnalyser } from "@atmokyaudio/engage-client";
// create audio source and audio object
const audioFile = new AudioFile(space.getAudioContext());
audioFile.load(urlToFile);
const audioObject = space.createAudioObject();
audioObject.setInput(audioFile);
// create and setup analyser
const rmsAnalyser = createRMSAnalyser(space.getAudioContext());
rmsAnalyser.callback = (rms: number) => {
console.log(rms);
};
rmsAnalyser.smoothingTime.value = 0.2; // 200ms smoothing
rmsAnalyser.updateInterval.value = 0.2; // 5 times per second
// connect audio source to analyser
audioFile.connect(rmsAnalyser);