mindcraft/src/agent/speak.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-02-12 16:26:48 +00:00
import { exec } from 'child_process';
let speakingQueue = [];
let isSpeaking = false;
2025-02-12 16:26:48 +00:00
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();
2025-02-12 16:26:48 +00:00
const isWin = process.platform === "win32";
const isMac = process.platform === "darwin";
let command;
if (isWin) {
2025-03-17 14:00:38 -05:00
command = `powershell -Command "Add-Type -AssemblyName System.Speech; $s = New-Object System.Speech.Synthesis.SpeechSynthesizer; $s.Rate = 2; $s.Speak(\\"${textToSpeak}\\"); $s.Dispose()"`;
2025-02-12 16:26:48 +00:00
} 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}`);
2025-03-13 14:40:18 -05:00
}
processQueue(); // Continue with the next message in the queue
2025-02-12 16:26:48 +00:00
});
}