2025-02-12 16:26:48 +00:00
|
|
|
import { exec } from 'child_process';
|
|
|
|
|
2025-02-12 19:10:14 +00:00
|
|
|
let speakingQueue = [];
|
|
|
|
let isSpeaking = false;
|
|
|
|
|
2025-02-12 16:26:48 +00:00
|
|
|
export function say(textToSpeak) {
|
2025-02-12 19:10:14 +00:00
|
|
|
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) {
|
|
|
|
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}`);
|
2025-02-12 19:12:34 +00:00
|
|
|
console.error(`${error.stack}`);
|
2025-02-12 19:10:14 +00:00
|
|
|
} else if (stderr) {
|
2025-02-12 19:12:34 +00:00
|
|
|
console.error(`Error: ${stderr}`);
|
2025-02-12 19:10:14 +00:00
|
|
|
processQueue(); // Continue with the next message in the queue
|
2025-02-12 16:26:48 +00:00
|
|
|
});
|
|
|
|
}
|