yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
9a18326
v0.20.0
1#!/usr/bin/env python3 2 3# python3 -m pip install openvr 4# License: BSD-3.0 (requires showing notice in binary distributions) 5import openvr as vr 6import sys 7import time 8 9EVENT_NONE = 0 10EVENT_RISING_EDGE = 1 11EVENT_FALLING_EDGE = 2 12 13class InputEvent : 14def __init__ ( self , 15opcode : int ) : 16self . opcode = opcode 17 18# Checks if the given button on the given controller is pressed. 19def pollButtonPress ( 20hand : str = "right" , 21button : str = "b" , 22ctrl = None # ThreadControl object 23) -> int : 24hands = {} 25hands [ "left" ] = vr . TrackedControllerRole_LeftHand 26hands [ "right" ] = vr . TrackedControllerRole_RightHand 27 28buttons = {} 29buttons [ "a" ] = vr . k_EButton_IndexController_A 30buttons [ "b" ] = vr . k_EButton_IndexController_B 31buttons [ "thumbstick" ] = vr . k_EButton_Axis0 32 33system = None 34first = True 35while ctrl . run_app and not system : 36try : 37system = vr . init ( vr . VRApplication_Background ) 38except Exception as e : 39if first : 40( f"Failed to start steamVR input thread: { repr ( e ) } " , file = sys . stderr ) 41first = False 42time . sleep ( 1 ) 43last_packet = 0 44event_high = False 45 46while ctrl . run_app : 47time . sleep ( 0.01 ) 48 49lh_idx = system . getTrackedDeviceIndexForControllerRole ( hands [ hand ]) 50#print("left hand device idx: {}".format(lh_idx)) 51 52got_state , state = system . getControllerState ( lh_idx ) 53if not got_state : 54continue 55 56if state . unPacketNum == last_packet : 57continue 58 59# Clicking joysticks and moving joysticks fire the same events. To 60# differentiate movement from clicking, we create a dead zone: if the event 61# fires while the stick isn't moved far from center, we assume it's a 62# click, not movement. 63dead_zone_radius = 0.7 64 65button_mask = ( 1 << buttons [ button ]) 66ret = EVENT_NONE 67if ( state . ulButtonPressed & button_mask ) != 0 and \ 68( state . rAxis [ 0 ] . x ** 2 + state . rAxis [ 0 ] . y ** 2 < dead_zone_radius ** 2 ) : 69#print("button pressed: %016x" % state.ulButtonPressed) 70#for i in range(0, 5): 71# print("axis {} x: {} y: {}".format(i, state.rAxis[i].x, state.rAxis[i].y)) 72if not event_high : 73yield InputEvent ( EVENT_RISING_EDGE ) 74event_high = True 75elif event_high : 76event_high = False 77yield InputEvent ( EVENT_FALLING_EDGE ) 78 79if __name__ == "__main__" : 80gen = pollButtonPress () 81while True : 82time . sleep ( 0.1 ) 83 84event = pollButtonPress ( session_state ) 85if event == EVENT_RISING_EDGE : 86( "rising edge" ) 87elif event == EVENT_FALLING_EDGE : 88( "falling edge" ) 89