Created
October 23, 2017 12:30
-
-
Save TimonLukas/4661af75de016ec3e14ee5668c374d5a to your computer and use it in GitHub Desktop.
A wrapper around the native webkit speech recognition, giving better and faster results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const recognition = new webkitSpeechRecognition(); // eslint-disable-line | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.lang = 'en-US'; | |
recognition.onstop = () => { | |
console.log('Stopping recognition'); | |
}; | |
export default (resetCallback, callback) => { | |
console.log('Starting voice recognition'); | |
recognition.onerror = resetCallback; | |
recognition.abort(); | |
recognition.start(); | |
let timeout = 0; | |
recognition.onresult = (result) => { | |
const { transcript } = result.results[0][0]; | |
clearTimeout(timeout); | |
timeout = setTimeout(() => { | |
recognition.abort(); | |
console.log('Voice recognition stopped'); | |
callback({ | |
transcript, | |
ended: true, | |
}); | |
}, 1000); | |
callback({ | |
transcript, | |
ended: false, | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment