如何增加webrtc中的麦克风增益

时间:2016-08-10 12:09:09

标签: android performance webrtc

我有webrtc应用程序,它运行在Android手机(护士)和平板电脑(患者)上。基本上我已经实现了一个远程辅助系统,通过该系统,护士可以进行webrtc呼叫和听力(只有音频流),这是在患者方面进行的。平板电脑安装在墙上,病床通常距离很远。我想在webrtc / android端增加麦克风增益,这样我甚至可以听到低音声。我试图在谷歌上找到线索,但无法找到任何有趣的东西。有人可以告诉我如何提高麦克风的灵敏度吗?

------------------------示例代码---------------------

 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;
};

在调试中我发现了以前和新音轨的区别: 这是我从localstream获得的原创音轨:

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

这是一个新的音轨,它获取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"

ID和标签是否重要?

1 个答案:

答案 0 :(得分:2)

我不知道是否可以控制实际的硬件麦克风,但您可以通过使用WebAudio处理它来增加输入信号的增益。

尝试此操作(在Chrome中使用https fiddle):

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>

某些浏览器(Firefox)也有mozAutoGainControl您可以尝试打开。