Abdalkaderdev commited on
Commit
c45eea2
·
1 Parent(s): 2ce54a8

Update frontend to use Supertonic 2 TTS API

Browse files
Files changed (1) hide show
  1. frontend/app/voice/page.tsx +30 -44
frontend/app/voice/page.tsx CHANGED
@@ -62,63 +62,49 @@ export default function OraVoice() {
62
  }
63
  };
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
 
117
  const toggleListen = () => {
118
  if (state === "IDLE") {
119
  recognitionRef.current?.start();
120
  } else if (state === "SPEAKING") {
121
- window.speechSynthesis.cancel();
122
  setState("IDLE");
123
  }
124
  };
 
62
  }
63
  };
64
 
65
+ const speak = async (text: string) => {
66
  setState("SPEAKING");
67
 
68
+ try {
69
+ // Call backend TTS API for professional voice
70
+ const res = await fetch("/api/tts", {
71
+ method: "POST",
72
+ headers: { "Content-Type": "application/json" },
73
+ body: JSON.stringify({ text }),
74
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ if (!res.ok) {
77
+ throw new Error("TTS failed");
78
+ }
 
79
 
80
+ // Get audio blob and play it
81
+ const audioBlob = await res.blob();
82
+ const audioUrl = URL.createObjectURL(audioBlob);
83
+ const audio = new Audio(audioUrl);
84
+
85
+ audio.onended = () => {
86
+ setState("IDLE");
87
+ URL.revokeObjectURL(audioUrl);
88
+ };
89
 
90
+ audio.onerror = () => {
91
+ setState("IDLE");
92
+ URL.revokeObjectURL(audioUrl);
93
+ };
94
 
95
+ await audio.play();
 
96
 
97
+ } catch (error) {
98
+ console.error("TTS error:", error);
99
+ setState("IDLE");
100
+ }
101
  };
102
 
103
  const toggleListen = () => {
104
  if (state === "IDLE") {
105
  recognitionRef.current?.start();
106
  } else if (state === "SPEAKING") {
107
+ // Audio will stop naturally or can be stopped via audio element
108
  setState("IDLE");
109
  }
110
  };