Skip to content

Commit

Permalink
spectator mode
Browse files Browse the repository at this point in the history
  • Loading branch information
blamarche committed Dec 22, 2020
1 parent ea5bf2a commit d0d97aa
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
79 changes: 71 additions & 8 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public partial class MainWindow : Window
static bool ascending = false;
static bool icoordEnabled = false;
static bool enableReadback = false;
static bool spectatorMode = false;
//static bool enableExcitedVoice = false;
static bool autoPlay = false;
static bool enableOpponentColorRead = true;
static int boardSize = 19;
Expand All @@ -50,6 +52,8 @@ public partial class MainWindow : Window
static BoardState currentGame;
static DispatcherTimer boardStateTimer;

static string lastSpoken = "";

public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -80,6 +84,7 @@ public MainWindow()
ch_StartStopCommands.Add("speech off");
ch_StartStopCommands.Add("male voice");
ch_StartStopCommands.Add("female voice");
//ch_StartStopCommands.Add("toggle excited voice");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
Expand All @@ -96,6 +101,8 @@ public MainWindow()
ch_play.Add("enable auto");
ch_play.Add("disable read back");
ch_play.Add("enable read back");
ch_play.Add("enable spectator");
ch_play.Add("disable spectator");
ch_play.Add("start game as black");
ch_play.Add("start game as white");
ch_play.Add("stop game");
Expand All @@ -107,6 +114,7 @@ public MainWindow()
ch_play.Add("board size 13");
ch_play.Add("board size 9");
ch_play.Add("troubleshoot");
ch_play.Add("repeat");
GrammarBuilder gb_play = new GrammarBuilder();
gb_play.Append(ch_play);
Grammar g_play = new Grammar(gb_play);
Expand Down Expand Up @@ -175,7 +183,7 @@ private void boardStateTimer_Tick(object sender, EventArgs e)
//int count = 0;
foreach (var c in changes)
{
if (c.PreviousStone==BoardState.Stone.Empty && c.Stone!= currentGameColor)
if (c.PreviousStone==BoardState.Stone.Empty && (c.Stone!= currentGameColor || spectatorMode))
{
sre.SpeechRecognized -= sre_SpeechRecognized;

Expand All @@ -187,10 +195,14 @@ private void boardStateTimer_Tick(object sender, EventArgs e)
string ch = Char.ToString((char)(x));
string color = (enableOpponentColorRead ? c.Stone.ToString()+", " : "");

string spk = "";
if (ascending) //not async because this might cause duplicate commands to be detected
ss.Speak(color + ch + ", " + (c.Y + 1));
spk=color + ch + ", " + (c.Y + 1);
else
ss.Speak(color + ch + ", " + (boardSize - c.Y));
spk=color + ch + ", " + (boardSize - c.Y);

ss.Speak(spk);
lastSpoken = spk;

//count++;
//if (count >= 2)
Expand All @@ -206,7 +218,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
//future use
}

private HotKey hk_Tab, hk_Left, hk_Right, hk_Up, hk_Down, hk_Numpad5, hk_BrowserHome, hk_BrowserBack;
private HotKey hk_Tab, hk_Left, hk_Right, hk_Up, hk_Down, hk_Numpad5, hk_BrowserHome, hk_BrowserBack, hk_End;
void EnableHotkeys()
{
hk_Tab = new HotKey(ModifierKeys.None, Keys.Tab, this);
Expand All @@ -232,12 +244,18 @@ void EnableHotkeys()

hk_Down = new HotKey(ModifierKeys.None, Keys.Down, this);
hk_Down.HotKeyPressed += (k) => { MoveMouseRelative(MouseDirection.DOWN); };

hk_End = new HotKey(ModifierKeys.None, Keys.End, this);
hk_End.HotKeyPressed += (k) => { speechOn = !speechOn; win.lStatus.Content = "Speech detection "+(speechOn? "on":"off"); };
}

static void SpeakAndLabel(string speak, string label="")
{
win.lStatus.Content = (label=="" ? speak.ToUpper():label.ToUpper());
//if (enableExcitedVoice)
// speak += "!";
ss.SpeakAsync(speak);
//lastSpoken = speak;
}

const float CONFIDENCE_THRESHOLD = 0.60f;
Expand All @@ -257,11 +275,13 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
{
SpeakAndLabel("Speech detection on");
speechOn = true;
return;
}
else if (txt.IndexOf("speech off") >= 0)
{
SpeakAndLabel("Speech detection off");
speechOn = false;
return;
}
if (speechOn == false) return;

Expand Down Expand Up @@ -344,7 +364,15 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
SpeakAndLabel("Male voice");
computervoice = VoiceGender.Male;
ss.SelectVoiceByHints(computervoice);
}
}/*
else if (txt.IndexOf("toggle excited voice") >= 0)
{
enableExcitedVoice = true;
if (enableExcitedVoice)
SpeakAndLabel("Excited voice enabled");
else
SpeakAndLabel("Excited voice disabled");
}*/
else if (txt.IndexOf("disable auto") >= 0)
{
SpeakAndLabel("Autoplay disabled");
Expand All @@ -365,6 +393,23 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
SpeakAndLabel("Coord readback enabled");
enableReadback = true;
}
else if (txt.IndexOf("disable spectator") >= 0)
{
SpeakAndLabel("Spectator mode disabled");
spectatorMode = false;
enableReadback = false;
currentGameColor = BoardState.Stone.Empty;
boardStateTimer.Stop();
}
else if (txt.IndexOf("enable spectator") >= 0)
{
SpeakAndLabel("Spectator mode enabled");
spectatorMode = true;
enableReadback = true;
currentGameColor = BoardState.Stone.Black;
currentGame = new BoardState(boardSize, topLeft, bottomRight);
boardStateTimer.Start();
}
else if (txt.IndexOf("ascending") >= 0)
{
SpeakAndLabel("Ascending coordinate order");
Expand Down Expand Up @@ -393,7 +438,12 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
else if (txt.IndexOf("click") >= 0)
{
ClickMouse();
SpeakAndLabel("click");
win.lStatus.Content = "CLICK";
//SpeakAndLabel("Placed!","Click");
}
else if (txt.IndexOf("repeat") >= 0)
{
SpeakAndLabel(lastSpoken);
}
else if (txt.IndexOf("troubleshoot") >= 0)
{
Expand All @@ -419,7 +469,16 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)

string[] tokens = txt.Split(' ');
if (tokens.Length <= 1) return;
int vindex = int.Parse(tokens[1].Trim());//txt.Substring(1).Trim()
int vindex;
try
{
vindex = int.Parse(tokens[1].Trim());//txt.Substring(1).Trim()
}
catch (Exception err)
{
//not a valid coord;
return;
}

if (vindex>0 && vindex<=boardSize && hindex>0 && hindex<=boardSize)
{
Expand All @@ -432,7 +491,9 @@ static void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
if (enableReadback)
{
sre.SpeechRecognized -= sre_SpeechRecognized;
ss.Speak(txt.Replace("AIY", "A"));
string spk = txt.Replace("AIY", "A");
ss.Speak(spk);
lastSpoken = spk;
sre.SpeechRecognized += sre_SpeechRecognized;
}
}
Expand Down Expand Up @@ -469,6 +530,7 @@ private static void SaveSettings()
s += bottomRight.Y + "\n";
s += enableHotkeys + "\n";
s += enableOpponentColorRead + "\n";
//s += enableExcitedVoice + "\n";

File.WriteAllText(path + SETTINGS_FILE, s);
}
Expand All @@ -491,6 +553,7 @@ private static void ReadSettingLine(int i, string[] lines)
case 9: bottomRight.Y = double.Parse(lines[i]); break;
case 10: enableHotkeys = bool.Parse(lines[i]); break;
case 11: enableOpponentColorRead = bool.Parse(lines[i]); break;
//case 12: enableExcitedVoice = bool.Parse(lines[i]); break;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Ensure your microphone is plugged in and working as the default Windows recordin
1. Say any horizontal coordinate followed by a vertical number coordinate. Example: `C 16` or `B 3`. You can also use the phonetic alphabet to help the system understand your commands. Example: `Charlie 16` or `Bravo 3`.
2. By default, you must then say `Click` to place the stone on the board. See the options below.

Optional: (Experimental) Say `START GAME AS BLACK` or `START GAME AS WHITE` when you begin a game and it will attempt to read your opponents move coordinates by scanning the screen. If computer performance is poor while reading moves, try shrinking your board window.
Optional: (Experimental) Say `START GAME AS BLACK` or `START GAME AS WHITE`, or `ENABLE SPECTATOR MODE` when you begin a game and it will attempt to read your opponents move coordinates by scanning the screen. If computer performance is poor while reading moves, try shrinking your board window.

### Frequently Used Options
* `Enable Auto Play` - Automatically click the mouse after every movement on the grid. This is particularly useful if you are playing a blitz game, but can cause misplays if the speech engine doesn't detect a coordinate properly.
Expand Down
20 changes: 15 additions & 5 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ BOARD SIZE 9 - Set board size to 9x9
DISABLE AUTO - (default) Disables auto-click after moving cursor
ENABLE AUTO - Enables auto-click after moving cursor

DISABLE HOT KEYS - Disables use of arrows to move cursor, Tab to click
ENABLE HOT KEYS - (default) Enables use of arrows to move cursor, Tab to click

DISABLE I COORDINATE - (default) Disables processing 'I' as a valid horizontal coordinate
ENABLE I COORDINATE - Enables processing 'I' as a valid horizontal coordinate

Expand All @@ -33,8 +30,21 @@ FEMALE VOICE - Set computer readback voice to female

- Experimental -
START GAME AS BLACK - Start reading opponent's move coordinates
START GAME AS WHITE - Start reading opponent's move coordinates
START GAME AS WHITE - Start reading opponent's move coordinates
STOP GAME - Stop reading back opponents moves

ENABLE SPECTATOR MODE - Start reading all detected move coordinates
DISABLE SPECTATOR MODE - Stop reading any moves

REPEAT - Say the last spoken coordinates again

DISABLE READING OPPONENT COLOR - Don't read the opponent color before each move
ENABLE READING OPPONENT COLOR - (default) Read the opponent color before each move
ENABLE READING OPPONENT COLOR - (default) Read the opponent color before each move

- Hotkeys -
DISABLE HOT KEYS - Disables use of hotkeys
ENABLE HOT KEYS - Enables the use of hotkeys

ARROWS - Move mouse along the grid
TAB, NUMPAD5, BROWSERHOME, BROWSERBACK - Click mouse
END - Toggle speech detection on/off (aka. mute)

0 comments on commit d0d97aa

Please sign in to comment.