forked from UIC-LIT/PATHWiSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp_test.html
292 lines (247 loc) · 9.84 KB
/
nlp_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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!DOCTYPE html>
<html>
<head>
<title>NLP Server Test Environment</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.controls {
margin: 20px 0;
}
button {
padding: 10px 20px;
margin-right: 10px;
font-size: 16px;
cursor: pointer;
}
button:disabled {
cursor: not-allowed;
opacity: 0.6;
}
.status-box {
padding: 10px;
margin: 10px 0;
border-radius: 4px;
background-color: #f0f0f0;
}
#calibration-status {
display: none;
background-color: #fff3cd;
border: 1px solid #ffeeba;
padding: 10px;
margin: 10px 0;
}
#silence-alert {
display: none;
background-color: #d4edda;
border: 1px solid #c3e6cb;
padding: 10px;
margin: 10px 0;
}
#recognized-text {
min-height: 100px;
max-height: 300px;
overflow-y: auto;
padding: 10px;
border: 1px solid #ddd;
margin: 10px 0;
white-space: pre-wrap;
}
.db-meter {
width: 100%;
height: 20px;
background-color: #eee;
margin: 10px 0;
position: relative;
}
.db-level {
height: 100%;
width: 0%;
background-color: #4CAF50;
transition: width 0.1s ease;
}
.error {
color: #721c24;
background-color: #f8d7da;
border: 1px solid #f5c6cb;
padding: 10px;
margin: 10px 0;
display: none;
}
</style>
</head>
<body>
<h1>NLP Server Test Environment</h1>
<div id="calibration-status" class="status-box">
Calibrating background noise... Please remain quiet.
<div class="countdown"></div>
</div>
<div class="controls">
<button id="startBtn" disabled>Start Recording</button>
<button id="stopBtn" disabled>Stop Recording</button>
</div>
<div id="status" class="status-box">
WebSocket Status: <span id="ws-status">Connecting...</span>
</div>
<div id="audio-levels">
<p>Audio Levels:</p>
<div class="db-meter">
<div class="db-level"></div>
</div>
<div id="db-value">Current: 0 dB</div>
<div id="threshold-value">Threshold: N/A</div>
</div>
<div id="state" class="status-box">
Current State: <span id="recording-state">Idle</span>
</div>
<div id="transcript">
<h3>Transcript:</h3>
<div id="recognized-text"></div>
</div>
<div id="silence-alert">
Silence Detected!
</div>
<div id="error" class="error"></div>
<script>
let socket;
let isRecording = false;
let mediaStream = null;
let audioContext = null;
let processor = null;
let isCalibrating = false;
let silenceThreshold = null;
// Initialize WebSocket connection
function connect() {
socket = new WebSocket('ws://localhost:8765');
socket.onopen = () => {
document.getElementById('ws-status').textContent = 'Connected';
document.getElementById('startBtn').disabled = false;
console.log('WebSocket connected');
};
socket.onclose = () => {
document.getElementById('ws-status').textContent = 'Disconnected';
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = true;
console.log('WebSocket disconnected');
setTimeout(connect, 5000);
};
socket.onmessage = handleMessage;
}
function handleMessage(event) {
const data = JSON.parse(event.data);
console.log('Received:', data);
switch(data.type) {
case 'calibration_start':
isCalibrating = true;
document.getElementById('calibration-status').style.display = 'block';
document.getElementById('recording-state').textContent = 'Calibrating';
startCalibrationCountdown(3); // 3 seconds countdown
break;
case 'calibration_complete':
isCalibrating = false;
document.getElementById('calibration-status').style.display = 'none';
document.getElementById('recording-state').textContent = 'Ready';
document.getElementById('threshold-value').textContent =
`Threshold: ${data.threshold_db.toFixed(2)} dB`;
silenceThreshold = data.threshold_db;
break;
case 'speech_recognized':
if (data.result && data.result.text) {
const transcriptDiv = document.getElementById('recognized-text');
transcriptDiv.textContent += data.result.text + '\n';
transcriptDiv.scrollTop = transcriptDiv.scrollHeight;
}
break;
case 'silence_detected':
const silenceAlert = document.getElementById('silence-alert');
silenceAlert.style.display = 'block';
setTimeout(() => {
silenceAlert.style.display = 'none';
}, 2000);
break;
}
}
function startCalibrationCountdown(seconds) {
const countdownDiv = document.querySelector('#calibration-status .countdown');
let timeLeft = seconds;
const countdown = setInterval(() => {
countdownDiv.textContent = `${timeLeft} seconds remaining...`;
timeLeft--;
if (timeLeft < 0) {
clearInterval(countdown);
countdownDiv.textContent = '';
}
}, 1000);
}
async function startRecording() {
try {
mediaStream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true
}
});
audioContext = new AudioContext({ sampleRate: 16000 });
const source = audioContext.createMediaStreamSource(mediaStream);
processor = audioContext.createScriptProcessor(4096, 1, 1);
source.connect(processor);
processor.connect(audioContext.destination);
processor.onaudioprocess = (e) => {
const inputData = e.inputBuffer.getChannelData(0);
const pcmData = new Int16Array(inputData.length);
for (let i = 0; i < inputData.length; i++) {
pcmData[i] = Math.max(-1, Math.min(1, inputData[i])) * 0x7FFF;
}
// Calculate and display current dB level
const rms = Math.sqrt(inputData.reduce((sum, x) => sum + x * x, 0) / inputData.length);
const db = 20 * Math.log10(Math.max(rms, 1e-10));
updateDBMeter(db);
if (socket.readyState === WebSocket.OPEN) {
const base64data = btoa(String.fromCharCode(...new Uint8Array(pcmData.buffer)));
socket.send(JSON.stringify({
type: 'audio_data',
audio: base64data
}));
}
};
isRecording = true;
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
document.getElementById('recording-state').textContent = isCalibrating ? 'Calibrating' : 'Recording';
} catch (error) {
console.error('Error starting recording:', error);
document.getElementById('error').textContent = 'Error starting recording: ' + error.message;
document.getElementById('error').style.display = 'block';
}
}
function stopRecording() {
if (mediaStream) {
mediaStream.getTracks().forEach(track => track.stop());
}
if (audioContext) {
audioContext.close();
}
isRecording = false;
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
document.getElementById('recording-state').textContent = 'Idle';
}
function updateDBMeter(db) {
// Normalize dB value for meter display (-60dB to 0dB range)
const normalizedDb = Math.max(0, Math.min(100, (db + 60) * (100/60)));
document.querySelector('.db-level').style.width = `${normalizedDb}%`;
document.getElementById('db-value').textContent = `Current: ${db.toFixed(2)} dB`;
}
// Event listeners
document.getElementById('startBtn').addEventListener('click', startRecording);
document.getElementById('stopBtn').addEventListener('click', stopRecording);
// Initialize connection
connect();
</script>
</body>
</html>