-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.html
200 lines (185 loc) · 6.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<script src="node_modules/@picovoice/leopard-web/dist/iife/index.js"></script>
<script src="node_modules/@picovoice/web-voice-processor/dist/iife/index.js"></script>
<script src="models/leopardModel.js"></script>
<script type="application/javascript">
let leopard = null;
window.onload = function () {
const audioContext = new (window.AudioContext || window.webKitAudioContext)(
{sampleRate: 16000}
);
function readAudioFile(selectedFile, callback) {
let reader = new FileReader();
reader.onload = function (ev) {
let wavBytes = reader.result;
audioContext.decodeAudioData(wavBytes, callback);
};
reader.readAsArrayBuffer(selectedFile);
}
const fileSelector = document.getElementById("audioFile");
fileSelector.addEventListener("change", (event) => {
writeMessage("Loading audio file...");
const fileList = event.target.files;
readAudioFile(fileList[0], async (audioBuffer) => {
const f32PCM = audioBuffer.getChannelData(0);
const i16PCM = new Int16Array(f32PCM.length);
const INT16_MAX = 32767;
const INT16_MIN = -32768;
i16PCM.set(
f32PCM.map((f) => {
let i = Math.trunc(f * INT16_MAX);
if (f > INT16_MAX) i = INT16_MAX;
if (f < INT16_MIN) i = INT16_MIN;
return i;
})
);
writeMessage("Transcribing audio file...");
const {transcript, words} = await leopard.process(i16PCM, {transfer: true});
document.getElementById("result").innerHTML = transcript;
setWordTable(words);
writeMessage("Transcribing audio file... done!");
});
});
const displayTimer = document.getElementById("displayTimer");
const recordButton = document.getElementById("recordAudio");
const stopRecord = document.getElementById("stopRecord");
let timer = null;
let currentTimer = 0.0;
let audioData = [];
const recorderEngine = {
onmessage: (event) => {
switch (event.data.command) {
case "process":
audioData.push(event.data.inputFrame);
break;
}
}
}
recordButton.addEventListener("click", async () => {
displayTimer.style.display = "inline";
stopRecord.style.display = "inline";
recordButton.style.display = "none";
currentTimer = 0.0;
audioData = [];
try {
writeMessage("Recording audio...");
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe(recorderEngine);
timer = setInterval(() => {
currentTimer += 0.1;
displayTimer.innerText = `${currentTimer.toFixed(1)} / 120`;
if (currentTimer === 120) {
stopRecord.click();
}
}, 100);
} catch (e) {
writeMessage(e);
}
});
stopRecord.addEventListener("click", async () => {
displayTimer.style.display = "none";
stopRecord.style.display = "none";
recordButton.style.display = "inline";
await window.WebVoiceProcessor.WebVoiceProcessor.unsubscribe(recorderEngine);
clearInterval(timer);
const frames = new Int16Array(audioData.length * 512);
for (let i = 0; i < audioData.length; i++) {
frames.set(audioData[i], i * 512);
}
writeMessage("Transcribing audio file...");
const {transcript, words} = await leopard.process(frames, {transfer: true});
document.getElementById("result").innerHTML = transcript;
setWordTable(words);
writeMessage("Transcribing audio file... done!");
});
}
function writeMessage(message) {
console.log(message);
document.getElementById("status").innerHTML = message;
}
function setWordTable(words) {
let html = `
<tr>
<th>word</th>
<th>startSec</th>
<th>endSec</th>
<th>confidence</th>
<th>speaker tag</th>
</tr>
`;
words.forEach(obj => {
html += `
<tr>
<td>${obj.word}</td>
<td>${obj.startSec.toFixed(3)}</td>
<td>${obj.endSec.toFixed(3)}</td>
<td>${obj.confidence.toFixed(3)}</td>
<td>${obj.speakerTag}</td>
</tr>
`
});
document.getElementById("words").innerHTML = html;
}
async function startLeopard(accessKey) {
writeMessage("Leopard is loading. Please wait...");
try {
leopard = await LeopardWeb.LeopardWorker.create(
accessKey,
leopardModel,
{
enableAutomaticPunctuation: true,
enableDiarization: true
}
);
document.getElementById("control").style.display = "block";
writeMessage("Leopard worker ready!");
} catch (err) {
writeMessage(err);
}
}
</script>
</head>
<body>
<h1>Leopard Web Demo</h1>
<p>This demo uses Leopard for Web to:</p>
<ol>
<li>
Create an instance of Leopard with the default language model.
</li>
<li>
Process/transcribe an audio file or a recorded audio stream in browser: the data does not leave the browser
while Leopard processes the audio data.
</li>
</ol>
After entering the AccessKey, click the "Start Leopard" button.
<hr/>
<label for="accessKey"
>AccessKey obtained from
<a href="https://console.picovoice.ai/">Picovoice Console</a>:</label
>
<input type="text" id="accessKey" name="accessKey"/>
<input
type="button"
id="submit"
value="Start Leopard"
onclick="startLeopard(document.getElementById('accessKey').value)"
/>
<hr/>
<div id="control" style="display: none">
<label for="audioFile">Choose audio file to transcribe:</label>
<input type="file" id="audioFile" name="audioFile"/>
<p><b>OR</b></p>
<label for="recordAudio">Record audio to transcribe (up to 2 minutes):</label>
<button id="recordAudio">Record Audio</button>
<span id="displayTimer" style="display: none;"></span>
<button id="stopRecord" style="display: none;">Stop Recording</button>
<hr/>
</div>
<div id="status" style="white-space: pre;"></div>
<br>
<div id="result"></div>
<br>
<table id="words"></table>
</body>
</html>