-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
158 lines (131 loc) · 3.52 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<html>
<head>
<meta charset="UTF-8">
<title>Listen</title>
</head>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.6.0/annyang.min.js"></script>
<script src="js/adapter.js"></script>
<script src="js/JsSpeechRecognizer.js"></script>
<script>
var config = {
language: 'en-US',
commands: {
listen: {
keyword: 'Rocks',
answer: 'Starting a memo.',
},
},
};
function findVoice() {
let language = config.language;
// Prefer a google voice
let voice = speechSynthesis.getVoices().find(x => x.lang === language && x.name.startsWith('Google'));
if (voice) {
return voice;
}
// Secondary option which only matches a language
voice = speechSynthesis.getVoices().find(x => x.lang === language);
if (voice) {
return voice;
}
language = language.replace('-', '_');
// Match a language with a different form
voice = speechSynthesis.getVoices().find(x => x.lang === language);
if (voice) {
return voice;
}
return null;
}
function speak(text, onend) {
var msg = new SpeechSynthesisUtterance();
msg.voice = findVoice();
msg.lang = config.language;
msg.text = text;
msg.onend = onend;
speechSynthesis.speak(msg);
}
function trigger(phrases) {
var iftttWebhookKey = document.getElementById('ifttt_webhook_key').value;
if (!iftttWebhookKey) {
return;
}
var value1 = encodeURI(phrases.join('<br><br>'));
var xhr = new XMLHttpRequest();
xhr.open("GET", 'https://maker.ifttt.com/trigger/memo/with/key/' + iftttWebhookKey + '?value1=' + value1, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
// IFTTT Webhook doesn't support CORS, but can be ignored.
}
class CommandMode {
start() {
annyang.setLanguage(config.language);
annyang.addCallback('resultNoMatch', (phrases) => this.onResultNoMatch(phrases));
annyang.addCommands({
[config.commands.listen.keyword]: () => this.onCommandListen(),
});
annyang.start({ autoRestart: true, continuous: false });
}
stop() {
annyang.abort();
annyang.removeCommands();
annyang.removeCallback('result');
}
onResultNoMatch(phrases) {
console.log("resultNoMatch: ", phrases);
}
onCommandListen() {
this.stop();
speak(config.commands.listen.answer, () => {
new DictationMode().start();
});
}
}
class DictationMode {
start() {
annyang.setLanguage(config.language);
annyang.addCallback('result', (phrases) => this.onResult(phrases));
annyang.start({ autoRestart: true, continuous: true });
}
stop() {
annyang.abort();
annyang.removeCallback('result');
}
onResult(phrases) {
this.stop();
document.getElementById('result').innerHTML = phrases.map(x => '<p>' + x + '</p>').join('');
trigger(phrases);
speak(phrases[0], () => {
new CommandMode().start();
});
}
}
if (annyang) {
speechSynthesis.onvoiceschanged = () => {
new CommandMode().start();
};
}
</script>
<div>
<h2>Say "Rocks" to start a memo.</h2>
</div>
<div>
<h3>Results</h3>
<div id="result">
</div>
</div>
<div id="triggers">
<h3>Triggers</h3>
<p>
<label for="ifttt_webhook_key"><a href="https://ifttt.com/services/maker_webhooks/settings">IFTTT Webhook</a> Key</label>
<input id="ifttt_webhook_key" onchange="iftttWebhookKeyChanged(this)"></input>
<script>
document.getElementById('ifttt_webhook_key').value = localStorage.iftttWebhookKey || '';
function iftttWebhookKeyChanged(input) {
localStorage.iftttWebhookKey = input.value;
}
</script>
</p>
</div>
</body>
</html>