Skip to content

Commit

Permalink
Added push to talk hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
topher-au committed Aug 13, 2015
1 parent 9dd64b8 commit bf726e2
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 49 deletions.
2 changes: 1 addition & 1 deletion hsVoiceCommands/HDT-Voice.vb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Public Class HDTVoice
Get
Dim pluginMenu As New MenuItem
AddHandler pluginMenu.Click, AddressOf OnButtonPress
pluginMenu.Header = New String("HDT-Voice Settings...")
pluginMenu.Header = New String("HDT-Voice settings...")
Return pluginMenu
End Get
End Property
Expand Down
2 changes: 1 addition & 1 deletion hsVoiceCommands/HDT-Voice.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<PropertyGroup>
<PostBuildEvent>copy /Y "$(TargetPath)" "D:\Games\Hearthstone\Hearthstone Deck Tracker\plugins"</PostBuildEvent>
<PostBuildEvent>copy /Y "$(TargetPath)" "C:\Hearthstone\Hearthstone Deck Tracker\plugins"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
12 changes: 12 additions & 0 deletions hsVoiceCommands/My Project/Settings.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hsVoiceCommands/My Project/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
<Setting Name="autoListen" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="toggleOrPTT" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 3 additions & 0 deletions hsVoiceCommands/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<setting name="autoListen" serializeAs="String">
<value>True</value>
</setting>
<setting name="toggleOrPTT" serializeAs="String">
<value>False</value>
</setting>
</HDTVoice.My.MySettings>
</userSettings>
<Application.Resources>
Expand Down
89 changes: 47 additions & 42 deletions hsVoiceCommands/hsVoicePlugin.vb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Public Class hsVoicePlugin
hsRecog.SetInputToDefaultAudioDevice()
hsRecog.BabbleTimeout = New TimeSpan(0, 0, 3)
hsRecog.InitialSilenceTimeout = New TimeSpan(0, 0, 3)
hsRecog.EndSilenceTimeout = New TimeSpan(0, 0, 0, 0, 500)
hsRecog.EndSilenceTimeoutAmbiguous = New TimeSpan(0, 0, 0, 0, 500)
writeLog("Successfuly started speech recognition")
Catch ex As Exception
writeLog("Error initializing speech recognition: " & ex.Message)
Expand Down Expand Up @@ -160,11 +162,23 @@ Public Class hsVoicePlugin
hotkeyWorker.RunWorkerAsync() 'Start listening for hotkey
grammarReloader.RunWorkerAsync()

ToggleSpeech()
If My.Settings.autoListen Then sreListen = True

hsRecog.LoadGrammar(buildGrammar)
hsRecog.RecognizeAsync(RecognizeMode.Multiple)
End Sub ' Run when the plugin is first initialized

'Speech recognition events
Public Sub onSpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles hsRecog.SpeechRecognized


actionInProgress = True

If Not sreListen Then
actionInProgress = False
Exit Sub
End If

'If hearthstone is inactive, exit
If Not checkActiveWindow() Then
writeLog("Heard command """ & e.Result.Text & """ but Hearthstone was inactive")
Expand All @@ -182,15 +196,6 @@ Public Class hsVoicePlugin
updateStatusText("Heard """ & e.Result.Text & """")
writeLog("Command recognized """ & e.Result.Text & """ - executing action")

Do While actionInProgress
'Loop if another command is executing or we get BUGS
Sleep(1)
Loop

actionInProgress = True
Dim sreReset = sreListen
If sreReset Then ToggleSpeech()

'start command processing

If Debugger.IsAttached Then 'debug only commands
Expand Down Expand Up @@ -261,7 +266,6 @@ Public Class hsVoicePlugin

lastCommand = e.Result.Text 'set last command executed
hsRecog.RequestRecognizerUpdate() 'request grammar update
If sreReset Then ToggleSpeech()
actionInProgress = False 'end command processing

End Sub ' Handles processing recognized speech input
Expand Down Expand Up @@ -300,9 +304,9 @@ Public Class hsVoicePlugin
End Sub
Public Sub onResetTimer() Handles timerReset.Tick
If sreListen = True Then
updateStatusText("Listening... (F12 to stop)")
updateStatusText("Listening...")
Else
updateStatusText("Stopped (Press F12)")
updateStatusText("Stopped")
End If

timerReset.Enabled = False
Expand Down Expand Up @@ -524,7 +528,6 @@ Public Class hsVoicePlugin
End Sub 'handle hero powers
Private Sub doAttack(e As SpeechRecognizedEventArgs)
If e.Result.Semantics.ContainsKey("opposing") Then ' Target is a minion

Dim myMinion = e.Result.Semantics("friendly").Value
Dim targetMinion = e.Result.Semantics("opposing").Value
moveCursorToEntity(myMinion, True)
Expand Down Expand Up @@ -800,7 +803,9 @@ Public Class hsVoicePlugin
End If
hdtStatus.Text = newStatus
timerReset.Enabled = True

End Sub)
overlayCanvas.UpdateLayout
Catch ex As Exception
Return
End Try
Expand Down Expand Up @@ -1250,36 +1255,36 @@ Public Class hsVoicePlugin
Public Sub hotkeyWorker_DoWork() Handles hotkeyWorker.DoWork

Do

Dim hotkeyState As Short = GetAsyncKeyState(Keys.F12)
If hotkeyState = 1 Or hotkeyState = Int16.MinValue Then
ToggleSpeech()
Sleep(500)
End If
Sleep(10)
Loop

End Sub
Public Sub ToggleSpeech()
If sreListen Then

sreListen = False
hsRecog.RecognizeAsyncCancel()
If Not actionInProgress Then _
updateStatusText("Stopped")
Else
sreListen = True
If hsRecog.Grammars.Count = 0 Then
hsRecog.LoadGrammar(buildGrammar)
Dim toggleHotkey = Keys.F12
Dim pttHotkey = Keys.LShiftKey

If My.Settings.toggleOrPTT Then ' Push-to-talk
Dim hotkeyState As Short = GetAsyncKeyState(pttHotkey)
If hotkeyState <> 0 Then
sreListen = True
updateStatusText("Listening...")
Do While hotkeyState <> 0
Sleep(1)
hotkeyState = GetAsyncKeyState(pttHotkey)
Loop
updateStatusText("Processing...")
Do While hsRecog.AudioState = AudioState.Speech
Sleep(1)
Loop
Sleep(500)
sreListen = False
End If
Else ' Toggle
Dim hotkeyState As Short = GetAsyncKeyState(toggleHotkey)
If hotkeyState <> 0 Then
sreListen = Not sreListen
updateStatusText(Nothing)
Sleep(200)
End If
End If
Try
hsRecog.RecognizeAsync(RecognizeMode.Multiple)
If Not actionInProgress Then _
updateStatusText("Started")
Catch ex As Exception

End Try
Sleep(50)
Loop

End If
End Sub
End Class
20 changes: 15 additions & 5 deletions hsVoiceCommands/metroConfig.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@
</Menu>
</TabItem>
<TabItem Header="recognizer">
<Menu>
<StackPanel>
<Label Content="Recognizer threshold" />
<Slider Name="slideThreshold" Width="200" Minimum="20" Maximum="95" Interval="5" ValueChanged="slideThreshold_ValueChanged"/>
<Label Name="labelThreshold" Content="0%" />
<CheckBox Name="checkQuickPlay" Content="Enable Quick Play" />
</Menu>
<Menu>
<Slider Name="slideThreshold" Width="200" Minimum="20" Maximum="95" Interval="5" ValueChanged="slideThreshold_ValueChanged"/>
<Label Name="labelThreshold" Content="0%" />
</Menu>
<CheckBox Name="checkQuickPlay" Content="Enable Quick Play" Margin="4"/>
<Menu>
<StackPanel>
<Label Content="Recognizer Hotkey (F12)" />
<RadioButton Name="radioToggle" Content="Toggle (F12)" Margin="4"/>
<RadioButton Name="radioPTT" Content="Push-to-Talk (Left Shift)" Margin="4"/>
</StackPanel>
</Menu>

</StackPanel>
</TabItem>
</TabControl>
<Menu Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Bottom" >
Expand Down
11 changes: 11 additions & 0 deletions hsVoiceCommands/metroConfig.xaml.vb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ Partial Class MetroConfig
slideThreshold.Value = My.Settings.Threshold
checkQuickPlay.IsChecked = My.Settings.quickPlay

Select Case My.Settings.toggleOrPTT
Case True
radioPTT.IsChecked = True

Case False
radioToggle.IsChecked = True
End Select

End Sub

Private Sub Button_Click(sender As Object, e As System.Windows.RoutedEventArgs)
Expand All @@ -43,6 +51,9 @@ Partial Class MetroConfig

My.Settings.Threshold = slideThreshold.Value
My.Settings.quickPlay = checkQuickPlay.IsChecked

My.Settings.toggleOrPTT = radioPTT.IsChecked
My.Settings.Save()
Me.Close()
End Sub

Expand Down

0 comments on commit bf726e2

Please sign in to comment.