Advertisement
Guest User

web speech

a guest
Oct 26th, 2024
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mainSection = document.getElementById("main-section");
  2.  
  3. const awakeSound = new Audio('assets/styles/awake.mp3');
  4. const sleepSound = new Audio('assets/styles/sleep.mp3');
  5.  
  6. const assistantPrimaryElement = document.getElementById('assistant-primary');
  7.  
  8. const introSection = document.getElementById('intro');
  9. const clickTooltip = document.getElementById('click-tooltip');
  10. const wakeTooltip = document.getElementById('wakeup-tooltip');
  11.  
  12.  
  13. if ('webkitSpeechRecognition' in window) {
  14.     // always assistant - check for wake word \/
  15.     const RECOGNITION_ALWAYS = new webkitSpeechRecognition();
  16.     RECOGNITION_ALWAYS.lang = 'en-US';
  17.     RECOGNITION_ALWAYS.interimResults = true;
  18.     RECOGNITION_ALWAYS.continuous = true;
  19.  
  20.     let autoRestart = true;
  21.     let introPassed = false;
  22.  
  23.     const WakeWord = 'harry';
  24.  
  25.     // check if the wake word is present
  26.     function checkWakeWord(transcript) {
  27.         if (transcript.toLowerCase().includes(WakeWord)) {
  28.             if (!introPassed) {
  29.                 introScreen();
  30.             } else {
  31.                 startPrimaryRecog();
  32.             }
  33.             console.log('intro passed: ' + introPassed);
  34.         }
  35.     }
  36.    
  37.     function introScreen() {
  38.         introSection.style.opacity = '0';
  39.         setTimeout(() => {
  40.             introSection.style.display = 'none';
  41.             mainSection.style.opacity = '1';
  42.             setTimeout(() => {
  43.                 startPrimaryRecog();
  44.                 introPassed = true;
  45.             }, 1000)
  46.         }, 700)
  47.     }
  48.  
  49.     RECOGNITION_ALWAYS.onresult = (event) => {
  50.         for (let i = event.resultIndex; i < event.results.length; i++) {
  51.             const transcript = event.results[i][0].transcript;
  52.             checkWakeWord(transcript);
  53.             // console.log(transcript);
  54.         }  // check if wake word is in transcript
  55.     };
  56.  
  57.     RECOGNITION_ALWAYS.onerror = (event) => {
  58.         if (event.error !== 'no-speech') {
  59.             // console.error('always Speech recognition error:', event.error);
  60.         }
  61.         if (autoRestart) {
  62.             RECOGNITION_ALWAYS.start();
  63.             // console.log('always recog stopped - restarting.');
  64.         }
  65.     };
  66.  
  67.     RECOGNITION_ALWAYS.onend = () => {
  68.         console.log("recognition always stopped.");
  69.         if (autoRestart) {
  70.             RECOGNITION_ALWAYS.start();
  71.             console.log('always recog stopped - restarting.');
  72.         }
  73.     };
  74.  
  75.     //  -----------------------------------------------------------------------------------
  76.     // awake assistant \/
  77.  
  78.     const PRIMARY_RECOGNITION = new webkitSpeechRecognition();
  79.     PRIMARY_RECOGNITION.lang = 'en-US';
  80.     PRIMARY_RECOGNITION.interimResults = true;
  81.     PRIMARY_RECOGNITION.continuous = true;
  82.  
  83.     const PRIMARY_ASSISTANT_RESULT = document.getElementById('result');
  84.     let finalTranscript = '';
  85.  
  86.     let silenceTimeout;
  87.  
  88.     const stopWord = 'harry stop';
  89.     const wallOnWord = 'turn on the lights'
  90.     const wallOffWord = 'turn off the lights'
  91.  
  92.     function checkKeyWord(transcript) {
  93.         if (transcript.toLowerCase().includes(stopWord)) {
  94.             PRIMARY_RECOGNITION.stop();
  95.             console.log('stop word detected - starting wake word recognition');
  96.         }
  97.         if (transcript.toLowerCase().includes(wallOnWord)) {
  98.             turnOnWall();
  99.             PRIMARY_RECOGNITION.stop();
  100.         }
  101.         if (transcript.toLowerCase().includes(wallOffWord)) {
  102.             turnOffWall();
  103.             PRIMARY_RECOGNITION.stop();
  104.         }
  105.     }
  106.  
  107.     PRIMARY_RECOGNITION.onresult = (event) => {
  108.         let interimTranscript = '';
  109.  
  110.         for (let i = event.resultIndex; i < event.results.length; i++) {
  111.             const transcript = event.results[i][0].transcript;
  112.  
  113.             if (event.results[i].isFinal) {
  114.                 finalTranscript += transcript;
  115.             } else {
  116.                 interimTranscript += transcript;  // append interim result
  117.             }
  118.         }
  119.  
  120.         PRIMARY_ASSISTANT_RESULT.textContent = finalTranscript + ' ' + interimTranscript;
  121.  
  122.         checkKeyWord(interimTranscript);
  123.     };
  124.  
  125.     PRIMARY_RECOGNITION.onerror = (event) => {
  126.         console.error('primary Speech recognition error:', event.error);
  127.     };
  128.  
  129.     PRIMARY_RECOGNITION.onend = () => {
  130.         setTimeout(stopPrimaryRecog, 500);
  131.     };
  132.  
  133.  
  134.     function startPrimaryRecog() {
  135.         autoRestart = false;
  136.         RECOGNITION_ALWAYS.stop();
  137.         awakeSound.play();
  138.         mainSection.style.setProperty('--border-opacity', '1');
  139.         assistantPrimaryElement.style.opacity = '1';
  140.         PRIMARY_RECOGNITION.start();  // Start the primary recognition
  141.         // console.log('wake word detected - starting primary recognition');
  142.     }
  143.    
  144.     function stopPrimaryRecog() {
  145.         mainSection.style.setProperty('--border-opacity', '0')
  146.         assistantPrimaryElement.style.opacity = '0';
  147.         sleepSound.play();
  148.         autoRestart = true;
  149.         PRIMARY_RECOGNITION.stop();
  150.         RECOGNITION_ALWAYS.start();
  151.         // console.log("primary recognition stopped");
  152.         PRIMARY_ASSISTANT_RESULT.textContent = '';
  153.         finalTranscript = '';
  154.     }
  155.  
  156.     // mainSection.addEventListener('click', () => {
  157.     //     RECOGNITION_ALWAYS.start();
  158.     // });
  159.  
  160.     introSection.addEventListener('click', function() {
  161.         clickTooltip.style.opacity = '0';
  162.         setTimeout(() => {
  163.             clickTooltip.style.display = 'none';
  164.             wakeTooltip.style.display = 'flex';
  165.             setTimeout(() => {
  166.                 wakeTooltip.style.opacity = '1';
  167.                 RECOGNITION_ALWAYS.start();
  168.             }, 200);
  169.         }, 700);
  170.     });
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement