Added voice-control.ps1

This commit is contained in:
Markus Fleschutz
2021-01-05 13:55:54 +01:00
parent 502b3ca7ca
commit c22f7460d2
4 changed files with 48 additions and 150 deletions

46
Scripts/voice-control.ps1 Executable file
View File

@ -0,0 +1,46 @@
#!/snap/bin/powershell
<#
.SYNTAX ./voice-control.ps1
.DESCRIPTION executes the PowerShell scripts by voice
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
write-output "Initializing the speech recognition engine..."
$speechRecogEng = [System.Speech.Recognition.SpeechRecognitionEngine]::new();
$speechRecogEng.InitialSilenceTimeout = 15
$speechRecogEng.SetInputToDefaultAudioDevice();
write-output "Loading the grammar..."
$grammar1 = [System.Speech.Recognition.GrammarBuilder]::new();
$grammar1.Append("hello");
$speechRecogEng.LoadGrammar($grammar1);
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new();
$grammar2.Append("open notepad");
$speechRecogEng.LoadGrammar($grammar2);
$grammar3 = [System.Speech.Recognition.GrammarBuilder]::new();
$grammar3.Append("exit");
$speechRecogEng.LoadGrammar($grammar3);
write-output "Listening now..."
$done = $false;
while (!$done) {
$recognized = $speechRecogEng.Recognize()
write-host -nonewline "."
if ($recognized.confidence -lt 0.30) { continue }
$myWords = $recognized.text
write-host -nonewline "$myWords ($($recognized.confidence) %)"
if ($myWords -match "hello") {
continue
}
if ($myWords -match "open notepad") {
continue
}
if ($myWords -match "exit") {
$done = $true
}
}
exit 0