diff options
Diffstat (limited to 'point/binding/speech.js')
-rw-r--r-- | point/binding/speech.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/point/binding/speech.js b/point/binding/speech.js new file mode 100644 index 0000000..41f5914 --- /dev/null +++ b/point/binding/speech.js | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | * This file is part of "What's The Point" <https://github.com/Pacien/WhatsThePoint> | ||
3 | * Copyright (C) 2014 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * "What's The Point" is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Affero General Public License as | ||
7 | * published by the Free Software Foundation, either version 3 of the | ||
8 | * License, or (at your option) any later version. | ||
9 | * | ||
10 | * "What's The Point" is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | define(["control/control"], function (control) { | ||
20 | |||
21 | var speech = { | ||
22 | |||
23 | init: function (settings) { | ||
24 | if ("speechRecognition" in window || "webkitSpeechRecognition" in window) { | ||
25 | var recognition = new webkitSpeechRecognition(); | ||
26 | |||
27 | recognition.continuous = true; | ||
28 | recognition.interimResults = true; | ||
29 | recognition.lang = settings.speechSettings.lang; | ||
30 | |||
31 | recognition.onresult = this.onResult; | ||
32 | |||
33 | recognition.onend = function () { | ||
34 | recognition.start(); | ||
35 | }; | ||
36 | |||
37 | recognition.start(); | ||
38 | } | ||
39 | |||
40 | this.sleepDelay = 1500; | ||
41 | this.wakeupTime = 0; | ||
42 | |||
43 | this.command = { | ||
44 | NEXT_SLIDE: settings.speechSettings.keywords.NEXT_SLIDE, | ||
45 | PREVIOUS_SLIDE: settings.speechSettings.keywords.PREVIOUS_SLIDE, | ||
46 | }; | ||
47 | }, | ||
48 | |||
49 | onResult: function (speechEvent) { | ||
50 | var result = event.results[event.resultIndex]; | ||
51 | |||
52 | if (result.isFinal) { | ||
53 | return; | ||
54 | } | ||
55 | |||
56 | var sentence = result[0].transcript.split(" "); | ||
57 | var command = sentence[sentence.length - 1]; | ||
58 | speech.processCommand(command); | ||
59 | }, | ||
60 | |||
61 | processCommand: function (speechCommand) { | ||
62 | if (Date.now() < speech.wakeupTime) { | ||
63 | return; | ||
64 | } | ||
65 | |||
66 | var command = speech.translateCommand(speechCommand); | ||
67 | |||
68 | if (command === undefined) { | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | speech.wakeupTime = Date.now() + speech.sleepDelay; | ||
73 | control.dispatchEvent(control.EVENT.GOTO, control.GOTO[command]); | ||
74 | }, | ||
75 | |||
76 | translateCommand: function (speechCommand) { | ||
77 | for (var command in speech.command) { | ||
78 | for (var index in speech.command[command]) { | ||
79 | if (speechCommand.indexOf(speech.command[command][index]) > -1) { | ||
80 | return command; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | }, | ||
85 | }; | ||
86 | |||
87 | return speech; | ||
88 | |||
89 | }); | ||