| 1 | import { exec, ChildProcess } from "child_process"; |
| 2 | import os from "node:os"; |
| 3 | |
| 4 | import { removeCodeBlocksAndTrim } from "."; |
| 5 | |
| 6 | import type { IMessenger } from "../protocol/messenger"; |
| 7 | import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; |
| 8 | |
| 9 | // The amount of time before a process is declared |
| 10 | // a zombie after executing .kill() |
| 11 | const ttsKillTimeout: number = 5000; |
| 12 | |
| 13 | /** |
| 14 | * Cleans a message text to safely be used in 'exec' context on host. |
| 15 | * |
| 16 | * Return modified message text. |
| 17 | */ |
| 18 | export function sanitizeMessageForTTS(message: string): string { |
| 19 | message = removeCodeBlocksAndTrim(message); |
| 20 | |
| 21 | // Remove or replace problematic characters |
| 22 | message = message |
| 23 | .replace(/"/g, "") |
| 24 | .replace(/`/g, "") |
| 25 | .replace(/\$/g, "") |
| 26 | .replace(/\\/g, "") |
| 27 | .replace(/[&|;()<>]/g, ""); |
| 28 | |
| 29 | message = message.trim().replace(/\s+/g, " "); |
| 30 | |
| 31 | return message; |
| 32 | } |
| 33 | |
| 34 | export class TTS { |
| 35 | static os: string | undefined = undefined; |
| 36 | static handle: ChildProcess | undefined = undefined; |
| 37 | static messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>; |
| 38 | |
| 39 | static async read(message: string) { |
| 40 | message = sanitizeMessageForTTS(message); |
| 41 | |
| 42 | try { |
| 43 | // Kill any active TTS processes |
| 44 | await TTS.kill(); |
| 45 | } catch (e) { |
| 46 | console.warn("Error killing TTS process: ", e); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | switch (TTS.os) { |
| 51 | case "darwin": |
| 52 | TTS.handle = exec(`say "${message}"`); |
| 53 | break; |
| 54 | case "win32": |
| 55 | // Replace single quotes on windows |
| 56 | TTS.handle = exec( |
| 57 | `powershell -Command "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('${message.replace( |
| 58 | /'/g, |
| 59 | "''", |
| 60 | )}')"`, |
| 61 | ); |
| 62 | break; |
| 63 | case "linux": |
| 64 | TTS.handle = exec(`espeak "${message}"`); |
| 65 | break; |
| 66 | default: |
| 67 | console.log( |
| 68 | "Text-to-speech is not supported on this operating system.", |
| 69 | ); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | void TTS.messenger.request("setTTSActive", true); |
| 74 | |
| 75 | TTS.handle?.once("exit", () => { |
| 76 | void TTS.messenger.request("setTTSActive", false); |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | static async kill(): Promise<void> { |
| 81 | return new Promise<void>((resolve, reject) => { |
| 82 | // Only kill a TTS process if it's still running |
| 83 | if (TTS.handle && TTS.handle.exitCode === null) { |
| 84 | // Use a timeout in case of zombie processes |
| 85 | let killTimeout: NodeJS.Timeout = setTimeout(() => { |
| 86 | reject(`Unable to kill TTS process: ${TTS.handle?.pid}`); |
| 87 | }, ttsKillTimeout); |
| 88 | |
| 89 | // Resolve our promise once the program has exited |
| 90 | TTS.handle.once("exit", () => { |
| 91 | clearTimeout(killTimeout); |
| 92 | TTS.handle = undefined; |
| 93 | resolve(); |
| 94 | }); |
| 95 | |
| 96 | TTS.handle.kill(); |
| 97 | } else { |
| 98 | resolve(); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | static async setup() { |
| 104 | TTS.os = os.platform(); |
| 105 | } |
| 106 | } |