yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
5f33e4e
v0.19.1
1#!/usr/bin/env python3 2 3import argparse 4import generate_utils 5import os 6import sys 7 8# A single parameter looks like this: 9# _Letter_Row00_Col00_Byte0("_Letter_Row00_Col00_Byte0", float) = 0 10def generateUnityParams ( nbytes : int , nrows : int , ncols : int , prefix : str = "" ) -> str : 11lines = [] 12lines . append ( prefix + "// BEGIN GENERATED CODE BLOCK" ) 13for byte in range ( 0 , nbytes ) : 14for row in range ( 0 , nrows ) : 15for col in range ( 0 , ncols ) : 16param_name = generate_utils . getShaderParamByRowColByte ( row , col , byte ) 17line = prefix + """{}("{}", float) = 0""" . format ( param_name , param_name ) 18lines . append ( line ) 19lines . append ( prefix + "// END GENERATED CODE BLOCK" ) 20return ' \n ' . join ( lines ) 21 22# A single parameter looks like this: 23# float _Letter_Row00_Col00_Byte0; 24def generateCgParams ( nbytes : int , nrows : int , ncols : int , prefix : str = "" ) -> str : 25lines = [] 26lines . append ( prefix + "// BEGIN GENERATED CODE BLOCK" ) 27for byte in range ( 0 , nbytes ) : 28for row in range ( 0 , nrows ) : 29for col in range ( 0 , ncols ) : 30param_name = generate_utils . getShaderParamByRowColByte ( row , col , byte ) 31line = prefix + """float {};""" . format ( param_name ) 32lines . append ( line ) 33lines . append ( prefix + "// END GENERATED CODE BLOCK" ) 34return ' \n ' . join ( lines ) 35 36# Define 3 constants: 37# uniform int BYTES_PER_CHAR = $nbytes; 38# uniform int NROWS = $nrows; 39# uniform int NCOLS = $ncols; 40def generateCgConstants ( nbytes : int , board_nrows : int , board_ncols : int , 41texture_nrows : int , texture_ncols : int , prefix : str = "" ) -> str : 42lines = [] 43lines . append ( prefix + "// BEGIN GENERATED CODE BLOCK" ) 44lines . append ( prefix + "#define BYTES_PER_CHAR {}" . format ( nbytes )) 45lines . append ( prefix + "#define BOARD_NROWS {}" . format ( board_nrows )) 46lines . append ( prefix + "#define BOARD_NCOLS {}" . format ( board_ncols )) 47lines . append ( prefix + "#define TEXTURE_NROWS {}" . format ( texture_nrows )) 48lines . append ( prefix + "#define TEXTURE_NCOLS {}" . format ( texture_ncols )) 49lines . append ( prefix + "// END GENERATED CODE BLOCK" ) 50return ' \n ' . join ( lines ) 51 52# This is the basic idea of what we're generating: 53# // Get the value of the parameter for the cell we're in. 54# uint GetLetterParameter(float2 uv) 55# { 56# float CHAR_COL = floor(uv.x * Cols); 57# float CHAR_ROW = floor(uv.y * Rows); 58# uint res = 0; 59# 60# [forcecase] switch(CHAR_ROW) { 61# case n: 62# case n-1: 63# ... 64# 65# [forcecase] switch (CHAR_COL) { 66# case 0: 67# case 1: 68# ... 69# 70# res |= ((uint) _Letter_Row00_Col00_Byte0) << (0 * 8); 71# res |= ((uint) _Letter_Row00_Col00_Byte1) << (1 * 8); 72# continue; 73# } 74# } 75# return res; 76# } 77# In English, this provides an accessor to the many (possibly thousands) 78# float parameters which hold the text on the board. 79def generateLetterAccessor ( nbytes : int , nrows : int , ncols : int , prefix : str = "" ) -> str : 80lines = [] 81lines . append ( prefix + "// BEGIN GENERATED CODE BLOCK" ) 82lines . append ( prefix + "[forcecase] switch (CHAR_ROW) {" ) 83for row in range ( 0 , nrows ) : 84lines . append ( prefix + " case {}:" . format ( nrows - ( row + 1 ))) 85lines . append ( prefix + " [forcecase] switch (CHAR_COL) {" ) 86for col in range ( 0 , ncols ) : 87lines . append ( prefix + " case {}:" . format ( col )) 88for byte in range ( 0 , nbytes ) : 89param_name = generate_utils . getShaderParamByRowColByte ( row , col , byte ) 90lines . append ( prefix + " res |= ((uint) {}) << ({} * 8);" . format ( param_name , byte )) 91lines . append ( prefix + " return res;" ) 92lines . append ( prefix + " default:" ) 93lines . append ( prefix + " return 0;" ) 94lines . append ( prefix + " }" ) 95lines . append ( prefix + "}" ) 96lines . append ( prefix + "// END GENERATED CODE BLOCK" ) 97return ' \n ' . join ( lines ) 98 99# Replace any line containing `macro` with `replacement`. 100def applyLineMacro ( old_path : str , new_path : str , macro : str , replacement : str ) -> bool : 101new_lines = [] 102times_applied = 0 103with open ( old_path , 'r' , encoding = "utf-8" ) as f : 104for line in f : 105if line [ - 1 ] == ' \n ' : 106line = line [ 0 : len ( line ) - 1 ] 107if macro in line : 108new_lines . append ( replacement ) 109times_applied += 1 110else : 111new_lines . append ( line ) 112with open ( new_path , 'w' , encoding = "utf-8" ) as f : 113f . write ( ' \n ' . join ( new_lines )) 114return times_applied 115 116if __name__ == "__main__" : 117( "args: {}" . format ( " " . join ( sys . argv ))) 118 119parser = argparse . ArgumentParser () 120parser . add_argument ( "--bytes_per_char" , type = str , help = "The number of bytes to use to represent each character" ) 121parser . add_argument ( "--board_rows" , type = str , help = "The number of rows on the board" ) 122parser . add_argument ( "--board_cols" , type = str , help = "The number of columns on the board" ) 123parser . add_argument ( "--texture_rows" , type = str , help = "The number of rows on the font textures" ) 124parser . add_argument ( "--texture_cols" , type = str , help = "The number of columns on the font textures" ) 125parser . add_argument ( "--shader_template" , type = str , help = "The path to the shader template" ) 126parser . add_argument ( "--shader_path" , type = str , help = "The path where the generated shader will be written" ) 127args = parser . parse_args () 128 129if not args . bytes_per_char or not args . board_rows or not args . board_cols \ 130or not args . texture_rows or not args . texture_cols \ 131or not args . shader_template or not args . shader_path : 132(( "--bytes_per_char, --board_rows, --board_cols, --texture_rows, " 133"--texture_cols, --shader_template, --shader_path required" ) , file = sys . stderr ) 134sys . exit ( 1 ) 135 136nbytes = int ( args . bytes_per_char ) 137board_nrows = int ( args . board_rows ) 138board_ncols = int ( args . board_cols ) 139texture_nrows = int ( args . texture_rows ) 140texture_ncols = int ( args . texture_cols ) 141 142replacement = generateUnityParams ( nbytes , board_nrows , board_ncols , prefix = "" ) 143#print(replacement) 144macro = "// %TEMPLATE__UNITY_ROW_COL_PARAMS%" 145applyLineMacro ( args . shader_template , args . shader_path , macro , replacement ) 146 147replacement = generateCgParams ( nbytes , board_nrows , board_ncols , prefix = " " ) 148#print(replacement) 149macro = "// %TEMPLATE__CG_ROW_COL_PARAMS%" 150applyLineMacro ( args . shader_path , args . shader_path , macro , replacement ) 151 152replacement = generateCgConstants ( nbytes , board_nrows , board_ncols , 153texture_nrows , texture_ncols , prefix = " " ) 154#print(replacement) 155macro = "// %TEMPLATE__CG_ROW_COL_CONSTANTS%" 156applyLineMacro ( args . shader_path , args . shader_path , macro , replacement ) 157 158replacement = generateLetterAccessor ( nbytes , board_nrows , board_ncols , prefix = " " ) 159#print(replacement) 160macro = "// %TEMPLATE__CG_LETTER_ACCESSOR%" 161applyLineMacro ( args . shader_path , args . shader_path , macro , replacement )