PowerShell/Scripts/voice-control.ps1

64 lines
2.0 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-16 16:50:10 +02:00
Executes PowerShell scripts by voice
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2021-10-16 16:50:10 +02:00
This script executes all the PowerShell scripts by voice.
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\voice-control.ps1
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2021-01-05 13:55:54 +01:00
#>
2021-01-09 12:21:23 +01:00
[double]$MinConfidence = 0.04
2021-01-05 14:33:12 +01:00
$PathToRepo = "$PSScriptRoot/.."
2021-01-09 11:40:04 +01:00
try {
2021-01-09 12:21:23 +01:00
write-output "Init speech recognition engine..."
2021-01-09 16:40:46 +01:00
[system.threading.thread]::currentthread.currentculture=[system.globalization.cultureinfo]"en-US"
2021-01-09 15:59:37 +01:00
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Speech")
2021-01-09 11:40:04 +01:00
$speechRecogEng = [System.Speech.Recognition.SpeechRecognitionEngine]::new()
#$speechRecogEng.InitialSilenceTimeout = 15
$speechRecogEng.SetInputToDefaultAudioDevice()
2021-01-05 13:55:54 +01:00
2021-01-09 12:21:23 +01:00
write-output "Learn script files..."
2021-01-09 11:40:04 +01:00
$Items = get-childItem -path "$PathToRepo/Scripts/"
$Count = 0
foreach ($Item in $Items) {
$Name = $Item.Name.Split('.')[0]
if ($Name -eq "") { continue }
$grammar1 = [System.Speech.Recognition.GrammarBuilder]::new()
$grammar1.Append($Name)
$speechRecogEng.LoadGrammar($grammar1)
$Count++
2021-01-05 13:55:54 +01:00
}
2021-01-09 12:21:23 +01:00
write-output "Learn internal command (exit)..."
2021-01-09 11:40:04 +01:00
$grammar2 = [System.Speech.Recognition.GrammarBuilder]::new()
$grammar2.Append("exit")
$speechRecogEng.LoadGrammar($grammar2)
2021-01-09 15:59:37 +01:00
write-output "NOTE: make sure the microphone volume is not too silent or too loud!"
2021-01-09 12:21:23 +01:00
write-output "Listening now ($Count voice commands)..."
while ($true) {
2021-01-09 11:40:04 +01:00
$recognized = $speechRecogEng.Recognize()
2021-01-09 12:21:23 +01:00
if ($recognized.confidence -lt $MinConfidence) { write-host -noNewline "?"; continue }
2021-01-09 11:40:04 +01:00
$myWords = $recognized.text
2021-01-09 12:21:23 +01:00
foreach ($Item in $Items) {
$Name = $Item.Name.Split('.')[0]
if ($Name -eq $myWords) {
write-host "$Name ($($recognized.confidence) %)..."
& "./$Item"
break
}
2021-01-09 11:40:04 +01:00
}
2021-01-09 12:21:23 +01:00
if ($myWords -match "exit") { write-host -noNewline "$Name ($($recognized.confidence) %)"; break }
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-01-09 11:40:04 +01:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-01-09 11:40:04 +01:00
exit 1
}