Skip to content

Instantly share code, notes, and snippets.

@pdehn
Last active August 6, 2020 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pdehn/2fa316d684419eb59eae to your computer and use it in GitHub Desktop.
Save pdehn/2fa316d684419eb59eae to your computer and use it in GitHub Desktop.
This is some example code from my latest work on integrating speech recognition in Unity. "squadControl.xml" is the main grammar definition (uses SRGS + SISR script tags) and squadControl.jsgf is a JSGF grammar that's generated from it for use by Pocketsphinx. When Pocketsphinx gives plain text output, I run it through an SRGS+SISR parser writte…
#JSGF V1.0;
grammar outputGrammar;
public <command> = (<squadCommand> | <teamCommand> );
<squadCommand> = squad (formation <formation> [of <pluralFormation> ] | (head | move | go) <direction> | halt );
<teamCommand> = <teamName> (formation <formation> );
<teamName> = (alpha | bravo | charlie);
<direction> = (north | east | south | west | northeast | northwest | southeast | southwest );
<formation> = (wedge | column | line);
<pluralFormation> = (wedges | columns | lines );
<?xml version="1.0" encoding="UTF-8"?>
<grammar>
<rule id="command">
<one-of>
<item><ruleref uri="#squadCommand" /><tag>out = rules.squadCommand;</tag></item>
<item><ruleref uri="#teamCommand" /><tag>out = rules.teamCommand;</tag></item>
</one-of>
</rule>
<rule id="squadCommand">
squad
<one-of>
<item>
formation
<tag>out.type = "squad-set-formation";</tag>
<ruleref uri="#formation" />
<tag>out.squadFormation = rules.formation;</tag>
<item repeat="0-1">
of <ruleref uri="pluralFormation" />
<tag>out.allTeamsFormation = rules.pluralFormation;</tag>
</item>
</item>
<item>
<one-of>
<item>head</item>
<item>move</item>
<item>go</item>
</one-of>
<tag>out.type = "squad-set-heading";</tag>
<ruleref uri="#direction" />
<tag>out.heading = rules.direction;</tag>
</item>
<item>
halt
<tag>out.type = "squad-halt";</tag>
</item>
</one-of>
</rule>
<rule id="teamCommand">
<ruleref uri="#teamName" />
<tag>out.team = rules.teamName;</tag>
<one-of>
<item>
formation
<tag>out.type = "team-set-formation";</tag>
<ruleref uri="#formation" />
<tag>out.formation = rules.formation;</tag>
</item>
</one-of>
</rule>
<rule id="teamName">
<one-of>
<item>alpha</item>
<item>bravo</item>
<item>charlie</item>
</one-of>
</rule>
<rule id="direction">
<one-of>
<item>north<tag>out = 0.0;</tag></item>
<item>east<tag>out = 90.0;</tag></item>
<item>south<tag>out = 180.0;</tag></item>
<item>west<tag>out = 270.0;</tag></item>
<item>northeast<tag>out = 45.0;</tag></item>
<item>northwest<tag>out = 315.0;</tag></item>
<item>southeast<tag>out = 135.0;</tag></item>
<item>southwest<tag>out = 225.0;</tag></item>
</one-of>
</rule>
<rule id="formation">
<one-of>
<item>wedge</item>
<item>column</item>
<item>line</item>
</one-of>
</rule>
<rule id="pluralFormation">
<one-of>
<item>wedges<tag>out = "wedge";</tag></item>
<item>columns<tag>out = "column";</tag></item>
<item>lines<tag>out = "line";</tag></item>
</one-of>
</rule>
</grammar>
namespace USphinx.Demo.SquadControl
{
using System.Text.RegularExpressions;
using UnityEngine;
using Srgs;
public class SquadLeader : Unit
{
public float speed = 2f;
Vector3 _direction;
override protected void Awake()
{
base.Awake();
recognizer.OnUtterance += OnUtterance;
}
override protected void FixedUpdate()
{
base.FixedUpdate();
transform.position += speed * _direction * Time.fixedDeltaTime;
}
void OnUtterance(string utt, SrgsItem semanticParse)
{
var typeItem = semanticParse.XGet("type");
if (typeItem == null) return;
switch (typeItem.AsString())
{
case "squad-set-heading":
var heading = (float) semanticParse.XGet("heading").AsDouble();
_direction = Quaternion.Euler(0f, heading, 0f) * Vector3.forward;
break;
case "squad-halt":
_direction = Vector3.zero;
break;
}
}
}
}
namespace USphinx.Demo.SquadControl
{
using System.Text.RegularExpressions;
using UnityEngine;
using Srgs;
public class TeamLeader : Follower
{
override protected void Awake()
{
base.Awake();
recognizer.OnUtterance += OnUtterance;
}
void OnUtterance(string utt, SrgsItem semanticParse)
{
var typeItem = semanticParse.XGet("type");
if (typeItem == null) return;
switch (typeItem.AsString())
{
case "squad-set-formation":
switch (semanticParse.XGet("squadFormation").AsString()) {
case "wedge": this.formation = Formation.WEDGE; break;
case "column": this.formation = Formation.COLUMN; break;
case "line": this.formation = Formation.LINE; break;
}
break;
}
}
}
}
namespace USphinx.Demo.SquadControl
{
using System.Text.RegularExpressions;
using UnityEngine;
using Srgs;
public class TeamMember : Follower
{
public string team = "alpha";
override protected void Awake()
{
base.Awake();
recognizer.OnUtterance += OnUtterance;
}
void OnUtterance(string utt, SrgsItem semanticParse)
{
var typeItem = semanticParse.XGet("type");
if (typeItem == null) return;
switch (typeItem.AsString())
{
case "squad-set-formation":
var allTeamsFormation = semanticParse.XGet("allTeamsFormation");
if (allTeamsFormation != null) switch (allTeamsFormation.AsString())
{
case "wedge": this.formation = Formation.WEDGE; break;
case "column": this.formation = Formation.COLUMN; break;
case "line": this.formation = Formation.LINE; break;
}
break;
case "team-set-formation":
var team = semanticParse.XGet("team").AsString();
if (team == this.team) switch (semanticParse.XGet("formation").AsString())
{
case "wedge": this.formation = Formation.WEDGE; break;
case "column": this.formation = Formation.COLUMN; break;
case "line": this.formation = Formation.LINE; break;
}
break;
}
}
}
}
@FarhanJohari
Copy link

any kickstart on how to do it or basic thing to do.. help me please its already been 4 month i didn't finish my project..

@Delaley
Copy link

Delaley commented Dec 3, 2015

Yea that's TRUE getting Cmu Sphinx to work in Unity3D is full of nightmares.
Their forum is dead.

@KnightRiderGuy
Copy link

Yup it seems impossible to get information on how to do this. It's nice the code is shared but how dod you use it, like what goes where for example. What gets imported into Unity some basic step by step information would be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment