The following example shows how to receive the outgoing audio frames and send them to a Web Worker for analysis.
cfg.audio.exposeOutgoingFrames = true;
...
sfs.addEventListener(SFSAudioEvent.OUTGOING_FRAME, onOutgoingFrame, this);
function onOutgoingFrame(evtParams)
{
const stream = evtParams[EventParam.MyAudioStream];
const frame = evtParams[EventParam.AudioFrame];
// Time offset of the frame from the beginning of the stream, in milliseconds
const offsetMs = frame.sequenceNumber * stream.frameDurationMs;
// The samples are copied by postMessage (don't transfer their buffer)
analysisWorker.postMessage({ offsetMs: offsetMs, samples: frame.samples });
}Members
(readonly) samples :Float32Array
The number of samples is sampleRate * frameDurationMs / 1000 * channels (see AudioStreamInfo).
The samples contain the captured audio signal after the audio processing performed by the browser, exactly as it is encoded and transmitted. Audio processing alters the signal in ways which are relevant if it is analyzed by the application:
- echo cancellation (see enableAEC) removes the audio played back by the device (for example the voices of other users) from the captured signal, which prevents it from affecting the analysis;
- noise suppression (see enableNS) applies a frequency-dependent gain to reduce background noise;
- automatic gain control (see enableAGC) varies the signal level over time, also depending on whether speech has been detected;
- when audio processing is enabled, the browser might also apply a high-pass filter, removing the lowest frequencies.
The actual effects of audio processing depend on the browser. If the signal level or spectrum is relevant to the analysis, consider disabling noise suppression and automatic gain control.
- Float32Array
(readonly) sequenceNumber :number
The first audio frame captured after the stream is started has sequence number 0; the value then increases by one for each captured audio frame, including those not delivered to the application because the stream is muted, or because no listeners were registered for the SFSAudioEvent.OUTGOING_FRAME event. The time offset of the audio frame from the beginning of the stream is sequenceNumber * frameDurationMs (see AudioStreamInfo#frameDurationMs).
Sequence numbers count captured audio frames, not elapsed time. If the browser doesn't capture audio for a while (for example because audio processing is suspended by the browser or the operating system), no audio frames are produced and no gap in sequence numbers results: the time offset calculated as described above can then be lower than the actual elapsed time.
- number