+
80
-

js如何抑制清除从麦克风捕获的声音产生的回音?

请问js如何抑制清除从麦克风捕获的声音产生的回音?

网友回复

+
0
-

可以在获取的时候将echoCancellation设为true

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

完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <style>
        body {
            background: #000;
        }
    </style>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/siriwave.umd.js"></script>
    <body>
   不要在iframe中打开
        <div id="container" style="width: 900px; height: 300px; background: #000"></div>
        <script>

            var SW = new SiriWave({
                style: 'ios9',
                speed: 0.1,
                amplitude: 0.2,
                speedInterpolationSpeed: 0,
                frequency: 2,
                height: 424,
                width: 964,
                container: document.getElementById('container'),
                autostart: true
            });




            function getAverageVolume(data) {
                var values = 0;
                var length = data.length;
                for (var i = 0; i < data.length; i++) {
                    values += data[i];
                }
                return values / data.length;
            }
            window.navigator.getUserMedia(
                {
                    audio: {echoCancellation: true}
                },

 
                function(stream) {
                    var audioContext = new (window.AudioContext || window.webkitAudioContext)();
                    var analyser = audioContext.createAnalyser();
                    analyser.connect(audioContext.destination); //这一行表示将麦克风声音直接输出,注意没有做回声清除
                    analyser.smoothingTimeConstant = 0.8;
                    analyser.fftSize = 2048;

                    var bufferLength = analyser.frequencyBinCount;
                    var microphone = audioContext.createMediaStreamSource(stream);

                    microphone.connect(analyser);
                    var processor = audioContext.createScriptProcessor(2048, 1, 1);
                    processor.connect(audioContext.destination);

                    processor.onaudioprocess = function() {
                        var array = new Uint8Array(analyser.frequencyBinCount);
                        analyser.getByteFrequencyData(array);
                        var average = getAverageVolume(array);
                        SW.setAmplitude(average / 140);
                    };
                },
                function() {
                    console.log(1)
                }
            );

        </script>
    </body>
</html>

我知道答案,我要回答