#!/usr/bin/env python3 """Record every spoken line of Dot and the Shape Friends, free and offline, with Kokoro. The narrator is Kokoro "af_heart"; Munchy is Kokoro "am_puck" (the game plays him a little slower). Each line becomes one small MP3 in v/ next to game.js, named by a hash of the line, and the VOX (narrator) and VOXM (Munchy) lists at the top of game.js's voice section are rewritten to match. A line with no clip still shows as a caption and is read by the device voice instead. Setup (once): pip install kokoro-onnx soundfile numpy put kokoro-v1.0.onnx and voices-v1.0.bin (from the kokoro-onnx releases) next to this file install ffmpeg (it turns the recordings into MP3) Use: python3 voices.py check game.js what is missing, without recording anything python3 voices.py record game.js record only the lines that have no clip yet python3 voices.py record game.js --all record every line again (new voice, new speed) python3 voices.py selftest checks the line finder and the file names Where the lines come from: the VOX and VOXM lists already in game.js, every say('...') and munSay('...') with a plain line in quotes, and Munchy's munLines() list. Lines the game builds from pieces ('Look! A see-through ' + name) are not visible here: open the game, run copy(__sf.lines().join('\\n')) in the browser console, paste into a text file and add --lines that-file.txt (and --munchy-lines for Munchy's own). """ import json, os, re, subprocess, sys, tempfile HERE = os.path.dirname(os.path.abspath(__file__)) NARRATOR, MUNCHY = 'af_heart', 'am_puck' # How a line is said, when the written form reads badly aloud. The clip keeps the written line's name. SAY_AS = { 'Wheee! Round and round!': 'Whee! Round and round!', 'Ah... ah... ah... choo!': 'Ah, ah, ah, choo!', 'Pee-yew! Stinky!': 'Pee-yoo! Stinky!', '1D! Now it is just a line.': 'One D! Now it is just a line.', '2D! Now it is flat, like a drawing.': 'Two D! Now it is flat, like a drawing.', '3D! A solid shape again.': 'Three D! A solid shape again.', } def name_for(line, munchy=False): """The clip's file name: a 32-bit hash of the line, 8 hex digits; Munchy's start with m.""" v = 5381 for ch in line: v = ((v * 33) ^ ord(ch)) & 0xffffffff return ('m' if munchy else '') + '%08x' % v + '.mp3' def read_list(js, var): """The JSON object after 'var NAME=' (or 'NAME=') in game.js, and where it sits.""" m = re.search(r'(? 0.012)[0] # trim the silence at both ends if len(loud): s = s[max(0, loud[0] - int(.03 * sr)):min(len(s), loud[-1] + int(.12 * sr))] s = s / max(1e-6, float(np.max(np.abs(s)))) * 0.86 # same loudness for every line fade = int(.04 * sr) s[-fade:] *= np.linspace(1, 0, fade) sf.write(wav, s, sr) subprocess.run(['ffmpeg', '-y', '-loglevel', 'error', '-i', wav, '-ac', '1', '-ar', '24000', '-b:a', '48k', os.path.join(vdir, f)], check=True) print(f' {n}/{len(todo)} {f} {line}') for line in narr: vox.setdefault(line, name_for(line)) for line in mun: voxm.setdefault(line, name_for(line, True)) for var, table in (('VOX', vox), ('VOXM', voxm)): # write the lists back into game.js _, (a, b) = read_list(js, var) js = js[:a] + json.dumps(dict(sorted(table.items())), ensure_ascii=False) + js[b:] open(js_path, 'w', encoding='utf-8').write(js) print('game.js lists updated') def check(js_path, extra_narr, extra_mun): _, vox, voxm, narr, mun, vdir, todo = plan(js_path, extra_narr, extra_mun) print(f'{len(narr)} narrator lines, {len(mun)} Munchy lines, clips in {vdir}') for line, f, munchy in todo: print(f' missing {"Munchy " if munchy else ""}{f} {line}') print('all lines have clips' if not todo else f'{len(todo)} missing: run "record" to make them') return len(todo) def selftest(): js = ("var VOX={\"Hi!\": \"x.mp3\"};var VOXM={};function munLines(){return ['Ha ha!','Hee \\'hee\\'!'];}" "say('Tap Dot!');say(\"It's a cube!\",'sub');munSay('Yum!',null,{p:0});dismay('no');say('A '+n+'!');") narr, mun = find_lines(js) assert narr == {'Tap Dot!', "It's a cube!"}, narr assert mun == {'Yum!', 'Ha ha!', "Hee 'hee'!"}, mun assert read_list(js, 'VOX')[0] == {'Hi!': 'x.mp3'} and read_list(js, 'VOXM')[0] == {} assert name_for('This is Dot. Dot is teeny tiny. Tap Dot!') == name_for('This is Dot. Dot is teeny tiny. Tap Dot!') assert name_for('Hi!', True).startswith('m') and len(name_for('Hi!')) == 12 print('selftest passed') def main(argv): if not argv or argv[0] not in ('check', 'record', 'selftest'): print(__doc__) return 2 if argv[0] == 'selftest': selftest() return 0 if len(argv) < 2: print('give the path to game.js') return 2 js_path, rest = argv[1], argv[2:] def lines_from(flag): if flag in rest: return [l.strip() for l in open(rest[rest.index(flag) + 1], encoding='utf-8') if l.strip()] return [] extra_n, extra_m = lines_from('--lines'), lines_from('--munchy-lines') if argv[0] == 'check': return 1 if check(js_path, extra_n, extra_m) else 0 record(js_path, extra_n, extra_m, '--all' in rest) return 0 if __name__ == '__main__': sys.exit(main(sys.argv[1:]))