yum-slop/TaSTT

Free self-hosted STT for VRChat.

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

yumVerify that audio is clean after VAD segmentationacccf8e

v0.20.0
6.4 KiB184 linesraw
1#!/usr/bin/env python3
2
3# python3 -m pip install pillow
4# License: HPND license.
5from PIL import Image, ImageFont, ImageDraw
6
7import math
8
9# Use a power of 2 pixels per character so we can evenly divide the plane.
10font_pixels = 128
11full_ratio = 0.75
12half_ratio = 0.5
13full_sz = int(font_pixels * full_ratio)
14half_sz = int(font_pixels * half_ratio)
15layout_engine = ImageFont.Layout.BASIC
16
17unifont = ImageFont.truetype("Fonts/unifont-15.0.01.ttf", full_sz, layout_engine=layout_engine)
18unifont_half = ImageFont.truetype("Fonts/unifont-15.0.01.ttf", half_sz, layout_engine=layout_engine)
19
20noto_sans_mono = ImageFont.truetype(
21        "Fonts/Noto_Sans_Mono/static/NotoSansMono/NotoSansMono-Bold.ttf",
22        full_sz, index=0, layout_engine=layout_engine)
23
24noto_sans_sc_half = ImageFont.truetype("Fonts/Noto_Sans_Simplified_Chinese/NotoSansSC-Regular.otf", half_sz, layout_engine=layout_engine)
25
26noto_sans_kr_half = ImageFont.truetype("Fonts/Noto_Sans_Korean/NotoSansKR-Regular.otf", half_sz, layout_engine=layout_engine)
27
28n_rows = 64
29n_cols = 128
30
31class FontInfo:
32    def __init__(self, font, dy):
33        self.font = font
34        self.dy = dy
35
36def allow_range(allowlist, lo_hi, font = None, dy = 0):
37    for i in range(lo_hi[0], lo_hi[1] + 1):
38        allowlist[i] = FontInfo(font, dy)
39def ban_range(allowlist, lo, hi):
40    for i in range(lo, hi + 1):
41        del allowlist[i]
42allowlist = {}
43# ASCII
44basic_latin = (32, 126)
45allow_range(allowlist, basic_latin, font=noto_sans_mono, dy = -20)
46# Latin-1 supplement
47latin_1_supplement = (0x00A1, 0x00ff)
48allow_range(allowlist, latin_1_supplement, font = noto_sans_mono)
49# Latin extended-A
50latin_extended_a = (0x0100, 0x017f)
51allow_range(allowlist, latin_extended_a, font = noto_sans_mono)
52# Latin extended-B
53latin_extended_b = (0x0180, 0x024f)
54allow_range(allowlist, latin_extended_b, font = noto_sans_mono)
55# Spacing modifier letters
56ipa_extensions = (0x0250, 0x02af)
57allow_range(allowlist, ipa_extensions, font = unifont)
58# Greek and Coptic
59greek = (0x0370, 0x03ff)
60allow_range(allowlist, greek, font = noto_sans_mono)
61ban_range(allowlist, 0x0378, 0x03a2)
62# Cyrillic
63cyrillic = (0x0400, 0x04ff)
64allow_range(allowlist, cyrillic, font = unifont)
65# Currency symbols
66currency_symbols = (0x20a0, 0x20c0)
67allow_range(allowlist, currency_symbols, font = noto_sans_mono)
68
69# CJK
70#
71hangul_jamo = (0x1100, 0x11FF)
72allow_range(allowlist, hangul_jamo, font = noto_sans_kr_half)
73#
74general_punctuation = (0x2000, 0x206f)
75allow_range(allowlist, general_punctuation, font = noto_sans_mono)
76#
77kangxi_radicals = (0x2f00, 0x2fdf)
78allow_range(allowlist, kangxi_radicals, font = noto_sans_sc_half)
79#
80cjk_symbols_and_punctuation = (0x3000, 0x303f)
81allow_range(allowlist, cjk_symbols_and_punctuation, font = noto_sans_sc_half)
82#
83hiragana = (0x3041, 0x309f)
84allow_range(allowlist, hiragana, font = noto_sans_sc_half)
85ban_range(allowlist, 0x3097, 0x3098)
86#
87katakana = (0x30a0, 0x30ff)
88allow_range(allowlist, katakana, font = noto_sans_sc_half)
89#
90hangul_compatibility_jamo = (0x3130, 0x318f)
91allow_range(allowlist, hangul_compatibility_jamo, font = noto_sans_sc_half)
92#
93enclosed_cjk_letters_and_months = (0x3200, 0x32FF)
94allow_range(allowlist, enclosed_cjk_letters_and_months, font = noto_sans_sc_half)
95#
96cjk_compatibility = (0x3300, 0x33ff)
97allow_range(allowlist, cjk_compatibility, font = noto_sans_sc_half)
98#
99cjk_unified_extension_a = (0x3400, 0x4dbf)
100allow_range(allowlist, cjk_unified_extension_a, font = noto_sans_sc_half)
101#
102cjk_ideographs = (0x4e00, 0x9fff)
103allow_range(allowlist, cjk_ideographs, font = noto_sans_sc_half)
104#
105hangul_syllables = (0xAC00, 0xD7A3)
106allow_range(allowlist, hangul_syllables, font = noto_sans_kr_half)
107#
108halfwidth_and_fullwidth = (0xff00, 0xffef)
109allow_range(allowlist, halfwidth_and_fullwidth, font = noto_sans_sc_half)
110
111def in_range(x, range_pair) -> bool:
112    return x >= range_pair[0] and x <= range_pair[1]
113
114max_char = max(allowlist)
115print("max char: {}".format(max_char))
116print("num chars: {}".format(len(allowlist)))
117
118def genUnicode():
119    total_rows = math.ceil(max_char / n_cols)
120    print("total rows {}".format(total_rows))
121    total_textures = math.ceil(total_rows / n_rows)
122    print("total textures {}".format(total_textures))
123
124    for nth_texture in range(0, total_textures):
125        # Create an 8K grayscale ("L") or black and white ("1") image
126        # Unity will re-encode b&w to grayscale, so using b&w just helps keep
127        # the package size low (we vendor these, we don't generate them
128        # client-side).
129        image = Image.new(mode="1", size=(8192,8192), color=0)
130        draw = ImageDraw.Draw(image)
131
132        row_begin = nth_texture * n_rows
133
134        for row in range(row_begin, row_begin + n_rows):
135            line = ""
136            for col in range(0, n_cols):
137                # Generate the unicode character for this spot.
138                n = row * n_cols + col
139                char = None
140                font_info = None
141                if n in allowlist.keys():
142                    char = chr(n)
143                    font_info = allowlist[n]
144                else:
145                    char = " "
146                    font_info = FontInfo(unifont, 0)
147                # Hack: Chinese, Japanese, and Korean characters are all double
148                # width and are all on textures [1,6]. To fit them in the same
149                # grid, we use a half-size font.
150                draw.text((col * font_pixels / 2, (row - row_begin) * font_pixels +
151                    font_info.dy), char, font=font_info.font, fill=255)
152
153        image.save("Fonts/Bitmaps/font-%01d.png" % nth_texture)
154
155def genASCII():
156    # Create an 8k grayscale image. 16 glyphs wide, 8 glyphs tall.
157    # Only characters on the range [0, 128).
158    image = Image.new(mode="RGBA", size=(8192,8192), color=0)
159    draw = ImageDraw.Draw(image)
160    n_rows = 8
161    n_cols = 16
162
163    font = ImageFont.truetype(
164            "Fonts/Noto_Sans_Mono/static/NotoSansMono/NotoSansMono-Bold.ttf",
165            int((8192 / 8) * 0.75), index=0, layout_engine=layout_engine)
166
167    for row in range(0, n_rows):
168        for col in range(0, n_cols):
169            n = row * n_cols + col
170            char = None
171            font_info = None
172            if n in allowlist.keys():
173                char = chr(n)
174            else:
175                char = " "
176            draw.text((col * font_pixels * 8 / 2, row * font_pixels * 8 - 20),
177                    char, font=font, fill=(255,255,255))
178    image.save("Fonts/Bitmaps/font-ascii.png")
179
180if __name__ == "__main__":
181    print("Generating unicode fonts")
182    #genUnicode()
183    print("Generating ASCII fonts")
184    genASCII()