Advertisement
SemlerPDX

AVCS QCC PTT Key Monitor v3 for VoiceAttack

Oct 20th, 2021 (edited)
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.48 KB | None | 0 0
  1. ' Quick Command Creator Inline-Function for VoiceAttack v3 -  Monitor for PTT Button(s) press
  2. '  by SemlerPDX Jan2021/Oct2021
  3. '  VETERANS-GAMING.COM
  4.  
  5. Imports Microsoft.VisualBasic
  6. Imports System
  7. Imports System.Threading
  8. Imports System.Threading.Tasks
  9.  
  10.  
  11. Public Class VAInline
  12.     dim checkKeyDown as string = ""
  13.     dim checkPovDir() as string
  14.     dim checkPOV as string = ""
  15.    
  16.     dim masterMode as boolean = false
  17.     dim listenAwake as boolean = false
  18.     dim listeningCanWake as boolean = false
  19.     dim listeningAltered as boolean = false
  20.     dim keyDown as boolean = false
  21.     dim keyAlwaysDown as boolean = false
  22.     dim keyMonitoring as boolean = true
  23.     dim keyMonitorActive as boolean = true
  24.     dim keyMonitorType as integer = 0
  25.    
  26.     dim listeningInterval as integer = 5500
  27.     dim keyPollingInterval as integer = 100
  28.    
  29.     dim activeProfile as string = "CORE"
  30.    
  31.     dim debugCheck as boolean
  32.     dim debugCount as integer = 0
  33.    
  34.    
  35.     Private Function CheckPTTkeyDownState(ByRef keyDown as boolean)
  36.         for keyIndex as integer = 1 to 6
  37.             keyAlwaysDown = false
  38.             'Check only if this PTT button has been set
  39.             if ((VA.GetText("AVCS_" + activeProfile + "_PTTBUTTON_" + keyIndex.ToString())) isNot nothing)
  40.                 checkKeyDown = VA.GetText("AVCS_" + activeProfile + "_PTTBUTTON_" + keyIndex.ToString())
  41.                 'Check if this is a reverse-PTT (push-to-mute)
  42.                 if ((VA.GetText("AVCS_" + activeProfile + "_PTTREVERSE_" + keyIndex.ToString())) isNot nothing)
  43.                     if ((VA.GetText("AVCS_" + activeProfile + "_PTTREVERSE_" + keyIndex.ToString())) = "reverse")
  44.                         keyAlwaysDown = true
  45.                     end if
  46.                 end if
  47.                 'Attempt to check 'Trigger' type button state
  48.                 if (checkKeyDown.EndsWith("TRIGGER"))
  49.                     if (IsNumeric(VA.Utility.ParseTokens("{" + checkKeyDown + "}")))
  50.                         if ((keyAlwaysDown) and (Convert.ToInt32(VA.Utility.ParseTokens("{" + checkKeyDown + "}")) < 20))
  51.                             keyDown = true
  52.                         end if
  53.                         if ((not(keyAlwaysDown)) and (Convert.ToInt32(VA.Utility.ParseTokens("{" + checkKeyDown + "}")) > 20))
  54.                             keyDown = true
  55.                         end if
  56.                     end if
  57.                 elseif (checkKeyDown.Contains("POV"))
  58.                     'Check PTT POV Directional state
  59.                     checkPovDir = checkKeyDown.Split(":")
  60.                     if ((keyAlwaysDown) and ((VA.Utility.ParseTokens("{" + checkPovDir(0) + "}")) = "CENTER"))
  61.                         keyDown = true
  62.                     end if
  63.                     if ((not(keyAlwaysDown)) and (VA.Utility.ParseTokens("{" + checkPovDir(0) + "}") <> "CENTER"))
  64.                         if (VA.Utility.ParseTokens("{" + checkPovDir(0) + "}") = checkPovDir(1))
  65.                             keyDown = true
  66.                         end if
  67.                     end if
  68.                    
  69.                 else
  70.                     'Check PTT key/button state
  71.                     if ((keyAlwaysDown) and ((VA.Utility.ParseTokens("{" + checkKeyDown + "}")) = "0"))
  72.                         keyDown = true
  73.                     end if
  74.                     if ((not(keyAlwaysDown)) and ((VA.Utility.ParseTokens("{" + checkKeyDown + "}")) = "1"))
  75.                         keyDown = true
  76.                     end if
  77.                 end if
  78.             end if
  79.             'Stop checking if a button/key down state has been set for return
  80.             if (keyDown)
  81.                 Exit For
  82.             end if
  83.         next
  84.     end function
  85.    
  86.     Private Function PTTStartListening(ByVal keyMonitorType as integer)
  87.         'Start Listening check with delay to eliminate false positives/negatives
  88.         if (keyMonitorType > 0)
  89.             Thread.CurrentThread.Sleep(150)
  90.             if (not(VA.State.GetListeningEnabled))
  91.                 VA.State.SetListeningEnabled(true)
  92.             end if
  93.         end if
  94.     end function
  95.    
  96.     Private Function PTTStopListening(ByVal keyMonitorType as integer)
  97.         'Stop Listening check with delay to eliminate false positives/negatives
  98.         if (keyMonitorType > 0)
  99.             Thread.CurrentThread.Sleep(500)
  100.             if (VA.State.GetListeningEnabled)
  101.                 VA.State.SetListeningEnabled(false)
  102.             end if
  103.         end if
  104.     end function
  105.    
  106.     Private Function SendDebugMessage(ByVal debugText as string, ByVal debugColor as integer)
  107.         if ((VA.GetText("AVCS_Debug_QMSG")) isNot nothing)
  108.             debugCount = VA.GetInt("AVCS_Debug_QMSG")
  109.         end if
  110.         debugCount += 1
  111.         VA.SetText("AVCS_Debug_TMSG_" + debugCount.ToString(), debugText)
  112.         VA.SetInt("AVCS_Debug_WCLR_" + debugCount.ToString(), debugColor)
  113.         VA.SetInt("AVCS_Debug_QMSG", debugCount)
  114.         try
  115.             VA.Command.Execute("f_core_debug -log", true)
  116.         catch
  117.             VA.WriteToLog("AVCS ERROR: PTTMON01 - f_core_debug null - restart VoiceAttack or create bug report", "red")
  118.         end try
  119.         debugCount = 0
  120.     end function
  121.    
  122.    
  123.     Public Sub Main()
  124.         if ((VA.GetText("AVCS_ACTIVE_PROFILE")) isNot nothing)
  125.             activeProfile = VA.GetText("AVCS_ACTIVE_PROFILE")
  126.         end if
  127.        
  128.         'Listening Wake - "Wait for spoken response" works when listening disabled; if that ever changes, set this bool to TRUE to enable hotfix below
  129.         if ((VA.GetInt("AVCS_" + activeProfile + "_PTT_CANWAKE")) isNot nothing)
  130.             listeningCanWake = VA.GetInt("AVCS_" + activeProfile + "_PTT_CANWAKE")
  131.         end if
  132.        
  133.         if ((VA.GetInt("AVCS_" + activeProfile + "_PTT_MODE")) isNot nothing)
  134.             keyMonitorType = VA.GetInt("AVCS_" + activeProfile + "_PTT_MODE")
  135.         end if
  136.        
  137.         if ((VA.GetInt("AVCS_" + activeProfile + "_TimeUntilStopListening")) isNot nothing)
  138.             listeningInterval = VA.GetInt("AVCS_" + activeProfile + "_TimeUntilStopListening")
  139.         end if
  140.        
  141.         if ((VA.GetInt("AVCS_" + activeProfile + "_DevicePollingPTT")) isNot nothing)
  142.             keyPollingInterval = VA.GetInt("AVCS_" + activeProfile + "_KeyPollingIntervalPTT")
  143.         end if
  144.        
  145.         'Main Mode Check - call this command from any init, if running it will exit before re-entering, or not at all (if unset)
  146.         if ((VA.GetBoolean("AVCS_" + activeProfile + "_RadioButtons_ON")) isNot nothing)
  147.             masterMode = VA.GetBoolean("AVCS_" + activeProfile + "_RadioButtons_ON")
  148.             if (masterMode)
  149.                 'Prepare Messages for AVCS Debug system to choose where to send them (event log/file/both)
  150.                 if ((VA.ParseTokens("{BOOL:AVCS_Debug_ON:false}")) = true)
  151.                     debugCheck = true                  
  152.                     SendDebugMessage("AVCS QCC PTT KeyMonitor Loop Entered",2)
  153.                 end if
  154.                
  155.                 'Stagger Delay for any existing loop to exit before new loop instance starts
  156.                 Thread.CurrentThread.Sleep(600)
  157.                 VA.SetBoolean("AVCS_RadioButtons_ON", true)
  158.                 VA.SetBoolean("AVCS_PTT_MODE_ON", true)
  159.                
  160.                 Thread.CurrentThread.Sleep(keyPollingInterval)
  161.                 PTTStopListening(keyMonitorType)
  162.                
  163.                 'MAIN Monitor Loop
  164.                 while (keyMonitorActive)
  165.                     'Exit Check - allows staggered exit and 'only one instance' of this loop
  166.                     if ((VA.ParseTokens("{BOOL:AVCS_RadioButtons_ON:false}")) = false)
  167.                         keyMonitorType = 1
  168.                         keyMonitorActive = false
  169.                         PTTStartListening(keyMonitorType)
  170.                         Exit While
  171.                     end if
  172.                    
  173.                     'Pausing Check - allows pausing the functionality of the main loop without exiting
  174.                     if ((VA.ParseTokens("{BOOL:AVCS_PTT_MODE_ON:false}")) = true)
  175.                         keyMonitoring = true
  176.                     else
  177.                         keyMonitoring = false
  178.                     end if
  179.                    
  180.                     Thread.CurrentThread.Sleep(keyPollingInterval)
  181.                    
  182.                     if (keyMonitoring)
  183.                        
  184.                         'Keydown Check once per interval
  185.                         keyDown = false
  186.                         CheckPTTkeyDownState(keyDown)
  187.                        
  188.                         'Listening Wake function - if "Wait for spoken response" ever respects global listening, this this will be needed
  189.                         if (not(keyDown))
  190.                             if (listeningCanWake)
  191.                                 if ((VA.ParseTokens("{BOOL:AVCS_QCC_LISTENING:false}")) = true)
  192.                                     keyDown = true
  193.                                     VA.SetBoolean("AVCS_QCC_LISTENING", nothing)
  194.                                 end if
  195.                             end if
  196.                         end if
  197.                        
  198.                         'Wake by Name Check once per interval
  199.                         if (not(keyDown))
  200.                             if ((VA.ParseTokens("{BOOL:AVCS_QCC_AWAKE:false}")) = true)
  201.                                 keyDown = true
  202.                                 listenAwake = true
  203.                                 VA.SetBoolean("AVCS_QCC_AWAKE", nothing)
  204.                             end if
  205.                         end if
  206.                        
  207.                         if (keyDown)
  208.                             VA.SetBoolean("AVCS_PTT_KeyDown", true)
  209.                             VA.SetBoolean("AVCS_" + activeProfile + "_PTT_KeyDown", true)
  210.                            
  211.                             PTTStartListening(keyMonitorType)
  212.                            
  213.                             if (debugCheck)
  214.                                 SendDebugMessage("AVCS PTT KEY PRESS / WAKE DETECTED",2)
  215.                             end if
  216.                            
  217.                             'If Listen Wake Hotfix enabled, allow VAS Loop to increase default wait for response here
  218.                             if (listeningCanWake)
  219.                                 if ((VA.GetDecimal("AVCS_VAS_TIMEOUT")) isNot nothing)
  220.                                     listeningInterval = 8500
  221.                                     listeningAltered = true
  222.                                 end if
  223.                             end if
  224.                            
  225.                             'Secondary Loop - Wait until done / stop listening
  226.                             for i as integer = 0 to listeningInterval step 500
  227.                                 Thread.CurrentThread.Sleep(500)
  228.                                
  229.                                 if (i >= listeningInterval)
  230.                                     Exit For
  231.                                 end if
  232.                                
  233.                                 'Secondary Exit Check - get out if told to exit instance
  234.                                 if ((VA.ParseTokens("{BOOL:AVCS_RadioButtons_ON:false}")) = false)
  235.                                     keyMonitorType = 1
  236.                                     keyMonitorActive = false
  237.                                     PTTStartListening(keyMonitorType)
  238.                                     Exit While
  239.                                 end if
  240.                                
  241.                                 'Increase waiting time by resetting counter upon unrecognized speech or repeated Wake Name use
  242.                                 if ((VA.ParseTokens("{BOOL:AVCS_QCC_AWAKE:false}")) = true)
  243.                                     i = 0
  244.                                     listenAwake = true
  245.                                     VA.SetBoolean("AVCS_QCC_AWAKE", nothing)
  246.                                 end if
  247.                                
  248.                                 'If wait loop entered via keyDown state detection, allow immediate on/off of Global Listening based on key state
  249.                                 if (not(listenAwake))
  250.                                     keyDown = false
  251.                                     CheckPTTkeyDownState(keyDown)
  252.                                     if (keyDown)
  253.                                         i = 0
  254.                                         PTTStartListening(keyMonitorType)
  255.                                     else
  256.                                         PTTStopListening(keyMonitorType)
  257.                                     end if
  258.                                 end if
  259.                             next
  260.                            
  261.                             'Reset all function variables to begin loop again
  262.                             VA.SetBoolean("AVCS_PTT_KeyDown", false)
  263.                             VA.SetBoolean("AVCS_" + activeProfile + "_PTT_KeyDown", false)
  264.                            
  265.                             PTTStopListening(keyMonitorType)
  266.                            
  267.                             keyDown = false
  268.                             listenAwake = false
  269.                            
  270.                             if ((listeningCanWake) and (listeningAltered))
  271.                                 listeningAltered = false
  272.                                 listeningInterval = 5500
  273.                                 if ((VA.GetInt("AVCS_" + activeProfile + "_TimeUntilStopListening")) isNot nothing)
  274.                                     listeningInterval = VA.GetInt("AVCS_" + activeProfile + "_TimeUntilStopListening")
  275.                                 end if
  276.                             end if
  277.                            
  278.                             if (debugCheck)
  279.                                 SendDebugMessage("AVCS PTT LISTENING INTERVAL COMPLETE",4)
  280.                             end if
  281.                         end if
  282.                        
  283.                     end if
  284.                    
  285.                 end while
  286.                
  287.                 VA.SetBoolean("AVCS_PTT_KeyDown", false)
  288.                 VA.SetBoolean("AVCS_" + activeProfile + "_PTT_KeyDown", false)
  289.                
  290.                 if (debugCheck)
  291.                     SendDebugMessage("AVCS QCC PTT KeyMonitor Loop ended",2)
  292.                 end if
  293.             else
  294.                 'Cancel - when lauched but PTT mode is not enabled for this profile
  295.                 keyMonitorType = 1
  296.                 PTTStartListening(keyMonitorType)
  297.             end if
  298.         end if
  299.        
  300.     End Sub
  301.  
  302. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement