<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>声変換・代読アプリ(修正版)</title>
<style>
body { font-family: sans-serif; text-align: center; padding: 40px; background: #f0f4f8; }
.container { background: white; padding: 30px; border-radius: 20px; box-shadow: 0 10px 20px rgba(0,0,0,0.1); max-width: 600px; margin: auto; }
select, button { padding: 12px; margin: 10px; font-size: 16px; border-radius: 8px; border: 1px solid #ddd; }
button { background-color: #ff4757; color: white; cursor: pointer; border: none; font-weight: bold; width: 80%; }
#output { background: #f9f9f9; padding: 20px; margin-top: 20px; border-radius: 10px; min-height: 50px; border: 1px dashed #ccc; }
</style>
</head>
<body>
<div class="container">
<h2>音声を別の声に変換</h2>
<p>1. 声の種類を選んでください</p>
<select id="voiceSelect"></select>
<br>
<button id="startBtn">🎤 喋って声を変換</button>
<div id="output">ここに聞き取り結果が出ます</div>
</div>
<script>
const voiceSelect = document.getElementById('voiceSelect');
const startBtn = document.getElementById('startBtn');
const output = document.getElementById('output');
let voices = [];
// 声のリストを読み込む関数
function loadVoices() {
voices = window.speechSynthesis.getVoices();
// 日本語(ja)または英語(en)の声だけをリストに表示
voiceSelect.innerHTML = voices
.filter(v => v.lang.includes('ja') || v.lang.includes('en'))
.map((voice, index) => `<option value="${index}">${voice.name}</option>`)
.join('');
}
// 初回読み込み
window.speechSynthesis.onvoiceschanged = loadVoices;
loadVoices();
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'ja-JP';
startBtn.onclick = () => {
// 【重要】クリック時に空の音声を再生して「ブラウザの音再生ブロック」を解除する
const silentUttr = new SpeechSynthesisUtterance("");
window.speechSynthesis.speak(silentUttr);
recognition.start();
startBtn.innerText = "聞き取り中...";
startBtn.style.backgroundColor = "#ffa502";
};
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
output.innerText = "聞き取り成功: " + transcript;
// 音声合成の準備
const uttr = new SpeechSynthesisUtterance(transcript);
// 選択された声を設定
const selectedVoice = voices[voiceSelect.value];
if (selectedVoice) {
uttr.voice = selectedVoice;
uttr.lang = selectedVoice.lang;
}
// 声の高さと速さを少し変えて「別人感」を出す
uttr.pitch = 1.2;
uttr.rate = 1.0;
// 再生!
window.speechSynthesis.speak(uttr);
startBtn.innerText = "🎤 喋って声を変換";
startBtn.style.backgroundColor = "#ff4757";
};
recognition.onerror = () => {
startBtn.innerText = "エラー発生:もう一度押して";
startBtn.style.backgroundColor = "#ff4757";
};
</script>
</body>
</html>
使用変数
| backgroundColor | |
| charset | |
| class | |
| id | |
| innerHTML | |
| innerText | |
| lang | |
| loadVoices -------( Function ) | |
| onclick | |
| onerror | |
| onresult | |
| onvoiceschanged | |
| output | |
| pitch | |
| rate | |
| recognition | |
| selectedVoice | |
| silentUttr | |
| SpeechRecognition | |
| startBtn | |
| transcript | |
| uttr | |
| v | |
| value | |
| voice | |
| voices | |
| voiceSelect |