yum-slop/TaSTT

Free self-hosted STT for VRChat.

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

yumBugfix: C++ transcription engine should not launch OSC layerd0d5eed

v0.21.0
6.8 KiB197 linesraw
1#!/usr/bin/env python3
2
3# The app loop does 2 things:
4#   1. Read lines from stdin and send them into the game via OSC.
5#   2. Write control info to stdout.
6# The app exits when stdin closes.
7
8from playsound import playsound
9
10import argparse
11import dataclasses
12import generate_utils
13import os
14import osc_ctrl
15import steamvr
16import sys
17import threading
18import time
19
20@dataclasses.dataclass
21class AudioState:
22    text: str
23    osc_state: osc_ctrl.OscState
24    enable_local_beep: int
25    use_builtin: int
26    button: str
27
28    send_transcript: bool
29    run_app: bool
30
31def writeControlMessage(run: bool):
32    msg = ""
33    if run:
34        msg += "1"
35    else:
36        msg += "0"
37    print(f"{msg}")
38
39def readControllerInput(audio_state: AudioState):
40    session = None
41    first = True
42    while session == None and audio_state.run_app == True:
43        try:
44            session = steamvr.SessionState()
45        except:
46            print("steamvr is off, no controller input", file=sys.stderr)
47            session = None
48            time.sleep(5)
49
50    RECORD_STATE = 0
51    PAUSE_STATE = 1
52    state = PAUSE_STATE
53    osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
54    osc_ctrl.indicatePaging(audio_state.osc_state.client, False)
55
56    hand_id = steamvr.hands[audio_state.button.split()[0]]
57    button_id = steamvr.buttons[audio_state.button.split()[1]]
58
59    last_rising = time.time()
60    while audio_state.run_app == True:
61        time.sleep(0.05)
62
63        event = steamvr.pollButtonPress(session, hand_id=hand_id,
64                button_id=button_id)
65
66        if event == steamvr.EVENT_RISING_EDGE:
67            last_rising = time.time()
68        elif event == steamvr.EVENT_FALLING_EDGE:
69            now = time.time()
70            if now - last_rising > 0.3:
71                # Long hold
72                state = PAUSE_STATE
73                if not audio_state.use_builtin:
74                    osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
75                    osc_ctrl.toggleBoard(audio_state.osc_state.client, False)
76
77                osc_ctrl.send_transcript = False
78                osc_ctrl.clear(audio_state.osc_state)
79            else:
80                # Short hold
81                if state == RECORD_STATE:
82                    state = PAUSE_STATE
83                    if not audio_state.use_builtin:
84                        osc_ctrl.indicateSpeech(audio_state.osc_state.client, False)
85                        osc_ctrl.lockWorld(audio_state.osc_state.client, True)
86
87                    osc_ctrl.send_transcript = False
88
89                    if audio_state.enable_local_beep == 1:
90                        playsound(os.path.abspath("Resources/Sounds/Noise_Off_Quiet.wav"))
91                elif state == PAUSE_STATE:
92                    state = RECORD_STATE
93                    if not audio_state.use_builtin:
94                        osc_ctrl.indicateSpeech(audio_state.osc_state.client, True)
95                        osc_ctrl.toggleBoard(audio_state.osc_state.client, True)
96                        osc_ctrl.lockWorld(audio_state.osc_state.client, False)
97
98                    osc_ctrl.send_transcript = True
99                    osc_ctrl.clear(audio_state.osc_state)
100
101                    audio_state.drop_transcription = True
102                    audio_state.audio_paused = False
103
104                    if audio_state.enable_local_beep == 1:
105                        playsound(os.path.abspath("Resources/Sounds/Noise_On_Quiet.wav"))
106
107def drainStdin(audio_state: AudioState):
108    while True:
109        try:
110            line = input()
111        except EOFError:
112            # Invoking process closes the write end of their stdin to signal us
113            # to exit.
114            # TODO(yum) merge all threads
115            audio_state.run_app = False
116            return
117        if len(line) > 0:
118            print(f"stdin get: {line}", file=sys.stderr)
119
120def mainLoop(audio_state: AudioState):
121    steamvr_input_thd = threading.Thread(target = readControllerInput,
122            args = [audio_state])
123    steamvr_input_thd.daemon = True
124    steamvr_input_thd.start()
125
126    drain_stdin_thd = threading.Thread(target = drainStdin,
127            args = [audio_state])
128    drain_stdin_thd.daemon = True
129    drain_stdin_thd.start()
130
131    writeControlMessage(False)
132
133    while audio_state.run_app:
134        time.sleep(0.01)
135        writeControlMessage(audio_state.send_transcript)
136
137if __name__ == "__main__":
138    print("args: {}".format(" ".join(sys.argv)), file=sys.stderr)
139
140    # Set cwd to TaSTT/
141    abspath = os.path.abspath(__file__)
142    dname = os.path.dirname(abspath)
143    dname = os.path.dirname(dname)
144    dname = os.path.dirname(dname)
145    os.chdir(dname)
146    print(f"Set cwd to {os.getcwd()}", file=sys.stderr)
147
148    parser = argparse.ArgumentParser()
149    parser.add_argument("--bytes_per_char", type=str, help="The number of bytes to use to represent each character")
150    parser.add_argument("--chars_per_sync", type=str, help="The number of characters to send on each sync event")
151    parser.add_argument("--rows", type=int, help="The number of rows on the board")
152    parser.add_argument("--cols", type=int, help="The number of columns on the board")
153    parser.add_argument("--enable_local_beep", type=int,
154            help=("Whether to play a local auditory indicator when "
155                "transcription starts/stops."))
156    parser.add_argument("--use_builtin", type=int,
157            help=("If set to 1, use the text box built into the game."))
158    parser.add_argument("--button", type=str,
159            help=("The controller button used to start/stop transcription. "
160                "E.g. \"left joystick\""))
161    args = parser.parse_args()
162
163    if args.bytes_per_char is None or args.chars_per_sync is None:
164        print("--bytes_per_char and --chars_per_sync required", file=sys.stderr)
165        sys.exit(1)
166    if args.rows is None or args.cols is None:
167        print("--rows and --cols required", file=sys.stderr)
168        sys.exit(1)
169    if args.button is None:
170        print("--button required", file=sys.stderr)
171        sys.exit(1)
172    if args.enable_local_beep is None:
173        print("--enable_local_beep required", file=sys.stderr)
174        sys.exit(1)
175    if args.use_builtin is None:
176        print("--use_builtin required", file=sys.stderr)
177        sys.exit(1)
178
179    generate_utils.config.BYTES_PER_CHAR = int(args.bytes_per_char)
180    generate_utils.config.CHARS_PER_SYNC = int(args.chars_per_sync)
181    generate_utils.config.BOARD_ROWS = int(args.rows)
182    generate_utils.config.BOARD_COLS = int(args.cols)
183
184    audio_state = AudioState(
185            text = "",
186            osc_state = osc_ctrl.OscState(
187                generate_utils.config.CHARS_PER_SYNC,
188                generate_utils.config.BOARD_ROWS,
189                generate_utils.config.BOARD_COLS),
190            button = args.button,
191            enable_local_beep = args.enable_local_beep,
192            use_builtin = args.use_builtin,
193            send_transcript = False,
194            run_app = True)
195
196    mainLoop(audio_state)
197