-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
221 lines (200 loc) · 8.5 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Skynet - Streaming Whisper Demo</title>
<link rel="stylesheet" href="bulma.min.css">
</head>
<body>
<section class="section">
<div class="container is-max-desktop has-text-centered">
<h1 class="title">Streaming Whisper Demo</h1>
</div>
</section>
<section class="section is-small">
<div class="container is-max-desktop">
<div class="box">
<div class="field">
<div class="control">
<div class="field">
<button class="button is-primary" id="transcribebtn">Transcribe</button>
<button class="button" id="stopbtn" disabled>Stop</button>
<div class="select is-disabled">
<select id="langselector" name="langselector">
<option value="en" selected="selected">English</option>
</select>
</div>
<div class="button" id="mutebtn">Mute</div>
</div>
<div class="field">
<input type="text"
class="input"
id="wshost"
placeholder="ws://localhost:8000/streaming-whisper/ws"
value="ws://localhost:8000/streaming-whisper/ws"
>
</div>
<div class="field">
<input type="text" id="jwt" class="input" placeholder="some JWT if using auth">
</div>
</div>
</div>
</div>
</div>
</section>
<section class="section">
<div class="container is-max-desktop">
<div class="box has-text-centered is-family-monospace has-text-grey" id="outputcontainer">
<p>Waiting for interims...</p>
</div>
</div>
</section>
<section class="section is-small">
<div class="container is-max-desktop" id="finalcontainer">
</div>
</section>
<audio controls="controls" id="aud" style="display: none"><source src="data:audio/wav;base64," type="audio/wav"></audio>
<script>
let orderedFinals = []
function setAndPlay(idx) {
var audio = document.getElementById('aud')
audio.src = 'data:audio/wav;base64,' + orderedFinals[idx].audio
audio.play()
}
// mostly taken from https://dev.to/louisgv/quick-guide-to-audioworklet-30df
const main = async () => {
let finals = []
const CLIENTID = crypto.randomUUID()
const MEETINGID = crypto.randomUUID()
const context = new AudioContext({ sampleRate: 16000 })
const microphone = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false
})
const source = context.createMediaStreamSource(microphone)
// load the worklet
await context.audioWorklet.addModule('recorder.worklet.js')
let ws = undefined
const langSel = document.getElementById('langselector')
const transcribeBtn = document.getElementById('transcribebtn')
const stopBtn = document.getElementById('stopbtn')
const output = document.getElementById('outputcontainer')
const final = document.getElementById('finalcontainer')
const jwt = document.getElementById('jwt')
const wsHost = document.getElementById('wshost')
const muteBtn = document.getElementById('mutebtn')
let isMuted = false
muteBtn.addEventListener('click', () => {
if (isMuted) {
microphone.getAudioTracks()[0].enabled = true
isMuted = false
muteBtn.innerHTML = 'Mute'
} else {
microphone.getAudioTracks()[0].enabled = false
isMuted = true
muteBtn.innerHTML = 'Unmute'
}
})
var isSpeaking = false
// create the recorder worklet
const recorder = new AudioWorkletNode(context, 'recorder.worklet')
source.connect(recorder).connect(context.destination)
function renderFinals() {
final.innerHTML = ''
orderedFinals = finals.sort((a, b) => a.ts - b.ts)
playerHidden = true
for (let [index, msg] of orderedFinals.entries()) {
d = new Date(msg.ts)
h = d.getHours()
m = d.getMinutes()
s = d.getSeconds()
ms = d.getMilliseconds()
playButton = ''
if (msg.audio !== '') {
playButton = ' <button class="button is-small" onclick="setAndPlay(' + index + ')">►</button>'
}
final.innerHTML += '<div class="columns">' +
'<div class="column"><span class="tag is-info is-light is-family-monospace">' +
h + ':' + m + ':' + s + '.' + ms + '</span>' + playButton + '</div>' +
'<div class="column is-three-fifths"><span class="transcript">' + msg.text + '</span>' +
'</div></div>'
}
}
function convertFloat32To16BitPCM(input) {
const output = new Int16Array(input.length)
for (let i = 0; i < input.length; i++) {
const s = Math.max(-1, Math.min(1, input[i]))
output[i] = s < 0 ? s * 0x8000 : s * 0x7fff
}
return output
}
// events
function setupWsEvents() {
ws.onmessage = (e) => {
let msg = JSON.parse(e.data)
if (msg.type === 'interim') {
output.innerHTML = '<p>' + msg.text + '</p>'
} else {
finals.push(msg)
renderFinals()
}
}
}
function wsConnect() {
let wsConnectionString = wsHost.value.trim() + '/' + MEETINGID
if (jwt.value.trim() != '') {
wsConnectionString += '?auth_token=' + jwt.value.trim()
}
ws = new WebSocket(wsConnectionString)
ws.binaryType = 'blob'
setupWsEvents()
}
function wsDisconnect() {
if (ws != undefined) {
ws.close()
}
}
function preparePayload(data) {
let lang = langSel.value
let str = CLIENTID + "|" + lang
if (str.length < 60) {
str = str.padEnd(60, " ")
}
let utf8Encode = new TextEncoder()
let buffer = utf8Encode.encode(str)
let headerArr = new Uint16Array(buffer.buffer)
const payload = []
headerArr.forEach(i => payload.push(i))
data.forEach(i => payload.push(i))
return Uint16Array.from(payload)
}
transcribeBtn.addEventListener("click", () => {
context.resume()
isSpeaking = true
transcribeBtn.disabled = true
stopBtn.disabled = false
langSel.disabled = true
wsConnect()
});
stopBtn.addEventListener('click', () => {
context.suspend()
isSpeaking = false
stopBtn.disabled = true;
transcribeBtn.disabled = false;
langSel.disabled = false
wsDisconnect()
});
recorder.port.onmessage = (e) => {
if (ws != undefined && isSpeaking) {
const audio = convertFloat32To16BitPCM(e.data)
const payload = preparePayload(audio)
ws.send(payload)
}
}
}
main()
</script>
</body>
</html>