-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
74 lines (65 loc) · 2.83 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blow Detection</title>
<style>
#indicator {
width: 50px;
height: 50px;
background-color: blue;
margin: 50px auto;
transition: width 0.2s;
}
</style>
</head>
<body>
<div id="indicator"></div>
<script>
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(function (stream) {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const analyser = audioContext.createAnalyser();
const microphone = audioContext.createMediaStreamSource(stream);
const biquadFilter = audioContext.createBiquadFilter();
// Configure the band-pass filter
biquadFilter.type = "bandpass";
biquadFilter.frequency.value = 1000; // Center frequency in Hz
biquadFilter.Q.value = 10; // Quality factor
// Connect the nodes: microphone -> biquadFilter -> analyser -> destination
microphone.connect(biquadFilter);
biquadFilter.connect(analyser);
analyser.fftSize = 256;
function isBlowing() {
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteFrequencyData(dataArray);
let sum = 0;
for (let i = 0; i < bufferLength; i++) {
sum += dataArray[i];
}
const average = sum / bufferLength;
return average;
}
function adjustWidth() {
const indicator = document.getElementById('indicator');
const averageVolume = isBlowing();
// Change the width based on the volume
console.log(averageVolume);
const newWidth = Math.min(averageVolume * 2, 300); // Adjust the multiplier and max width as needed
indicator.style.width = `${newWidth}px`;
}
setInterval(adjustWidth, 200); // Check every 200ms
})
.catch(function (err) {
console.log('Unable to access microphone: ' + err);
});
} else {
console.log('getUserMedia not supported on your browser!');
}
</script>
</body>
</html>