yum-slop/TaSTT

Free self-hosted STT for VRChat.

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

yumFix distilled models5638d86

v0.19.1
3.0 KiB96 linesraw
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:
13    def __init__(self,
14            rows: int = 4,
15            cols: int = 40,
16            chars_per_sync: int = 10,
17            osc_sync_rate_hz: int = 3):
18        self.rows = rows
19        self.cols = cols
20        self.chars_per_sync = chars_per_sync
21        self.osc_sync_rate_hz = osc_sync_rate_hz
22        self.client = osc_ctrl.getClient()
23
24def encodeMessage(msg):
25    encoded = []
26    for char in msg:
27        encoded.append(ord(char))
28    return encoded
29
30class OSCSyncHelper:
31    def __init__(self,
32            config: AppConfig):
33        self.sync_delay_s = 1.0 / config.osc_sync_rate_hz
34        self.last = time.time() - self.sync_delay_s
35
36    def 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.
39        while time.time() - self.last < self.sync_delay_s:
40            time.sleep(0.01)
41        self.last = time.time()
42
43def sendMessage(msg: str, cfg: AppConfig, osc: OSCSyncHelper) -> None:
44    num_cells = cfg.rows * cfg.cols
45    num_regions = ceil(num_cells / cfg.chars_per_sync)
46
47    pager = MultiLinePager(cfg.chars_per_sync, cfg.rows, cfg.cols)
48
49    # Show the chatbox
50    osc.waitForSync()
51    osc_ctrl.toggleBoard(cfg.client, True)
52    osc_ctrl.ellipsis(cfg.client, False)
53    osc_ctrl.disable(cfg.client)
54
55    # Ensure that the chatbox is cleared.
56    addr="/avatar/parameters/" + generate_utils.getClearBoardParam()
57    cfg.client.send_message(addr, True)
58    osc.waitForSync()
59    cfg.client.send_message(addr, False)
60
61    slice_idx = 0
62    while slice_idx != -1:
63
64        msg_slice, slice_idx = pager.getNextSlice(msg)
65        which_region = slice_idx % num_regions
66
67        print(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.
71        osc.waitForSync()
72
73        # Enable chatbox animations.
74        osc_ctrl.enable(cfg.client)
75
76        # Seek to the current region.
77        addr="/avatar/parameters/" + generate_utils.getSelectParam()
78        cfg.client.send_message(addr, which_region)
79
80        # Send all characters in the current region.
81        encoded = encodeMessage(msg_slice)
82        for i in range(0, len(msg_slice)):
83            print(f"Sending char {msg_slice[i]} / {encoded[i]}")
84            addr="/avatar/parameters/" + generate_utils.getBlendParam(i, 0)
85            letter_remapped = (-127.5 + encoded[i]) / 127.5
86            cfg.client.send_message(addr, letter_remapped)
87
88    # Disable chatbox animations to ensure stability.
89    osc.waitForSync()
90    osc_ctrl.disable(cfg.client)
91
92if __name__ == "__main__":
93    cfg = AppConfig()
94    osc = OSCSyncHelper(cfg)
95    sendMessage("Hello, world! aiueo aiueo aiueo aiueo aiueo eeeeeeeeeeeeeeeeeeeeeeee", cfg, osc)
96