Saturday, November 17, 2012

AlphaLabs Node Garden - adding speech

My first attempt at a play with the AlphaLabs Node Garden.

I thought using the screen was too much like manual labour - so I thought I'd try voice control.

The verdict - wow, is it simple to add voice control to your apps!






private async Task StartRecognizeAsync()
{
var speechRecognizer = new SpeechRecognizer();
speechRecognizer.Grammars.AddGrammarFromList(
"answer",
new string[]
{
"Left", "Right", "Up", "Down", "AlphaLabs", "Centre"
});
while (true)
{
var result = await speechRecognizer.RecognizeAsync();
if (result.TextConfidence != SpeechRecognitionConfidence.Rejected)
{
await ProcessResult(result);
}
else
{
Debug.WriteLine("No text!");
}
}
}
private async Task ProcessResult(SpeechRecognitionResult result)
{
Debug.WriteLine("Yes! {0}", result.Text);
var x = _myNode.X;
var y = _myNode.Y;
switch (result.Text)
{
case "Left":
x -= 100;
break;
case "Right":
x += 100;
break;
case "Up":
y -= 100;
break;
case "Down":
y += 100;
break;
case "Centre":
x = 240;
y = 400;
break;
case "AlphaLabs":
await DoAlphaLabsSequence();
x = 240;
y = 400;
break;
}
_myNode.SetPosition(x, y);
}
private async Task DoAlphaLabsSequence()
{
_myNode.SetPosition(10,10);
await Task.Delay(1000);
_myNode.SetPosition(10, 790);
await Task.Delay(1000);
_myNode.SetPosition(470, 790);
await Task.Delay(1000);
_myNode.SetPosition(470, 10);
await Task.Delay(1000);
_myNode.SetPosition(110, 210);
await Task.Delay(1000);
_myNode.SetPosition(110, 590);
await Task.Delay(1000);
_myNode.SetPosition(370, 590);
await Task.Delay(1000);
_myNode.SetPosition(370, 210);
await Task.Delay(1000);
}
public void SetPosition(double x, double y)
{
if (x < 0)
x = 0;
if (x > 480)
x = 480;
if (y < 0)
y = 0;
if (y > 800)
y = 800;
this.X = x;
this.Y = y;
UpdateGardener();
}
public void UpdateGardener()
{
this.gardener.UpdateSelfNodePosition(this.X, this.Y);
}
// Also remove the Update override
// public override void Update(Size canvasSize)
// {
// }
// And in the constructor, add:
public MyVisualNode(Random rand, Canvas mainCanvas, NodeGardenLib.Gardener gardener)
: base(rand, mainCanvas)
{
// ...
// existing code...
// ...
this.DisableVirtualMovement = true;
}
<Capability Name="ID_CAP_MICROPHONE" />
<Capability Name="ID_CAP_SPEECH_RECOGNITION" />
// in Update(Size) change:
if (Math.Abs(this.CurrentX - this.X) > 0.5 && Math.Abs(this.CurrentY - this.Y) > 0.5)
// to an OR test like:
if (Math.Abs(this.CurrentX - this.X) > 0.5 || Math.Abs(this.CurrentY - this.Y) > 0.5)

1 comment:

  1. So that's why you were having issues moving in just one direction. :)
    Sorry.

    ReplyDelete