yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
5638d86
v0.19.1
1#!/usr/bin/env python3 2# python3 -m pip install python-osc pillow 3 4from math import ceil 5from paging import MultiLinePager 6from pythonosc import udp_client 7 8import generate_utils 9import osc_ctrl 10import time 11 12class AppConfig : 13def __init__ ( self , 14rows : int = 4 , 15cols : int = 40 , 16chars_per_sync : int = 10 , 17osc_sync_rate_hz : int = 3 ) : 18self . rows = rows 19self . cols = cols 20self . chars_per_sync = chars_per_sync 21self . osc_sync_rate_hz = osc_sync_rate_hz 22self . client = osc_ctrl . getClient () 23 24def encodeMessage ( msg ) : 25encoded = [] 26for char in msg : 27encoded . append ( ord ( char )) 28return encoded 29 30class OSCSyncHelper : 31def __init__ ( self , 32config : AppConfig ) : 33self . sync_delay_s = 1.0 / config . osc_sync_rate_hz 34self . last = time . time () - self . sync_delay_s 35 36def waitForSync ( self ) -> None : 37# sleep() can sleep for too short a time, so use a loop to ensure that 38# we sleep at least a full sync window's worth of time. 39while time . time () - self . last < self . sync_delay_s : 40time . sleep ( 0.01 ) 41self . last = time . time () 42 43def sendMessage ( msg : str , cfg : AppConfig , osc : OSCSyncHelper ) -> None : 44num_cells = cfg . rows * cfg . cols 45num_regions = ceil ( num_cells / cfg . chars_per_sync ) 46 47pager = MultiLinePager ( cfg . chars_per_sync , cfg . rows , cfg . cols ) 48 49# Show the chatbox 50osc . waitForSync () 51osc_ctrl . toggleBoard ( cfg . client , True ) 52osc_ctrl . ellipsis ( cfg . client , False ) 53osc_ctrl . disable ( cfg . client ) 54 55# Ensure that the chatbox is cleared. 56addr = "/avatar/parameters/" + generate_utils . getClearBoardParam () 57cfg . client . send_message ( addr , True ) 58osc . waitForSync () 59cfg . client . send_message ( addr , False ) 60 61slice_idx = 0 62while slice_idx != - 1 : 63 64msg_slice , slice_idx = pager . getNextSlice ( msg ) 65which_region = slice_idx % num_regions 66 67( f"Sending slice ' { msg_slice } ' to region { which_region } " ) 68 69# Wait until OSC has had enough time to sync the previous window of 70# data. 71osc . waitForSync () 72 73# Enable chatbox animations. 74osc_ctrl . enable ( cfg . client ) 75 76# Seek to the current region. 77addr = "/avatar/parameters/" + generate_utils . getSelectParam () 78cfg . client . send_message ( addr , which_region ) 79 80# Send all characters in the current region. 81encoded = encodeMessage ( msg_slice ) 82for i in range ( 0 , len ( msg_slice )) : 83( f"Sending char { msg_slice [ i ] } / { encoded [ i ] } " ) 84addr = "/avatar/parameters/" + generate_utils . getBlendParam ( i , 0 ) 85letter_remapped = ( - 127.5 + encoded [ i ]) / 127.5 86cfg . client . send_message ( addr , letter_remapped ) 87 88# Disable chatbox animations to ensure stability. 89osc . waitForSync () 90osc_ctrl . disable ( cfg . client ) 91 92if __name__ == "__main__" : 93cfg = AppConfig () 94osc = OSCSyncHelper ( cfg ) 95sendMessage ( "Hello, world! aiueo aiueo aiueo aiueo aiueo eeeeeeeeeeeeeeeeeeeeeeee" , cfg , osc ) 96