AiDeveloper1 commited on
Commit
6b59d11
·
verified ·
1 Parent(s): fe7ded9

Update index.html

Browse files
Files changed (1) hide show
  1. 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: 50px;
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">Initializing...</div>
37
 
38
  <script>
39
  const speakButton = document.getElementById("speakButton");
@@ -52,12 +52,23 @@
52
  });
53
  }
54
 
55
- function unlockSafariAudio() {
56
- const silentUtterance = new SpeechSynthesisUtterance("");
57
- silentUtterance.volume = 0;
58
- synth.speak(silentUtterance);
 
 
 
 
 
 
 
 
 
 
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
- async function askPermissions() {
 
76
  try {
77
  await navigator.mediaDevices.getUserMedia({ audio: true });
78
- if (navigator.userAgent.includes("Safari") && !navigator.userAgent.includes("Chrome")) {
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 in Japanese...";
100
  };
101
 
102
  recognition.onresult = (event) => {
@@ -111,19 +118,19 @@
111
 
112
  recognition.onend = () => {
113
  if (!synth.speaking) {
114
- statusDiv.textContent += " | Tap to speak again.";
115
  }
116
  };
117
 
118
  recognition.start();
119
  }
120
 
121
- window.onload = async () => {
122
- await loadVoices();
123
- await askPermissions();
124
  };
125
 
126
- speakButton.addEventListener("click", startRecognition);
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>