Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mainSection = document.getElementById("main-section");
- const awakeSound = new Audio('assets/styles/awake.mp3');
- const sleepSound = new Audio('assets/styles/sleep.mp3');
- const assistantPrimaryElement = document.getElementById('assistant-primary');
- const introSection = document.getElementById('intro');
- const clickTooltip = document.getElementById('click-tooltip');
- const wakeTooltip = document.getElementById('wakeup-tooltip');
- if ('webkitSpeechRecognition' in window) {
- // always assistant - check for wake word \/
- const RECOGNITION_ALWAYS = new webkitSpeechRecognition();
- RECOGNITION_ALWAYS.lang = 'en-US';
- RECOGNITION_ALWAYS.interimResults = true;
- RECOGNITION_ALWAYS.continuous = true;
- let autoRestart = true;
- let introPassed = false;
- const WakeWord = 'harry';
- // check if the wake word is present
- function checkWakeWord(transcript) {
- if (transcript.toLowerCase().includes(WakeWord)) {
- if (!introPassed) {
- introScreen();
- } else {
- startPrimaryRecog();
- }
- console.log('intro passed: ' + introPassed);
- }
- }
- function introScreen() {
- introSection.style.opacity = '0';
- setTimeout(() => {
- introSection.style.display = 'none';
- mainSection.style.opacity = '1';
- setTimeout(() => {
- startPrimaryRecog();
- introPassed = true;
- }, 1000)
- }, 700)
- }
- RECOGNITION_ALWAYS.onresult = (event) => {
- for (let i = event.resultIndex; i < event.results.length; i++) {
- const transcript = event.results[i][0].transcript;
- checkWakeWord(transcript);
- // console.log(transcript);
- } // check if wake word is in transcript
- };
- RECOGNITION_ALWAYS.onerror = (event) => {
- if (event.error !== 'no-speech') {
- // console.error('always Speech recognition error:', event.error);
- }
- if (autoRestart) {
- RECOGNITION_ALWAYS.start();
- // console.log('always recog stopped - restarting.');
- }
- };
- RECOGNITION_ALWAYS.onend = () => {
- console.log("recognition always stopped.");
- if (autoRestart) {
- RECOGNITION_ALWAYS.start();
- console.log('always recog stopped - restarting.');
- }
- };
- // -----------------------------------------------------------------------------------
- // awake assistant \/
- const PRIMARY_RECOGNITION = new webkitSpeechRecognition();
- PRIMARY_RECOGNITION.lang = 'en-US';
- PRIMARY_RECOGNITION.interimResults = true;
- PRIMARY_RECOGNITION.continuous = true;
- const PRIMARY_ASSISTANT_RESULT = document.getElementById('result');
- let finalTranscript = '';
- let silenceTimeout;
- const stopWord = 'harry stop';
- const wallOnWord = 'turn on the lights'
- const wallOffWord = 'turn off the lights'
- function checkKeyWord(transcript) {
- if (transcript.toLowerCase().includes(stopWord)) {
- PRIMARY_RECOGNITION.stop();
- console.log('stop word detected - starting wake word recognition');
- }
- if (transcript.toLowerCase().includes(wallOnWord)) {
- turnOnWall();
- PRIMARY_RECOGNITION.stop();
- }
- if (transcript.toLowerCase().includes(wallOffWord)) {
- turnOffWall();
- PRIMARY_RECOGNITION.stop();
- }
- }
- PRIMARY_RECOGNITION.onresult = (event) => {
- let interimTranscript = '';
- for (let i = event.resultIndex; i < event.results.length; i++) {
- const transcript = event.results[i][0].transcript;
- if (event.results[i].isFinal) {
- finalTranscript += transcript;
- } else {
- interimTranscript += transcript; // append interim result
- }
- }
- PRIMARY_ASSISTANT_RESULT.textContent = finalTranscript + ' ' + interimTranscript;
- checkKeyWord(interimTranscript);
- };
- PRIMARY_RECOGNITION.onerror = (event) => {
- console.error('primary Speech recognition error:', event.error);
- };
- PRIMARY_RECOGNITION.onend = () => {
- setTimeout(stopPrimaryRecog, 500);
- };
- function startPrimaryRecog() {
- autoRestart = false;
- RECOGNITION_ALWAYS.stop();
- awakeSound.play();
- mainSection.style.setProperty('--border-opacity', '1');
- assistantPrimaryElement.style.opacity = '1';
- PRIMARY_RECOGNITION.start(); // Start the primary recognition
- // console.log('wake word detected - starting primary recognition');
- }
- function stopPrimaryRecog() {
- mainSection.style.setProperty('--border-opacity', '0')
- assistantPrimaryElement.style.opacity = '0';
- sleepSound.play();
- autoRestart = true;
- PRIMARY_RECOGNITION.stop();
- RECOGNITION_ALWAYS.start();
- // console.log("primary recognition stopped");
- PRIMARY_ASSISTANT_RESULT.textContent = '';
- finalTranscript = '';
- }
- // mainSection.addEventListener('click', () => {
- // RECOGNITION_ALWAYS.start();
- // });
- introSection.addEventListener('click', function() {
- clickTooltip.style.opacity = '0';
- setTimeout(() => {
- clickTooltip.style.display = 'none';
- wakeTooltip.style.display = 'flex';
- setTimeout(() => {
- wakeTooltip.style.opacity = '1';
- RECOGNITION_ALWAYS.start();
- }, 200);
- }, 700);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement