yum-slop/TaSTT

Free self-hosted STT for VRChat.

git clone https://git.yummers.dev/yum-slop/TaSTT

yumBugfix: fix process leak in PythonWrapper::InvokeCommandWithArgs9a18326

v0.21.0
2.7 KiB89 linesraw
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:
14    def __init__(self,
15            opcode: int):
16        self.opcode = opcode
17
18# Checks if the given button on the given controller is pressed.
19def pollButtonPress(
20        hand: str = "right",
21        button: str = "b",
22        ctrl = None  # ThreadControl object
23        ) -> int:
24    hands = {}
25    hands["left"] = vr.TrackedControllerRole_LeftHand
26    hands["right"] = vr.TrackedControllerRole_RightHand
27
28    buttons = {}
29    buttons["a"] = vr.k_EButton_IndexController_A
30    buttons["b"] = vr.k_EButton_IndexController_B
31    buttons["thumbstick"] = vr.k_EButton_Axis0
32
33    system = None
34    first = True
35    while ctrl.run_app and not system:
36        try:
37            system = vr.init(vr.VRApplication_Background)
38        except Exception as e:
39            if first:
40                print(f"Failed to start steamVR input thread: {repr(e)}", file=sys.stderr)
41            first = False
42            time.sleep(1)
43    last_packet = 0
44    event_high = False
45
46    while ctrl.run_app:
47        time.sleep(0.01)
48
49        lh_idx = system.getTrackedDeviceIndexForControllerRole(hands[hand])
50        #print("left hand device idx: {}".format(lh_idx))
51
52        got_state, state = system.getControllerState(lh_idx)
53        if not got_state:
54            continue
55
56        if state.unPacketNum == last_packet:
57            continue
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.
63        dead_zone_radius = 0.7
64
65        button_mask = (1 << buttons[button])
66        ret = EVENT_NONE
67        if (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))
72            if not event_high:
73                yield InputEvent(EVENT_RISING_EDGE)
74            event_high = True
75        elif event_high:
76            event_high = False
77            yield InputEvent(EVENT_FALLING_EDGE)
78
79if __name__ == "__main__":
80    gen = pollButtonPress()
81    while True:
82        time.sleep(0.1)
83
84        event = pollButtonPress(session_state)
85        if event == EVENT_RISING_EDGE:
86            print("rising edge")
87        elif event == EVENT_FALLING_EDGE:
88            print("falling edge")
89