You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ws-client.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // #region web speech recognition api
  2. var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
  3. var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
  4. var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
  5. // #endregion
  6. // #region state management
  7. var state = '';
  8. var question = 0;
  9. var rePrompt = false;
  10. var partTwo = false;
  11. var questionThreeCount = 0;
  12. var strike = 0;
  13. // #endregion
  14. // #region questions
  15. const QUESTION_ONE = 'Ich werde Ihnen jetzt langsam eine Liste mit Worten vorlesen. Danach wiederholen Sie bitte möglichst viele dieser Worte. Auf die Reihenfolge kommt es nicht an.';
  16. const QUESTION_ONE_PT2 = 'Vielen Dank. Nun nenne ich Ihnen die gleichen 10 Worte ein zweites mal. Auch danach sollen Sie wieder möglichst viele Worte wiederholen';
  17. const QUESTION_TWO = 'Nennen Sie mir bitte so viel Dinge wie möglich, die man im Supermarkt kaufen kann. Sie haben dafür eine Minute Zeit. Und Los';
  18. const QUESTION_THREE = 'Ich werde Ihnen jetzt eine Zahlenreihe nennen, die Sie mir dann bitte in umgekehrter Reihenfolge wiederholen sollen. Wenn ich beispielsweise, vier - fünf sage, dann sagen Sie bitte, fünf - vier.';
  19. // #endregion
  20. // #region intents
  21. const WELCOME_INTENT = 'Default Welcome Intent';
  22. const WELCOME_FOLLOWUP_YES = 'Default Welcome Intent - yes';
  23. const WELCOME_FOLLOWUP_NO = 'Default Welcome Intent - no';
  24. const MORE_TIME = 'Add Time Intent';
  25. const MORE_TIME_YES = 'Add Time Intent - yes';
  26. const MORE_TIME_NO = 'Add Time Intent - no';
  27. const QUIT_INTENT = 'Quit Intent';
  28. const FALLBACK_INTENT = 'Default Fallback Intent';
  29. const HELP_INTENT = 'Help Intent';
  30. const CHOOSE_QUESTION = 'Frage_Starten';
  31. const NEXT_QUESTION = 'Nächste Frage';
  32. // #endregion
  33. // #region questions and expected results
  34. const QUESTION_ONE_ANSWERS = { 'teller': 1, 'hund': 1, 'lampe': 1, 'brief': 1, 'apfel': 1, 'apfelwiese': 2, 'apfelbaum': 2, 'und': 1, 'hose': 1, 'tisch': 1, 'wiese': 1, 'glas': 1, 'baum': 1 };
  35. const QUESTION_ONE_QUESTIONS = ['teller', 'hund', 'lampe', 'brief', 'apfel', 'hose', 'tisch', 'wiese', 'glas', 'baum'];
  36. const QUESTION_TWO_ANSWERS = {};
  37. var QUESTION_TWO_QUESTIONS;
  38. const QUESTION_THREE_QUESTIONS_PT1 = ['7, 2', '4, 7, 9', '5, 4, 9, 6', '2, 7, 5, 3, 6', '8, 1, 3, 5, 4, 2'];
  39. const QUESTION_THREE_QUESTIONS_PT2 = ['8, 6', '3, 1, 5', '1, 9, 7, 4', '1, 3, 5, 4, 8', '4, 1, 2, 7, 9, 5'];
  40. const QUESTION_THREE_ANSWERS_PT1 = ['27', '974', '6945', '63572', '245318'];
  41. const QUESTION_THREE_ANSWERS_PT2 = ['68', '513', '4791', '84531', '597214'];
  42. LoadQuestionTwo();
  43. function LoadQuestionTwo () {
  44. var xmlhttp;
  45. if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  46. xmlhttp = new XMLHttpRequest();
  47. } else { // code for IE6, IE5
  48. xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  49. }
  50. xmlhttp.onreadystatechange = function () {
  51. if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
  52. var text = xmlhttp.responseText.toLowerCase();
  53. // Now convert it into array using regex
  54. QUESTION_TWO_QUESTIONS = text.split('\r\n');
  55. for (let word of QUESTION_TWO_QUESTIONS) {
  56. QUESTION_TWO_ANSWERS[word] = 1;
  57. }
  58. }
  59. };
  60. xmlhttp.open('GET', 'lebensmittel.txt', true);
  61. xmlhttp.send();
  62. }
  63. // #endregion
  64. // #region points
  65. const questionPoints = {
  66. 1: 0,
  67. 2: 0,
  68. 3: 0,
  69. 4: 0,
  70. 5: 0 };
  71. // #endregion
  72. // tokenization
  73. const separators = [' ', '\\\+', '-', '\\\(', '\\\)', '\\*', '/', ':', '\\\?'];
  74. // Timers
  75. var timerId;
  76. // #region html elements
  77. var serverPara = document.querySelector('.server');
  78. var diagnosticPara = document.querySelector('.output');
  79. var testBtn = document.querySelector('button');
  80. var testBtn2 = document.getElementById('speechBtn');
  81. var infoPara = document.getElementById('info');
  82. var questionNumDisplay = document.querySelector('.quest');
  83. // #endregion
  84. // websocket to communicate with the server
  85. var ws = new WebSocket('ws://' + window.location.host + window.location.pathname + 'ws');
  86. // #region speech recognition initialization
  87. var recognition = new SpeechRecognition();
  88. recognition.lang = 'de-DE';
  89. // recognition.interimResults = false;
  90. recognition.maxAlternatives = 1;
  91. recognition.continuous = true;
  92. var answerQuery = '';
  93. var skipRecording = false;
  94. // #endregion
  95. // #region speech synthesis initialization
  96. var speechsynth = new SpeechSynthesisUtterance();
  97. var listSpeechsynth = new SpeechSynthesisUtterance();
  98. var voices;
  99. // #endregion
  100. // #region speech events
  101. window.speechSynthesis.onvoiceschanged = function () {
  102. voices = window.speechSynthesis.getVoices();
  103. voices.forEach(element => {
  104. if (element.name === 'Google Deutsch') {
  105. speechsynth.voice = element;
  106. listSpeechsynth.voice = element;
  107. }
  108. });
  109. listSpeechsynth.rate = 0.7;
  110. };
  111. speechsynth.onend = function (event) {
  112. switch (question) {
  113. case 1:
  114. break;
  115. case 2:
  116. break;
  117. case 3:
  118. break;
  119. case 4:
  120. break;
  121. case 5:
  122. break;
  123. }
  124. if (!skipRecording) {
  125. recognition.start();
  126. console.log('reocgnition started. Question: ' + question);
  127. }
  128. skipRecording = false;
  129. diagnosticPara.textContent = '';
  130. console.log('global speech end');
  131. };
  132. // #endregion
  133. // #region websocket events
  134. ws.onopen = function () {
  135. serverPara.style.background = 'green';
  136. serverPara.innerHTML = 'Server online';
  137. };
  138. ws.onmessage = function (payload) {
  139. var dialogflowResult = JSON.parse(payload.data);
  140. checkIntent(dialogflowResult);
  141. // document.querySelector('h1').innerHTML = dialogflowResult.intent.displayName;
  142. };
  143. // #endregion
  144. // INTENT HANDLING
  145. function checkIntent (result) {
  146. switch (result.intent.displayName) {
  147. case QUIT_INTENT:
  148. state = 'quit';
  149. if (timerId !== undefined) {
  150. clearTimeout(timerId);
  151. }
  152. skipRecording = true;
  153. speak('Beende die Durchführung.');
  154. break;
  155. case WELCOME_INTENT:
  156. state = 'detect';
  157. speak(result.fulfillmentText);
  158. break;
  159. case WELCOME_FOLLOWUP_YES:
  160. startQuestion(1);
  161. break;
  162. case WELCOME_FOLLOWUP_NO:
  163. skipRecording = true;
  164. speak('Okay, Danke fürs Benutzen.');
  165. break;
  166. case MORE_TIME:
  167. state = 'detect';
  168. speak('Brauchen Sie noch etwas Zeit?');
  169. break;
  170. case MORE_TIME_YES:
  171. rePrompt = true;
  172. state = 'answer';
  173. speak('Alles klar');
  174. break;
  175. case MORE_TIME_NO:
  176. skipRecording = true;
  177. state = 'answer';
  178. speak('Verstanden');
  179. recognition.stop();
  180. ws.send(answerQuery);
  181. break;
  182. case CHOOSE_QUESTION:
  183. question = result.parameters.fields.num.numberValue;
  184. state = 'answer';
  185. handleQuestion();
  186. break;
  187. case FALLBACK_INTENT:
  188. // if (state === 'answer') {
  189. // handleAnswer(result.queryText)
  190. // }
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. // #region question handling
  197. function startQuestion (number) {
  198. question = number;
  199. state = 'answer';
  200. questionNumDisplay.textContent = 'Question: ' + question;
  201. handleQuestion();
  202. }
  203. function handleQuestion () {
  204. switch (question) {
  205. case 1:
  206. skipRecording = true;
  207. speak(QUESTION_ONE);
  208. readQuestionOne();
  209. break;
  210. case 2:
  211. readQuestionTwo();
  212. break;
  213. case 3:
  214. readQuestionThree();
  215. break;
  216. case 4:
  217. break;
  218. case 5:
  219. break;
  220. }
  221. }
  222. function readQuestionOne () {
  223. for (let i = 0; i < QUESTION_ONE_QUESTIONS.length; i++) {
  224. let utterance = new SpeechSynthesisUtterance();
  225. utterance.voice = voices[2];
  226. utterance.rate = 0.75;
  227. utterance.text = QUESTION_ONE_QUESTIONS[i];
  228. window.speechSynthesis.speak(utterance);
  229. if (i === 9) {
  230. utterance.onend = function (event) {
  231. recognition.start();
  232. console.log('reocgnition started. Question: ' + question);
  233. };
  234. }
  235. }
  236. }
  237. function readQuestionTwo () {
  238. let utterance = new SpeechSynthesisUtterance();
  239. utterance.voice = voices[2];
  240. utterance.text = QUESTION_TWO;
  241. window.speechSynthesis.speak(utterance);
  242. utterance.onend = function (event) {
  243. window.setTimeout(
  244. function () {
  245. recognition.stop();
  246. window.setTimeout(
  247. function () {
  248. handleAnswer(answerQuery);
  249. answerQuery = '';
  250. }, 3000);
  251. }, 6000);
  252. recognition.start();
  253. console.log('reocgnition started. Question: ' + question);
  254. };
  255. }
  256. function readQuestionThree () {
  257. skipRecording = true;
  258. speak('Dankeschön, weiter geht es mit der nächsten Frage.');
  259. let utterance = new SpeechSynthesisUtterance();
  260. utterance.voice = voices[2];
  261. utterance.text = QUESTION_THREE;
  262. window.speechSynthesis.speak(utterance);
  263. utterance.onend = function (event) {
  264. speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]);
  265. };
  266. utterance.onerror = function (event) {
  267. console.log('An error has occurred with the speech synthesis: ' + event.error);
  268. };
  269. }
  270. function handleAnswer (query) {
  271. switch (question) {
  272. case 1:
  273. handleAnswerToFirstQuestion(query);
  274. break;
  275. case 2:
  276. handleAnswerToSecondQuestion(query);
  277. break;
  278. case 3:
  279. handleAnswerToThirdQuestion(query);
  280. break;
  281. case 4:
  282. break;
  283. case 5:
  284. break;
  285. }
  286. }
  287. function handleAnswerToFirstQuestion (answer) {
  288. var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
  289. questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
  290. if (partTwo) {
  291. partTwo = false;
  292. console.log('question 1 points: ' + questionPoints[question]);
  293. skipRecording = true;
  294. speak('Vielen Dank, nun geht es weiter mit der nächsten Frage');
  295. startQuestion(2);
  296. // state = 'detect'
  297. } else {
  298. rePrompt = false;
  299. skipRecording = true;
  300. speak(QUESTION_ONE_PT2);
  301. readQuestionOne(QUESTION_ONE);
  302. partTwo = true;
  303. }
  304. }
  305. function handleAnswerToSecondQuestion (answer) {
  306. var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
  307. questionPoints[question] = calculatePoints(tokens, QUESTION_TWO_ANSWERS);
  308. console.log('question 2 points: ' + questionPoints[question]);
  309. startQuestion(3);
  310. // state = 'detect'
  311. }
  312. function handleAnswerToThirdQuestion (query) {
  313. speechsynth.rate = 0.87;
  314. query = query.replace(' ', '');
  315. let answerArray;
  316. let questionArray;
  317. if (!partTwo) {
  318. answerArray = QUESTION_THREE_ANSWERS_PT1;
  319. } else {
  320. answerArray = QUESTION_THREE_ANSWERS_PT2;
  321. }
  322. if (query === answerArray[questionThreeCount]) {
  323. strike = 0;
  324. partTwo = false;
  325. questionThreeCount++;
  326. questionPoints[question] = questionThreeCount + 1;
  327. questionArray = QUESTION_THREE_QUESTIONS_PT1;
  328. } else {
  329. strike++;
  330. partTwo = true;
  331. questionArray = QUESTION_THREE_QUESTIONS_PT2;
  332. }
  333. if (strike === 2 || questionThreeCount === 5) {
  334. speechsynth.rate = 1;
  335. console.log('question 3 points: ' + questionPoints[question]);
  336. skipRecording = true;
  337. speak('weiter geht es mit der Nächsten Frage');
  338. startQuestion(4);
  339. return;
  340. }
  341. speak(questionArray[questionThreeCount]);
  342. console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionPoints[question]);
  343. }
  344. // #endregion
  345. // function recognizeSpeech () {
  346. // if (state === 'answer') {
  347. // var arr;
  348. // switch (question) {
  349. // case 1:
  350. // arr = QUESTION_ONE_QUESTIONS;
  351. // break;
  352. // case 2:
  353. // // arr = QUESTION_TWO_QUESTIONS;
  354. // break;
  355. // case 3:
  356. // arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  357. // break;
  358. // case 4:
  359. // break;
  360. // case 5:
  361. // break;
  362. // }
  363. // // var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + arr.join(' | ') + ' ;';
  364. // // var speechRecognitionList = new SpeechGrammarList();
  365. // // speechRecognitionList.addFromString(grammar, 1);
  366. // // recognition.grammars = speechRecognitionList;
  367. // }
  368. // #region speech recognition event
  369. recognition.onresult = function (event) {
  370. var last = event.results.length - 1;
  371. var speechResult = event.results[last][0].transcript.toLowerCase();
  372. diagnosticPara.textContent += speechResult + ' ';
  373. // console.log('Confidence: ' + event.results[0][0].confidence)
  374. console.log('process: ' + speechResult);
  375. processSpeech(speechResult);
  376. // testBtn.disabled = false
  377. // testBtn.textContent = 'record...'
  378. };
  379. 1;
  380. recognition.onspeechend = function () {
  381. // recognition.stop();
  382. // testBtn.disabled = false;
  383. // testBtn.textContent = 'Start new test';
  384. };
  385. recognition.onerror = function (event) {
  386. testBtn.disabled = false;
  387. testBtn.textContent = 'Start new test';
  388. diagnosticPara.textContent = 'Error occurred in recognition: ' + event.error;
  389. };
  390. recognition.onaudiostart = function (event) {
  391. // Fired when the user agent has started to capture audio.
  392. };
  393. recognition.onaudioend = function (event) {
  394. };
  395. recognition.onend = function (event) {
  396. // Fired when the speech recognition service has disconnected.
  397. };
  398. recognition.onnomatch = function (event) {
  399. // Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the confidence threshold.
  400. // console.log('SpeechRecognition.onnomatch')
  401. };
  402. recognition.onsoundstart = function (event) {
  403. // Fired when any sound — recognisable speech or not — has been detected.
  404. };
  405. recognition.onsoundend = function (event) {
  406. // Fired when any sound — recognisable speech or not — has stopped being detected.
  407. };
  408. recognition.onspeechstart = function (event) {
  409. // Fired when sound that is recognised by the speech recognition service as speech has been detected.
  410. };
  411. recognition.onstart = function (event) {
  412. // Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
  413. };
  414. // }
  415. // #endregion
  416. // #region global functions
  417. function processSpeech (speechResult) {
  418. console.log('To dialogflow: ' + speechResult);
  419. ws.send(speechResult);
  420. let timeOut;
  421. switch (question) {
  422. case 1:
  423. timeOut = 6500;
  424. break;
  425. case 2:
  426. answerQuery += speechResult;
  427. return;
  428. case 3:
  429. if (speechResult.includes('uhr')) {
  430. speechResult = speechResult.replace('uhr', '');
  431. }
  432. timeOut = 6500;
  433. break;
  434. case 4:
  435. break;
  436. case 5:
  437. timeOut = 6500;
  438. break;
  439. }
  440. if (state === 'answer') {
  441. if (timerId != undefined) {
  442. clearTimeout(timerId);
  443. }
  444. answerQuery += speechResult;
  445. timerId = window.setTimeout(
  446. function () {
  447. // if (!rePrompt) {
  448. // ws.send('ich brauche noch etwas Zeit')
  449. // } else {
  450. console.log('recording end. Evaluate: ' + answerQuery);
  451. handleAnswer(answerQuery);
  452. answerQuery = '';
  453. diagnosticPara.textContent = '';
  454. // }
  455. recognition.stop();
  456. console.log('timer fallback');
  457. }, timeOut);
  458. } else {
  459. console.log('recording end.');
  460. recognition.stop();
  461. }
  462. }
  463. function startDemenzScreening () {
  464. // ws.send('starte demenz test');
  465. startQuestion(2);
  466. testBtn.disabled = true;
  467. testBtn.textContent = 'Test in progress';
  468. infoPara.textContent = 'wait...';
  469. diagnosticPara.textContent = 'detecting...';
  470. }
  471. function testSpeechOut () {
  472. answerQuery = 'apfel wiese tisch apfel lampe pferd';
  473. question = 1;
  474. for (let i = 0; i < 2; i++) {
  475. var tokens = answerQuery.split(new RegExp(separators.join('|'), 'g'));
  476. questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
  477. }
  478. console.log(questionPoints[question]);
  479. // speechsynth.text = 'test 123';
  480. // speechsynth.volume = 1;
  481. // speechsynth.rate = 1;
  482. // console.log(speechsynth);
  483. // window.speechSynthesis.speak(speechsynth);
  484. // console.log(window.speechSynthesis);
  485. }
  486. function speak (sentence) {
  487. speechsynth.text = sentence;
  488. window.speechSynthesis.speak(speechsynth);
  489. }
  490. function calculatePoints (tokens, d) {
  491. let points = 0;
  492. let dict = {};
  493. Object.assign(dict, d);
  494. for (let word of tokens) {
  495. if (dict[word] !== undefined) {
  496. points += dict[word];
  497. delete dict[word];
  498. }
  499. }
  500. return points;
  501. }
  502. // #endregion
  503. testBtn.addEventListener('click', startDemenzScreening);
  504. testBtn2.addEventListener('click', testSpeechOut);