-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
153 lines (144 loc) · 3.95 KB
/
index.html
File metadata and controls
153 lines (144 loc) · 3.95 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Sound Level Meter</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #222;
color: #eee;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 1rem;
text-align: center;
}
h1 {
color: #4caf50;
margin-bottom: 0.5rem;
}
#instructions {
margin-bottom: 1rem;
max-width: 300px;
}
#volumeMeter {
width: 300px;
height: 40px;
background: #444;
border-radius: 8px;
overflow: hidden;
margin-bottom: 1rem;
}
#volumeLevel {
height: 100%;
width: 0%;
background: #4caf50;
transition: width 0.1s ease-out;
}
button {
background: #4caf50;
color: #222;
border: none;
border-radius: 6px;
padding: 0.75rem 1.5rem;
cursor: pointer;
font-weight: bold;
font-size: 1rem;
transition: background-color 0.3s ease;
}
button:hover {
background: #388e3c;
}
#status {
margin-top: 1rem;
font-style: italic;
color: #ccc;
}
</style>
</head>
<body>
<h1>Simple Sound Level Meter</h1>
<div id="instructions">Click "Start" and allow microphone access. The green bar shows the sound volume level in real time.</div>
<div id="volumeMeter" aria-label="Sound volume level meter">
<div id="volumeLevel"></div>
</div>
<button id="startStopBtn">Start</button>
<div id="status"></div>
<script>
let audioContext = null;
let meter = null;
let rafId = null;
let microphone = null;
const volumeLevel = document.getElementById('volumeLevel');
const status = document.getElementById('status');
const startStopBtn = document.getElementById('startStopBtn');
function startMeter() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
status.textContent = "getUserMedia is not supported by your browser.";
return;
}
status.textContent = "Requesting microphone access...";
navigator.mediaDevices.getUserMedia({audio:true})
.then(stream => {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
microphone = audioContext.createMediaStreamSource(stream);
meter = audioContext.createAnalyser();
meter.fftSize = 256;
microphone.connect(meter);
status.textContent = "Microphone access granted. Sound level active.";
startVisualizing();
})
.catch(err => {
status.textContent = "Microphone access denied or error: " + err.message;
});
}
function startVisualizing() {
if (!meter) return;
const dataArray = new Uint8Array(meter.frequencyBinCount);
function update() {
meter.getByteFrequencyData(dataArray);
// Calculate average volume
let sum = 0;
for (let i = 0; i < dataArray.length; i++) {
sum += dataArray[i];
}
const avg = sum / dataArray.length;
const percent = Math.min(100, (avg / 255) * 100);
volumeLevel.style.width = percent + '%';
rafId = requestAnimationFrame(update);
}
update();
}
function stopMeter() {
if (rafId) {
cancelAnimationFrame(rafId);
rafId = null;
}
if (microphone) {
microphone.disconnect();
microphone = null;
}
if (audioContext) {
audioContext.close();
audioContext = null;
}
volumeLevel.style.width = '0%';
status.textContent = 'Sound meter stopped.';
}
startStopBtn.addEventListener('click', () => {
if (startStopBtn.textContent === 'Start') {
startMeter();
startStopBtn.textContent = 'Stop';
} else {
stopMeter();
startStopBtn.textContent = 'Start';
}
});
</script>
</body>
</html>