<!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>
<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: 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>
网友回复