<html>
<head>
<script src="https://cdn.jsdelivr.net/gh/msqr1/[email protected]/examples/Vosklet.min.js" async defer></script>
<script>
async function start() {
// Make sure sample rate matches that in the training data
let ctx = new AudioContext({sampleRate : 16000})
// Setup mic with correct sample rate
let micNode = ctx.createMediaStreamSource(await navigator.mediaDevices.getUserMedia({
video: false,
audio: {
echoCancellation: true,
noiseSuppression: true,
channelCount: 1,
sampleRate: 16000
},
}))
// Load Vosklet module, model and recognizer
let module = await loadVosklet()
let model = await module.createModel("https://raw.githubusercontent.com/msqr1/Vosklet/main/examples/en-model.tgz","model","ID")
let recognizer = await module.createRecognizer(model, 16000)
// Listen for result and partial result
recognizer.addEventListener("result", ev => {
console.log("Result: ", ev.detail)
})
recognizer.addEventListener("partialResult", ev => {
console.log("Partial result: ", ev.detail)
})
// Create a transferer node to get audio data on the main thread
let transferer = await module.createTransferer(ctx, 128 * 150)
// Recognize data on arrival
transferer.port.onmessage = ev => {
recognizer.acceptWaveform(ev.data)
}
// Connect to microphone
micNode.connect(transferer)
}
</script>
<!-- Start and create audio context only as a result of user's action -->
<button onclick="start()">Start</button>
</head>
</html>