4

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 ?

  • Dunno if the actual hardware mic can be controlled, but you can increase the gain of the input signal by [processing it in webaudio](http://stackoverflow.com/a/35000726/918910). Some browsers also have [autoGainControl](http://stackoverflow.com/a/37332145/918910) that you could try turning on. The only trick would might be getting the tracks back into streams in Chrome which doesn't support track manipulation yet AFAIK. – jib Aug 10 '16 at 12:57
  • Is it possible to use something else instead of using MediaStream? basically i am trying to using this in a mobile via corodova plugin and chrome version is currently showing 44 which dont support MediaStream. So that getting Uncaught ReferenceError: MediaStream is not defined online var src = ctx.createMediaStreamSource(new MediaStream([audioTrack])); –  Aug 10 '16 at 14:42
  • You're audio-only, so I think you can skip the separating-and-putting-back-together step. Try `ctx.createMediaStreamSource(stream)` and then `ctx.createMediaStreamDestination()` returns a media stream already which you can use directly I would think. – jib Aug 10 '16 at 17:40
  • Well something fishy going on i unable to get any audio in remote side. i update my question with code and audio track object which i found in debug. Please have a look and let me know why i unable to get audio in remote side. –  Aug 10 '16 at 17:54
  • Make sure the audio is playing over the loudspeaker, by default webrtc's audio plays over the earspeaker like when you call someone on the phone. – Kevin Aug 11 '16 at 08:50

1 Answers1

4

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.

Community
  • 1
  • 1
jib
  • 40,579
  • 17
  • 100
  • 158
  • I have updated my question Can you please tell me why i not getting any audio if i call modifyGain method? –  Aug 10 '16 at 18:03
  • [Wfm](https://jsfiddle.net/jib1/z9gybrz3/). Try to isolate the problem by checking if it works locally first. – jib Aug 10 '16 at 21:40
  • I have tested this on my local browser and it works fine but unfortunately this feature are not be a part of old browser (chrome version 43) so that it not working on cordova plugin on android devices. I need to wait when they upgrade chrome version in cordova API. –  Aug 11 '16 at 09:42