Can you get the volume of a webrtc audio stream without playing it?

2.8k views Asked by At

I have a bunch of webrtc streams represented in an angularjs view and I want to visualize which ones are over a certain volume threshold where I can then listen to those ones one at a time. Is this possible? Everything I find seems to require you to attach the stream to your audiocontext to calculate the volume. And this plays the stream. Can you calculate the volume without playing the stream? I have been playing with hark.js for volume threshold detection.

my stream is derived from a 3rd part rtc library but ultimately I have a bunch of webrtc streams and want to listen to one at a time while visualizing which ones are making noise. Most examples of this seem to have this logic on the broadcasters side.

The hark code pretty much does the following:

-start

    var audioContextType = window.AudioContext || window.webkitAudioContext;
    var audioContext = new audioContextType();
    var analyser = audioContext.createAnalyser();
    analyser.fftSize = 512;
    analyser.smoothingTimeConstant = 0.1;
    var node = audioContext.createMediaStreamSource(stream);
    node.connect(analyser);//plays stream

-loop

    var fftBins = new Float32Array(analyser.fftSize);
    analyser.getFloatFrequencyData(fftBins);
    //check fftBins for volume data

-also I get the streams from an icelink, 3rd party webrtc library, object. The video collection in the view is created from a bunch of streams like so

    var remoteStream = fm.icelink.webrtc.linkExtensions.getRemoteStream(link);
    var videoObject = angular.element('<video></video>');
    videoObject.attr('src', window.URL.createObjectURL(remoteStream.getBackingStream()));
    videoObject.attr('autoplay', 'true');
    resolve(videoObject);

I am trying to overlay some visual indicators to show sound level on different video streams without playing them

1

There are 1 answers

5
jib On BEST ANSWER

You don't have to "play" a stream to hook it up to an AudioContext. Just connect to an analyser, and omit node.connect(audioCtx.destination) to forgo default speaker output.