shibly100 commited on
Commit
1ff9fd2
·
verified ·
1 Parent(s): 7c0e199

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +106 -0
script.js ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
2
+ let recognition = null;
3
+ let isRecording = false;
4
+ let lastTimestamp = null;
5
+
6
+ const languageSelect = document.getElementById('languageSelect');
7
+ const toggleButton = document.getElementById('toggleButton');
8
+ const transcriptionOutput = document.getElementById('transcriptionOutput');
9
+ const statusMessage = document.getElementById('statusMessage');
10
+ const clearButton = document.getElementById('clearButton');
11
+ const downloadButton = document.getElementById('downloadButton');
12
+
13
+ let liveBuffer = "";
14
+
15
+ // Highlight fillers
16
+ function highlightDisfluencies(text) {
17
+ return text.replace(/\b(um+|uh+|er+|ah+|eh+|oh+|hmm+|mmm+)\b/gi,
18
+ (m) => `<span class="disfluency">${m}</span>`);
19
+ }
20
+
21
+ // ============= Live Mic ============= //
22
+ if (!SpeechRecognition) {
23
+ statusMessage.innerText = "❌ Web Speech API not supported. Use Chrome/Edge desktop.";
24
+ toggleButton.disabled = true;
25
+ } else {
26
+ initRecognition(languageSelect.value);
27
+ }
28
+
29
+ function initRecognition(lang) {
30
+ recognition = new SpeechRecognition();
31
+ recognition.lang = lang;
32
+ recognition.continuous = true;
33
+ recognition.interimResults = true;
34
+
35
+ recognition.onstart = () => {
36
+ statusMessage.innerText = `🎙️ Listening… (${lang})`;
37
+ };
38
+
39
+ recognition.onerror = (e) => {
40
+ statusMessage.innerText = `⚠️ Error: ${e.error}`;
41
+ };
42
+
43
+ recognition.onend = () => {
44
+ if (isRecording) recognition.start(); // auto restart
45
+ };
46
+
47
+ recognition.onresult = (event) => {
48
+ let interim = "";
49
+ let final = "";
50
+
51
+ for (let i = event.resultIndex; i < event.results.length; i++) {
52
+ const text = event.results[i][0].transcript.trim();
53
+
54
+ if (event.results[i].isFinal) {
55
+ final += text + " ";
56
+ } else {
57
+ interim += text + " ";
58
+ }
59
+ }
60
+
61
+ if (interim) {
62
+ liveBuffer = interim;
63
+ statusMessage.innerText = "🎙️ Listening (interim)...";
64
+ }
65
+
66
+ if (final) {
67
+ const now = Date.now();
68
+ let prefix = " ";
69
+ if (lastTimestamp && now - lastTimestamp > 3000) prefix = "\n... ";
70
+
71
+ transcriptionOutput.innerHTML += prefix + highlightDisfluencies(final);
72
+ transcriptionOutput.scrollTop = transcriptionOutput.scrollHeight;
73
+
74
+ lastTimestamp = now;
75
+ liveBuffer = "";
76
+ }
77
+ };
78
+ }
79
+
80
+ toggleButton.addEventListener("click", () => {
81
+ if (isRecording) {
82
+ isRecording = false;
83
+ recognition.stop();
84
+ toggleButton.textContent = "🎤 Start Listening";
85
+ } else {
86
+ initRecognition(languageSelect.value);
87
+ recognition.start();
88
+ isRecording = true;
89
+ toggleButton.textContent = "⏹ Stop Listening";
90
+ }
91
+ });
92
+
93
+ clearButton.addEventListener("click", () => {
94
+ transcriptionOutput.innerHTML = "";
95
+ statusMessage.innerText = "Cleared.";
96
+ });
97
+
98
+ downloadButton.addEventListener("click", () => {
99
+ const text = transcriptionOutput.innerText;
100
+ const blob = new Blob([text], { type: "text/plain" });
101
+ const url = URL.createObjectURL(blob);
102
+ const a = document.createElement("a");
103
+ a.href = url;
104
+ a.download = "transcription.txt";
105
+ a.click();
106
+ });