#!/usr/bin/env python3 """Record every spoken line of Clock Knockout with Kokoro "af_heart", free and offline. Each line becomes one small MP3 in v/ next to the game, named by a 32-bit hash of the line (the same hash the game uses in clipName()), so no list has to be kept in the code. A line with no clip is read by the device voice instead. Setup (once): pip install kokoro-onnx soundfile numpy playwright ; ffmpeg on the path ; put kokoro-v1.0.onnx and voices-v1.0.bin (github.com/thewh1teagle/kokoro-onnx, release model-files-v1.0) next to this file. Use: python3 docs/clock-knockout/voices.py docs/clock-knockout/lines.json public/games/clock-knockout/v/ record the missing lines python3 docs/clock-knockout/voices.py docs/clock-knockout/lines.json public/games/clock-knockout/v/ --all record every line again After changing a line in the game: run docs/clock-knockout/split-for-site.py, save a new lines.json, record, delete clips no line uses. lines.json is the game's own list: open the game and run copy(JSON.stringify(__lines())) in the console. """ import json, os, re, subprocess, sys, tempfile HERE = os.path.dirname(os.path.abspath(__file__)) SAY_AS = {'Zzzz...': 'Zzzzz.', 'Gallop gallop!': 'Gallop, gallop!', 'Six.': 'six', 'six.': 'six', 'It is': "It's", "When the long blue saber points straight up to twelve, we say o'clock.": 'When the long blue saber points straight up to twelve, we say, oh clock.', "Great! The short saber tells the hour. You know o'clock!": 'Great! The short saber tells the hour. You know, oh clock!'} def name_for(line): v = 5381 for ch in line: v = ((v * 33) ^ ord(ch)) & 0xffffffff return '%08x.mp3' % v def spoken(line): t = SAY_AS.get(line, line) t = re.sub(r"\b(say|means|read) o'clock", r'\1, oh clock', t) t = re.sub(r'\b(thir|four|fif|six|seven|eigh|nine)teen\b', r'\1-teen', t, flags=re.I) # 'sixteen' was heard as 'sixty' # a lone o'clock is swallowed as 'a clock' return re.sub(r'\b([A-Z]{2,})\b', lambda m: m.group(1).capitalize(), t) # MINE! is read as a word, not spelled def main(argv): if len(argv) < 2: print(__doc__); return 2 lines = json.load(open(argv[0], encoding='utf-8')); out = argv[1]; redo = '--all' in argv os.makedirs(out, exist_ok=True) todo = [l for l in lines if redo or not os.path.exists(os.path.join(out, name_for(l)))] print(f'{len(lines)} lines, {len(todo)} to record') if not todo: return 0 import numpy as np, soundfile as sf from kokoro_onnx import Kokoro k = Kokoro(os.path.join(HERE, 'kokoro-v1.0.onnx'), os.path.join(HERE, 'voices-v1.0.bin')) wav = os.path.join(tempfile.gettempdir(), 'clock-line.wav') for n, line in enumerate(todo, 1): s, sr = k.create(spoken(line), voice='af_heart', speed=0.95, lang='en-us') s = np.asarray(s, dtype=np.float32) loud = np.where(np.abs(s) > 0.012)[0] if len(loud): s = s[max(0, loud[0] - int(.03 * sr)):min(len(s), loud[-1] + int(.10 * sr))] s = s / max(1e-6, float(np.max(np.abs(s)))) * 0.86 fade = int(.03 * 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', '40k', os.path.join(out, name_for(line))], check=True) if n % 25 == 0: print(f' {n}/{len(todo)}') return 0 if __name__ == '__main__': sys.exit(main(sys.argv[1:]))