user526206
user526206

Reputation:

How to increase mic gain in webrtc

I have webrtc application which running on android phone(for Nurses) and tablet(for patients). Basically i have implement a remote assistant system by which nurses can make webrtc call and hear(only audio stream) that whats going on in patient side. As tablet are mounted on wall and patient bed are normally little bit on distance. I want to increase the mic gain either in webrtc/android side so that i can hear even a low level sound. I try to find a clue on google but unable to find anything interesting. Can someone give me hint how i would increase the microphone sensitivity ?

------------------------ sample code---------------------

 function getLocalStream(successCallback) {
    if (localStream && successCallback) {
        successCallback(localStream);
        return;
    }
    navigator.getUserMedia(streamOptions, function (str) {
        var stream= modifyGain(str);

        uiHandler("peer.call.localstream", {payload: stream});
        localStream = stream;
        if (successCallback) {
            successCallback(stream);
        }
    }, function (err) {
        uiHandler("status.error", {payload: "Failed to access local camera", error: err});
    });
}

function modifyGain (stream){
    var audioTrack = stream.getAudioTracks()[0];
    var ctx = new AudioContext();
    var src = ctx.createMediaStreamSource(stream);
    var dst = ctx.createMediaStreamDestination();
    var gainNode = ctx.createGain();
    gainNode.gain.value = 50;
    [src, gainNode, dst].reduce( function(a, b) { return a && a.connect(b) });
    stream.removeTrack(audioTrack);
    var newAudioTrack = dst.stream.getAudioTracks()[0];

    stream.addTrack(newAudioTrack);
    return stream;
};

In debug i have found the difference in previous and new audio track: This is original audio track which i get from localstream:

enabled: true
id: "8e4363c2-f1c8-44ec-9bed-f3414f3b943a"
kind: "audio"
label: "Default"
muted: false
onended: null
onmute: null
onunmute: null
readyState: "live"

This is a new audio track which get dst.stream.getAudioTracks()[0];

enabled: true
id: "cc0c4293-a606-407e-9adb-4caddaa32583"
 kind: "audio"
label: "MediaStreamAudioDestinationNode"
muted: false
onended: null
onmute: null
onunmute: null
readyState: "live"

Is id and label matter to play stream ?

Upvotes: 5

Views: 4750

Answers (1)

jib
jib

Reputation: 42480

I don't know if the actual hardware mic can be controlled, but you can increase the gain of the input signal by processing it with WebAudio.

Try this (use https fiddle in Chrome):

navigator.mediaDevices.getUserMedia({audio: true})
  .then(stream => audio.srcObject = modifyGain(stream, 2.5))
  .catch(e => console.log(e));

var modifyGain = (stream, gainValue) => {
  var ctx = new AudioContext();
  var src = ctx.createMediaStreamSource(stream);
  var dst = ctx.createMediaStreamDestination();
  var gainNode = ctx.createGain();
  gainNode.gain.value = gainValue;
  [src, gainNode, dst].reduce((a, b) => a && a.connect(b));
  return dst.stream;
};
<audio id="audio" controls autoplay></audio>

Some browsers (Firefox) also have a mozAutoGainControl you could try turning on.

Upvotes: 4

Related Questions