Skip to content

My Favorite LiveSwitch Audio Hack

Jacob Steele Jul 26, 2022 6:46:59 AM

LiveSwitch Audio Stream provides crystal-clear voice chat inside your web, mobile and native apps for more efficient communication. People commonly want to know what audio options they have for web, how to adjust them and what I recommend.

Question: What is your favorite way to handle audio in LiveSwitch Web?

Answer:There are two different code snippets I want to share when talking about audio, LiveSwitch and web. The first is an interesting way to adjust both the audio and video settings for WebRTC and then merging that userMedia object into a LiveSwitch LocalMedia wrapper.


      
      var constraints = {
        
audio: {
echoCancellation: true,
autoGainControl: true,
noiseSuppression: true,
channelCount: 2
},
video: {
width: { min: 320, ideal: 320, max: 640 },
height: { min: 180, ideal: 240, max: 480},
facingMode: "user"
}
}
try {
let stream = await navigator.mediaDevices.getUserMedia(constraints);
this.localMedia = new fm.liveswitch.LocalMedia();
this.localMedia._internal._setAudioMediaStream(stream);
this.localMedia._internal._setVideoMediaStream(stream);
this.localMedia._internal.setState(fm.liveswitch.LocalMediaState.Started);
this.createLocalMediaButtons();
} catch(ex) {
fm.liveswitch.Log.error("Error starting local media", ex);
}

The code snippet above does a lot of adjustments. Browsers will naturally ignore parameters they don’t support so you don’t have to be afraid of that parameter.

If you want to capture more background noise or music outside of the user's voice, it helps to disable auto gain control and noise suppression.

Setting the channel count to 2, suggests you would like to use stereo audio when it’s available from the microphone source. However in chrome, this value alone isn’t enough to have opus output stereo in LiveSwitch, enter the code snippet below.

More information about channel count can be found here along with updated supported browsers.

As of publishing, some browsers reduce the audio processing done on the audio when stereo is enabled.


      
      connection.addOnLocalDescription(function (conn, sessionDescription) {
        
        
var sdpMessage = sessionDescription.getSdpMessage();
var audioDescription = sdpMessage.getAudioDescription();  
var rtpMapAttribute = audioDescription.getRtpMapAttribute("opus", 48000, "2");
if (rtpMapAttribute) {
var formatParametersAttribute = rtpMapAttribute.getRelatedFormatParameters
Attribute
();
formatParametersAttribute.setFormatSpecificParameter("stereo", "1");
}
});

 

This snippet checks to see if the browser is Google Chrome then adjust the SDP description to signal support for opus stereo. This internally also changes the browser to attempt to use 2 channel audio.