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