File size: 2,515 Bytes
70eeaf7 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voice Assistant</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#transcription {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
height: 150px;
overflow-y: auto;
}
#audio-player {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Voice Assistant</h1>
<button id="start-btn">Start Recording</button>
<button id="stop-btn" disabled>Stop Recording</button>
<div id="transcription"></div>
<audio id="audio-player" controls></audio>
<script>
const startBtn = document.getElementById('start-btn');
const stopBtn = document.getElementById('stop-btn');
const transcriptionDiv = document.getElementById('transcription');
const audioPlayer = document.getElementById('audio-player');
let websocket;
let mediaRecorder;
let audioChunks = [];
startBtn.addEventListener('click', async () => {
startBtn.disabled = true;
stopBtn.disabled = false;
websocket = new WebSocket('ws://localhost:8000/ws');
websocket.binaryType = 'arraybuffer';
websocket.onmessage = (event) => {
if (event.data instanceof ArrayBuffer) {
const audioBlob = new Blob([event.data], { type: 'audio/wav' });
audioPlayer.src = URL.createObjectURL(audioBlob);
audioPlayer.play();
} else {
transcriptionDiv.innerText += event.data + '\n';
}
};
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
audioChunks.push(event.data);
websocket.send(event.data);
}
};
mediaRecorder.start(1000); // Send audio data every second
});
stopBtn.addEventListener('click', () => {
startBtn.disabled = false;
stopBtn.disabled = true;
mediaRecorder.stop();
websocket.close();
});
</script>
</body>
</html> |