Spaces:
Sleeping
Sleeping
Commit
·
56fcb88
1
Parent(s):
f6f721b
Improve voice quality and naturalness
Browse files- frontend/app/voice/page.tsx +43 -3
frontend/app/voice/page.tsx
CHANGED
|
@@ -64,13 +64,53 @@ export default function OraVoice() {
|
|
| 64 |
|
| 65 |
const speak = (text: string) => {
|
| 66 |
setState("SPEAKING");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 68 |
-
|
|
|
|
| 69 |
const voices = window.speechSynthesis.getVoices();
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
utterance.onend = () => setState("IDLE");
|
|
|
|
|
|
|
| 74 |
window.speechSynthesis.speak(utterance);
|
| 75 |
};
|
| 76 |
|
|
|
|
| 64 |
|
| 65 |
const speak = (text: string) => {
|
| 66 |
setState("SPEAKING");
|
| 67 |
+
|
| 68 |
+
// Cancel any ongoing speech
|
| 69 |
+
window.speechSynthesis.cancel();
|
| 70 |
+
|
| 71 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 72 |
+
|
| 73 |
+
// Get available voices and select the best one
|
| 74 |
const voices = window.speechSynthesis.getVoices();
|
| 75 |
+
|
| 76 |
+
// Priority order for natural-sounding voices
|
| 77 |
+
const preferredVoices = [
|
| 78 |
+
"Microsoft Zira - English (United States)",
|
| 79 |
+
"Google US English Female",
|
| 80 |
+
"Samantha",
|
| 81 |
+
"Karen",
|
| 82 |
+
"Moira",
|
| 83 |
+
"Tessa"
|
| 84 |
+
];
|
| 85 |
+
|
| 86 |
+
let selectedVoice = voices.find(v =>
|
| 87 |
+
preferredVoices.some(pv => v.name.includes(pv))
|
| 88 |
+
);
|
| 89 |
+
|
| 90 |
+
// Fallback to any female English voice
|
| 91 |
+
if (!selectedVoice) {
|
| 92 |
+
selectedVoice = voices.find(v =>
|
| 93 |
+
v.lang.includes("en") && v.name.toLowerCase().includes("female")
|
| 94 |
+
);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// Final fallback
|
| 98 |
+
if (!selectedVoice && voices.length > 0) {
|
| 99 |
+
selectedVoice = voices[0];
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
if (selectedVoice) {
|
| 103 |
+
utterance.voice = selectedVoice;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// Voice parameters for warmth and naturalness
|
| 107 |
+
utterance.rate = 0.9; // Slightly slower for clarity
|
| 108 |
+
utterance.pitch = 0.95; // Slightly lower for warmth
|
| 109 |
+
utterance.volume = 1.0; // Full volume
|
| 110 |
|
| 111 |
utterance.onend = () => setState("IDLE");
|
| 112 |
+
utterance.onerror = () => setState("IDLE");
|
| 113 |
+
|
| 114 |
window.speechSynthesis.speak(utterance);
|
| 115 |
};
|
| 116 |
|