Spaces:
Running
Running
Update index.html
Browse files- index.html +27 -20
index.html
CHANGED
@@ -7,7 +7,7 @@
|
|
7 |
body {
|
8 |
font-family: Arial, sans-serif;
|
9 |
text-align: center;
|
10 |
-
margin-top:
|
11 |
}
|
12 |
#speakButton {
|
13 |
padding: 15px 30px;
|
@@ -33,7 +33,7 @@
|
|
33 |
<h2>Japanese Voice Agent</h2>
|
34 |
<p>Click the button and speak in Japanese. The system will repeat what you said.</p>
|
35 |
<button id="speakButton">Tap to Speak</button>
|
36 |
-
<div id="status">
|
37 |
|
38 |
<script>
|
39 |
const speakButton = document.getElementById("speakButton");
|
@@ -52,12 +52,23 @@
|
|
52 |
});
|
53 |
}
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
|
|
|
61 |
async function speakJapanese(text) {
|
62 |
await loadVoices();
|
63 |
const utterance = new SpeechSynthesisUtterance(text);
|
@@ -72,19 +83,15 @@
|
|
72 |
synth.speak(utterance);
|
73 |
}
|
74 |
|
75 |
-
|
|
|
76 |
try {
|
77 |
await navigator.mediaDevices.getUserMedia({ audio: true });
|
78 |
-
|
79 |
-
unlockSafariAudio();
|
80 |
-
}
|
81 |
-
statusDiv.textContent = "Ready. Tap the button and speak in Japanese.";
|
82 |
-
} catch (error) {
|
83 |
statusDiv.textContent = "Microphone permission denied.";
|
|
|
84 |
}
|
85 |
-
}
|
86 |
|
87 |
-
function startRecognition() {
|
88 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
89 |
if (!SpeechRecognition) {
|
90 |
statusDiv.textContent = "Speech recognition not supported.";
|
@@ -96,7 +103,7 @@
|
|
96 |
recognition.interimResults = false;
|
97 |
|
98 |
recognition.onstart = () => {
|
99 |
-
statusDiv.textContent = "Listening
|
100 |
};
|
101 |
|
102 |
recognition.onresult = (event) => {
|
@@ -111,19 +118,19 @@
|
|
111 |
|
112 |
recognition.onend = () => {
|
113 |
if (!synth.speaking) {
|
114 |
-
statusDiv.textContent += " | Tap to speak
|
115 |
}
|
116 |
};
|
117 |
|
118 |
recognition.start();
|
119 |
}
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
};
|
125 |
|
126 |
-
speakButton.addEventListener("click",
|
127 |
</script>
|
128 |
</body>
|
129 |
</html>
|
|
|
7 |
body {
|
8 |
font-family: Arial, sans-serif;
|
9 |
text-align: center;
|
10 |
+
margin-top: 60px;
|
11 |
}
|
12 |
#speakButton {
|
13 |
padding: 15px 30px;
|
|
|
33 |
<h2>Japanese Voice Agent</h2>
|
34 |
<p>Click the button and speak in Japanese. The system will repeat what you said.</p>
|
35 |
<button id="speakButton">Tap to Speak</button>
|
36 |
+
<div id="status">Loading voice...</div>
|
37 |
|
38 |
<script>
|
39 |
const speakButton = document.getElementById("speakButton");
|
|
|
52 |
});
|
53 |
}
|
54 |
|
55 |
+
// Unlock Japanese TTS on startup
|
56 |
+
async function unlockJapaneseTTS() {
|
57 |
+
await loadVoices();
|
58 |
+
const testUtter = new SpeechSynthesisUtterance("準備ができました");
|
59 |
+
const jpVoice = voices.find(v => v.lang === "ja-JP");
|
60 |
+
if (jpVoice) {
|
61 |
+
testUtter.voice = jpVoice;
|
62 |
+
testUtter.lang = "ja-JP";
|
63 |
+
} else {
|
64 |
+
testUtter.lang = "ja-JP";
|
65 |
+
}
|
66 |
+
testUtter.volume = 0;
|
67 |
+
synth.speak(testUtter); // Silent utterance to unlock audio in Safari
|
68 |
+
statusDiv.textContent = "Ready. Tap the button and speak in Japanese.";
|
69 |
}
|
70 |
|
71 |
+
// Speak Japanese text
|
72 |
async function speakJapanese(text) {
|
73 |
await loadVoices();
|
74 |
const utterance = new SpeechSynthesisUtterance(text);
|
|
|
83 |
synth.speak(utterance);
|
84 |
}
|
85 |
|
86 |
+
// When user clicks the button: request mic, then listen
|
87 |
+
async function handleSpeak() {
|
88 |
try {
|
89 |
await navigator.mediaDevices.getUserMedia({ audio: true });
|
90 |
+
} catch (err) {
|
|
|
|
|
|
|
|
|
91 |
statusDiv.textContent = "Microphone permission denied.";
|
92 |
+
return;
|
93 |
}
|
|
|
94 |
|
|
|
95 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
96 |
if (!SpeechRecognition) {
|
97 |
statusDiv.textContent = "Speech recognition not supported.";
|
|
|
103 |
recognition.interimResults = false;
|
104 |
|
105 |
recognition.onstart = () => {
|
106 |
+
statusDiv.textContent = "Listening...";
|
107 |
};
|
108 |
|
109 |
recognition.onresult = (event) => {
|
|
|
118 |
|
119 |
recognition.onend = () => {
|
120 |
if (!synth.speaking) {
|
121 |
+
statusDiv.textContent += " | Tap again to speak.";
|
122 |
}
|
123 |
};
|
124 |
|
125 |
recognition.start();
|
126 |
}
|
127 |
|
128 |
+
// On load: unlock Japanese TTS only
|
129 |
+
window.onload = () => {
|
130 |
+
unlockJapaneseTTS();
|
131 |
};
|
132 |
|
133 |
+
speakButton.addEventListener("click", handleSpeak);
|
134 |
</script>
|
135 |
</body>
|
136 |
</html>
|