diff --git a/settings.js b/settings.js index c226658..b205206 100644 --- a/settings.js +++ b/settings.js @@ -30,7 +30,7 @@ export default "load_memory": false, // load memory from previous session "init_message": "Respond with hello world and your name", // sends to all on spawn "only_chat_with": [], // users that the bots listen to and send general messages to. if empty it will chat publicly - + "speak": false, // allows all bots to speak through system text-to-speech. tested on windows, should work on mac, on linux you may need to `apt install espeak` "language": "en", // translate to/from this language. Supports these language names: https://cloud.google.com/translate/docs/languages "show_bot_views": false, // show bot's view in browser at localhost:3000, 3001... diff --git a/src/agent/agent.js b/src/agent/agent.js index 853df3f..db4e591 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -14,6 +14,7 @@ import { addViewer } from './viewer.js'; import settings from '../../settings.js'; import { serverProxy } from './agent_proxy.js'; import { Task } from './tasks.js'; +import { say } from './speak.js'; export class Agent { async start(profile_fp, load_mem=false, init_message=null, count_id=0, task_path=null, task_id=null) { @@ -351,6 +352,9 @@ export class Agent { } } else { + if (settings.speak) { + say(message); + } this.bot.chat(message); } } diff --git a/src/agent/speak.js b/src/agent/speak.js new file mode 100644 index 0000000..af18298 --- /dev/null +++ b/src/agent/speak.js @@ -0,0 +1,42 @@ +import { exec } from 'child_process'; + +let speakingQueue = []; +let isSpeaking = false; + +export function say(textToSpeak) { + speakingQueue.push(textToSpeak); + if (!isSpeaking) { + processQueue(); + } +} + +function processQueue() { + if (speakingQueue.length === 0) { + isSpeaking = false; + return; + } + + isSpeaking = true; + const textToSpeak = speakingQueue.shift(); + const isWin = process.platform === "win32"; + const isMac = process.platform === "darwin"; + + let command; + + if (isWin) { + command = `powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(\\"${textToSpeak}\\")"`; + } else if (isMac) { + command = `say "${textToSpeak}"`; + } else { + command = `espeak "${textToSpeak}"`; + } + + exec(command, (error, stdout, stderr) => { + if (error) { + console.error(`Error: ${error.message}`); + console.error(`${error.stack}`); + } else if (stderr) { + console.error(`Error: ${stderr}`); + processQueue(); // Continue with the next message in the queue + }); +}