This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (52 loc) · 1.88 KB
/
index.js
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
var ContainerPlugin = require('container_plugin');
var JST = require('./jst');
var _ = require('underscore');
class SpeechControl extends ContainerPlugin {
get name() { return 'speech_control' }
constructor(options) {
super(options)
if ('webkitSpeechRecognition' in window) {
this.createRecognition()
}
}
createRecognition() {
this.recognition = new webkitSpeechRecognition()
this.recognition.continuous = true
this.recognition.interimResults = true
this.recognition.addEventListener('start', () => this.recognitionStarted())
this.recognition.addEventListener('end', () => this.recognitionEnded())
this.recognition.addEventListener('result', (event) => this.recognitionTalked(event))
this.recognition.addEventListener('error', function (error) { console.log(error) })
this.recognition.start()
}
recognitionStarted() { console.log('start receiving voice commands') }
recognitionEnded() { console.log('done receiving voice commands') }
recognitionTalked(event) { this.handleCommand(event) }
executeCommand(message) {
if (message.match(/play/)) {
console.log("playing")
if (!this.container.isPlaying()) this.container.play()
} else if (message.match(/stop/)) {
console.log("stopping")
this.container.stop()
} else if (message.match(/pause/)) {
console.log("pausing")
this.container.pause()
} else if (message.match(/mute/)) {
console.log("muting")
this.container.setVolume(0)
} else {
console.log('unrecognized command:', message)
}
}
handleCommand(event) {
if (event.results && event.results.length) {
for (var i = event.resultIndex; i <= event.results.length; ++i) {
if (_.has(event.results[i], 0)) {
this.executeCommand(event.results[i][0].transcript)
}
}
}
}
}
module.exports = window.SpeechControl = SpeechControl;