-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy path05continuousrecognition.html
63 lines (55 loc) · 1.57 KB
/
05continuousrecognition.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
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.5/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.5/addons/p5.dom.js"></script>
<script src="../lib/p5.speech.js"></script>
<script>
var myRec = new p5.SpeechRec('en-US', parseResult); // new P5.SpeechRec object
myRec.continuous = true; // do continuous recognition
myRec.interimResults = true; // allow partial recognition (faster, less accurate)
var x, y;
var dx, dy;
function setup()
{
// graphics stuff:
createCanvas(800, 600);
background(255, 255, 255);
fill(0, 0, 0, 255);
x = width/2;
y = height/2;
dx = 0;
dy = 0;
// instructions:
textSize(20);
textAlign(LEFT);
text("draw: up, down, left, right, clear", 20, 20);
//myRec.onResult = parseResult; // now in the constructor
myRec.start(); // start engine
}
function draw()
{
ellipse(x, y, 5, 5);
x+=dx;
y+=dy;
if(x<0) x = width;
if(y<0) y = height;
if(x>width) x = 0;
if(y>height) y = 0;
}
function parseResult()
{
// recognition system will often append words into phrases.
// so hack here is to only use the last word:
var mostrecentword = myRec.resultString.split(' ').pop();
if(mostrecentword.indexOf("left")!==-1) { dx=-1;dy=0; }
else if(mostrecentword.indexOf("right")!==-1) { dx=1;dy=0; }
else if(mostrecentword.indexOf("up")!==-1) { dx=0;dy=-1; }
else if(mostrecentword.indexOf("down")!==-1) { dx=0;dy=1; }
else if(mostrecentword.indexOf("clear")!==-1) { background(255); }
console.log(mostrecentword);
}
</script>
</head>
<body>
</body>
</html>