簡単文章読み上げソフトの導入
ずっと目を使ってると疲れてくるときに、読まなきゃいけない資料を耳で聞ければという場面がしばしばあります。
そういう時に「文章読み上げソフト」とかで調べて適当なサービスやソフトを探すのですが、どれも使いづらかったり、入力文字数に制限があったりとかゆいところに手が届かないので、AIを使って自分で作ってみました。

こんな特徴があります。
・メモ帳(Windowsに入ってるやつ)を使用・入力文字数制限なし・コピペしたらすぐ読み上げ、音声ファイルの生成をしない・読み上げ速度、文字サイズ、読み上げ音声を変更できる
おかげで目を休めながらインプットできます
導入の仕方
①Windowsの「メモ帳」を立ち上げる
(Macでも動くと報告を受けています)

②↓のコードを開いたメモ帳に張り付ける
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>リアルタイム テキスト読み上げツール</title> <style> /* デザインのためのCSS */ body { padding: 20px; background-color: #f0f0f0 ; } .tts-container { border: 1px solid #ccc ; border-radius: 8px; padding: 20px; background-color: #f9f9f9 ; font-family: sans-serif; /* ツール全体の横幅をブラウザの80%に設定 */ width: 80%; margin: auto; } .tts-textarea { /* 横幅は親要素(tts-container)の98% */ width: 98%; height: 200px; margin-bottom: 15px; } .tts-controls { display: flex; align-items: center; gap: 20px; /* 間隔を調整 */ margin-bottom: 15px; flex-wrap: wrap; } .tts-button { padding: 10px 15px; font-size: 14px; border: none; border-radius: 5px; cursor: pointer; color: white; } .tts-button:disabled { cursor: not-allowed; opacity: 0.6; } .play-btn { background-color: #4CAF50 ; } .pause-btn { background-color: #FF9800 ; } .resume-btn { background-color: #008CBA ; } .stop-btn { background-color: #f44336 ; } .tts-status { font-weight: bold; color: #555 ; } #voice -select, #font -select { padding: 5px; border-radius: 4px; } </style> </head> <body> <div class="tts-container"> <h2>リアルタイム テキスト読み上げツール</h2> <textarea id="tts-text" class="tts-textarea">ここに < > 記号を含む文章を貼り付けても、問題なく読み上げられます。下のプルダウンから声も選べます。</textarea> <div class="tts-controls"> <div> <label for="rate">速度:</label> <input type="range" min="0.5" max="2" value="1" step="0.1" id="rate"> <span id="rate-label">1.0</span> </div> <div> <label for="font-size">文字サイズ:</label> <input type="range" min="10" max="24" value="14" step="1" id="font-size"> <span id="font-size-label">14</span>px </div> <div> <label for="font-select">フォント:</label> <select id="font-select"> <option value="'Helvetica Neue', 'Arial', 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', 'Meiryo', sans-serif">標準 (ゴシック)</option> <option value="'BIZ UDPGothic', 'Yu Gothic', 'YuGothic', sans-serif">BIZ UDPゴシック</option> <option value="'UD Digi Kyokasho N-R', 'sans-serif'">UD デジタル教科書体</option> <option value="'Times New Roman', 'Hiragino Mincho ProN', 'MS PMincho', serif">標準 (明朝)</option> <option value="'BIZ UDPMincho', serif">BIZ UDP明朝</option> </select> </div> <div> <label for="voice-select">声の種類:</label> <select id="voice-select"><option>読み込み中...</option></select> </div> </div> <div> <button id="play-btn" class="tts-button play-btn">▶️ 読み上げ開始</button> <button id="pause-btn" class="tts-button pause-btn" disabled>⏸️ 一時停止</button> <button id="resume-btn" class="tts-button resume-btn" disabled>⏯️ 再開</button> <button id="stop-btn" class="tts-button stop-btn" disabled>⏹️ 停止</button> </div> <p class="tts-status">状態: <span id="status">待機中</span></p> </div> <script> // --- HTML要素の取得 --- const textEl = document.getElementById('tts-text'); const playBtn = document.getElementById('play-btn'); const pauseBtn = document.getElementById('pause-btn'); const resumeBtn = document.getElementById('resume-btn'); const stopBtn = document.getElementById('stop-btn'); const rateEl = document.getElementById('rate'); const rateLabelEl = document.getElementById('rate-label'); const statusEl = document.getElementById('status'); const voiceSelectEl = document.getElementById('voice-select'); const fontSizeEl = document.getElementById('font-size'); const fontSizeLabelEl = document.getElementById('font-size-label'); const fontSelectEl = document.getElementById('font-select'); // フォント選択のHTML要素を取得 // --- グローバル変数の定義 --- let utterance = null; // 現在の読み上げオブジェクト let voices = []; // 利用可能な音声リスト let voiceInterval; // 音声リスト取得のためのタイマー let knownVoiceCount = 0; // 発見済みの音声の数 let originalText = ""; // 読み上げ対象の元のテキスト let resumeCharIndex = 0; // 読み上げを再開する文字の位置 /** * @function populateVoiceList * @description 利用可能な日本語の音声を取得し、ドロップダウンリストを更新する */ function populateVoiceList() { // 'ja'で始まる言語の音声のみをフィルタリング voices = speechSynthesis.getVoices().filter(voice => voice.lang.startsWith('ja')); // 新しい音声が見つからない場合は処理を中断 if (voices.length === knownVoiceCount) return; knownVoiceCount = voices.length; // 見つけた音声の数を更新 if (voices.length > 0) { const currentSelection = voiceSelectEl.value; // 現在選択中の値を保持 voiceSelectEl.innerHTML = ''; // ドロップダウンをクリア // 見つかった音声でドロップダウンを作成 voices.forEach(voice => { const option = document.createElement('option'); option.textContent = `${voice.name} (${voice.lang})`; option.setAttribute('data-name', voice.name); voiceSelectEl.appendChild(option); }); // リスト更新前に選択していた項目があれば、それを再度選択 if (currentSelection) voiceSelectEl.value = currentSelection; } else { // 利用可能な音声がまだない場合 voiceSelectEl.innerHTML = '<option>音声リストを読み込み中...</option>'; } } // --- 音声リストの読み込み処理 --- // 音声リストが変更されたら(非同期)、リストを更新 speechSynthesis.onvoiceschanged = populateVoiceList; // 念のため、5秒間は0.25秒ごとにリストをチェックし続ける (ChromeのWeb音声対策) voiceInterval = setInterval(populateVoiceList, 250); setTimeout(() => { clearInterval(voiceInterval); }, 5000); populateVoiceList(); // 初回実行 /** * @function speakFrom * @description 指定された位置から読み上げを開始する共通関数 * @param {number} startIndex - 読み上げを開始する文字インデックス */ function speakFrom(startIndex) { // 既存のutterance(読み上げ)が存在する場合、 // cancel()によるonend(終了)イベントの暴発を防ぐため、 // 先にイベントハンドラをすべて無効化(nullに)する if (utterance) { utterance.onend = null; utterance.onerror = null; utterance.onboundary = null; } // 既存の読み上げキューをキャンセル speechSynthesis.cancel(); // 読み上げるテキストを、指定された開始位置から最後まで切り出す const textToSpeak = originalText.substring(startIndex); if (textToSpeak.trim().length === 0) { // テキストが空なら待機中に戻す updateUI('待機中'); return; } // 新しい読み上げオブジェクトをグローバル変数に(再)代入 utterance = new SpeechSynthesisUtterance(textToSpeak); // --- 読み上げオブジェクトの設定 --- // 1. 声の設定 if (voiceSelectEl.selectedOptions && voiceSelectEl.selectedOptions.length > 0) { const selectedVoiceName = voiceSelectEl.selectedOptions[0].getAttribute('data-name'); const selectedVoice = voices.find(voice => voice.name === selectedVoiceName); if (selectedVoice) utterance.voice = selectedVoice; } // 2. 速度の設定 (スライダーの現在の値) utterance.rate = parseFloat(rateEl.value); // 3. イベントハンドラの設定 // 読み上げ境界(単語ごとなど)で発火し、現在の文字位置を保存 utterance.onboundary = (event) => { resumeCharIndex = startIndex + event.charIndex; }; // 読み上げが正常に終了したら発火 utterance.onend = () => { resumeCharIndex = 0; // 再開位置をリセット updateUI('待機中'); }; // エラーが発生したら発火 utterance.onerror = (event) => { console.error('SpeechSynthesisUtterance.onerror', event); statusEl.textContent = 'エラーが発生しました: ' + event.error; updateUI('待機中'); }; // 読み上げ実行 speechSynthesis.speak(utterance); updateUI('読み上げ中'); } // --- ボタンのイベントリスナー --- // "読み上げ開始" ボタン playBtn.addEventListener('click', () => { originalText = textEl.value.replace(/<|>/g, ' '); // テキストを保存 resumeCharIndex = 0; // 最初から speakFrom(resumeCharIndex); // 0の位置から読み上げ開始 }); // "一時停止" ボタン pauseBtn.addEventListener('click', () => { speechSynthesis.pause(); updateUI('一時停止中'); }); // "再開" ボタン resumeBtn.addEventListener('click', () => { // onboundaryで保存された位置(resumeCharIndex)から読み上げを再実行 speakFrom(resumeCharIndex); }); // "停止" ボタン stopBtn.addEventListener('click', () => { // 停止時も、onendが暴発しないようにハンドラを無効化 if (utterance) { utterance.onend = null; } speechSynthesis.cancel(); resumeCharIndex = 0; // 再開位置をリセット updateUI('待機中'); }); // --- スライダーのイベントリスナー --- // 速度スライダー rateEl.addEventListener('input', () => { const newRate = parseFloat(rateEl.value); rateLabelEl.textContent = newRate.toFixed(1); // 読み上げ中(speaking)であれば、速度を即時反映 if (utterance && speechSynthesis.speaking) { utterance.rate = newRate; } // 一時停止中は、speakFrom()がスライダーの値を参照するので何もしない }); /** * @function applyStyles * @description テキストエリアのフォントサイズとフォントファミリーを適用する */ function applyStyles() { // 文字サイズを適用 const fontSize = fontSizeEl.value; fontSizeLabelEl.textContent = fontSize; textEl.style.fontSize = fontSize + 'px'; // フォントファミリーを適用 const fontName = fontSelectEl.value; textEl.style.fontFamily = fontName; } // スライダーが動いたときにスタイルを適用 fontSizeEl.addEventListener('input', applyStyles); fontSelectEl.addEventListener('input', applyStyles); // 初回読み込み時に実行 applyStyles(); /** * @function updateUI * @description 状態に応じてボタンの有効/無効を切り替える * @param {string} state - "待機中", "読み上げ中", "一時停止中" のいずれか */ function updateUI(state) { statusEl.textContent = state; playBtn.disabled = (state === '読み上げ中' || state === '一時停止中'); pauseBtn.disabled = (state !== '読み上げ中'); resumeBtn.disabled = (state !== '一時停止中'); stopBtn.disabled = (state === '待機中'); } // (無効化) 状態監視タイマー /* setInterval(() => { if (!speechSynthesis.speaking && !speechSynthesis.paused) { if (statusEl.textContent !== '待機中') { updateUI('待機中'); } } }, 500); */ </script> </body> </html>
↑の引用部分を全部コピーしてメモ帳に貼り付け
③名前を付けて保存
コードを貼り付けたメモ帳ファイルに「yomiage.html」という名前で保存します。

④保存したyomiage.htmlをダブルクリック
作成したhtmlファイルを立ち上げるとデフォルトで使っているブラウザ(EdgeやChromeなど)で読み上げソフトが立ち上がります。
⑤読み上げてほしいテキストを貼り付け、「読み上げ開始」
これで読み上げが始まります!
いじれる所は↓です。
・読み上げ速度(0~2.0倍)
・文字のサイズとフォント
・読み上げる音声の種類
・入力スペースのサイズ

入力スペースのサイズは少しわかりづらいですが、他は直感的に使えるかと思います。
読み上げ速度は一時停止してから再開すると反映されます。
一度yomiage.htmlを作成した後は、このファイルをまたクリックすれば開けます。
自分が開きやすいところにファイルを置いておけばすぐに音声読み上げを使えます。
何か不明点がありましたらコメントで教えてください♪
※この記事の情報に基づくいかなる損害も責任を負いかねますので、ご自身の責任のもとで実行するようにしてください。