可以在获取的时候将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>
网友回复
如何将linux服务器的文件目录映射到windows电脑磁盘?
Docling 与 MarkItDown 两个库有啥不同?
豆包收费后国产其他ai软件也会跟进收费吗?
JPEG 与 HEIF图片格式区别?
centos7版本太旧无法安装python3.11,如何在docker中运行python3.11?
python如何做个RPA按键精灵的程序?
写一个windows的cmd的python代码如何在命令行中捕获获取复制粘贴的图片?
如何将别人爆款的抖音短视频短剧文案提取为seedance2的提示词?
阿里云域名dns云解析10万次日限额如何应对?
windows电脑如何提交上架ipa苹果应用?


