-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
105 lines (99 loc) · 3.95 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
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
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
AFRAME.registerSystem('speech-command', {
init: function () {
//console.log("in speech-command system init");
this.entities = [];
//window.addEventListener('loaded', this.onSceneLoaded.bind(this));
},
registerMe: function (comp) {
this.entities.push(comp);
//console.log("in register, comp: "+comp.data.command);
},
unregisterMe: function (comp) {
var index = this.entities.indexOf(comp);
this.entities.splice(index, 1);
},
onSceneLoaded: function(evt) {
//console.log("in speech-command system onSceneLoaded listener");
},
play: function() {
//console.log("in system play, entities: "+this.entities);
}
});
AFRAME.registerComponent('speech-command', {
multiple: true,
schema: {
command: { type: 'string' },
type: { type: 'string' },
targetElement: { type: 'selector' },
targetComponent: { type: 'string' },
function: { type: 'string' },
attribute: { type: 'string' },
value: { type: 'string' },
keyCode: { type: 'string' }
},
init: function () {
this.system.registerMe(this);
if (!this.data.targetElement) {
this.data.targetElement = this.el;
}
if (this.data.keyCode) {
window.addEventListener('keyup', this.onKeyup.bind(this));
}
},
remove: function () {
this.system.unregisterMe(this);
},
play: function() {
//console.log("in speech-command play, command: "+this.data.command+", type: "+this.data.type);
},
executeCommand: function () {
//console.log("in executeCommand for: "+this.data.targetElement);
var targetElement = this.data.targetElement;
if (this.data.type == 'attribute') {
//console.log("about to change attribute "+this.data.attribute+" to: "+this.data.value);
targetElement.setAttribute(this.data.attribute, this.data.value);
} else if (this.data.type == 'function') {
//console("targetElement: "+targetElement+", components"+targetElement.components);
var targetComponent = targetElement.components[this.data.targetComponent];
targetComponent[this.data.function]();
}
},
onKeyup: function (evt) {
if (evt.keyCode == this.data.keyCode) {
//console.log("in speech command keyup for: "+this.data.command);
this.executeCommand();
}
}
});
AFRAME.registerComponent('annyang-speech-recognition', {
init: function () {
//console.log("in annyang-speech-recognition init");
},
play: function() {
if (annyang) {
//console.log("annyang: "+annyang);
//console.log("annyang.addCommands: "+annyang.addCommands);
var speechCommandSystem = document.querySelector('a-scene').systems['speech-command'];
var commands = {};
var commandsMap = {};
for (var i = 0; i < speechCommandSystem.entities.length; i++) {
var speechCommand = speechCommandSystem.entities[i];
commandsMap[speechCommand.data.command] = speechCommand;
// note: function empty here because real work is done in the resultMatch callback below
commands[speechCommand.data.command] = function() { };
}
annyang.addCommands(commands);
annyang.addCallback('resultMatch', function(userSaid, commandText, phrases) {
//console.log("commandText: "+commandText); // sample output: 'hello (there)'
var speechCommand = commandsMap[commandText];
speechCommand.executeCommand();
});
// Start listening. You can call this here, or attach this call to an event, button, etc.
annyang.start();
}
}
});