feat(tts): multi-voice switcher for EN & RU, Irina default voice, punctuation pause normalization and speed calibration
This commit is contained in:
1 parent
7893b353aa
commit
31e105b2c2
11 files changed
+486
-61
No files matched your search
+69
-1
@@ -4,7 +4,7 @@
|
||||
// =======================================================================
|
||||
|
||||
import { api } from './modules/api.js';
|
||||
import { initTheme, openModal, closeModal, showToast } from './modules/ui.js';
|
||||
import { initTheme, openModal, closeModal, showToast, playTextSpeech } from './modules/ui.js';
|
||||
import { initDictionary, loadInitialData } from './modules/dictionary.js';
|
||||
import { initSrs, startSrsTrainingSession, openStatsDashboard } from './modules/srs.js';
|
||||
import { initReader, loadBookshelf, openBook } from './modules/reader.js';
|
||||
@@ -546,6 +546,14 @@ export function initNavigationDrawer() {
|
||||
themeToggle.click();
|
||||
});
|
||||
}
|
||||
const drawerNavVoiceSettings = document.getElementById('drawer-nav-voice-settings');
|
||||
const voiceSettingsModal = document.getElementById('voice-settings-modal');
|
||||
if (drawerNavVoiceSettings && voiceSettingsModal) {
|
||||
drawerNavVoiceSettings.addEventListener('click', () => {
|
||||
closeDrawer();
|
||||
openVoiceSettingsModal();
|
||||
});
|
||||
}
|
||||
if (drawerNavLogout && logoutBtn) {
|
||||
drawerNavLogout.addEventListener('click', () => {
|
||||
closeDrawer();
|
||||
@@ -554,6 +562,63 @@ export function initNavigationDrawer() {
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Voice Settings Modal Controller (TTS)
|
||||
// -----------------------------------------------------------------------
|
||||
function openVoiceSettingsModal() {
|
||||
const voiceSettingsModal = document.getElementById('voice-settings-modal');
|
||||
if (!voiceSettingsModal) return;
|
||||
const currentRu = localStorage.getItem('notes_tts_voice_ru') || 'ru_RU-irina';
|
||||
const currentEn = localStorage.getItem('notes_tts_voice_en') || 'en_US-lessac';
|
||||
const ruInput = voiceSettingsModal.querySelector(`input[name="tts_voice_ru"][value="${currentRu}"]`);
|
||||
if (ruInput) ruInput.checked = true;
|
||||
const enInput = voiceSettingsModal.querySelector(`input[name="tts_voice_en"][value="${currentEn}"]`);
|
||||
if (enInput) enInput.checked = true;
|
||||
openModal(voiceSettingsModal);
|
||||
}
|
||||
|
||||
function initVoiceSettingsModal() {
|
||||
const voiceSettingsModal = document.getElementById('voice-settings-modal');
|
||||
const voiceSettingsCloseBtn = document.getElementById('voice-settings-close-btn');
|
||||
const voiceSettingsSaveBtn = document.getElementById('voice-settings-save-btn');
|
||||
|
||||
if (voiceSettingsCloseBtn && voiceSettingsModal) {
|
||||
voiceSettingsCloseBtn.addEventListener('click', () => closeModal(voiceSettingsModal));
|
||||
}
|
||||
|
||||
if (voiceSettingsSaveBtn && voiceSettingsModal) {
|
||||
voiceSettingsSaveBtn.addEventListener('click', () => {
|
||||
const ruVal = voiceSettingsModal.querySelector('input[name="tts_voice_ru"]:checked')?.value || 'ru_RU-irina';
|
||||
const enVal = voiceSettingsModal.querySelector('input[name="tts_voice_en"]:checked')?.value || 'en_US-lessac';
|
||||
localStorage.setItem('notes_tts_voice_ru', ruVal);
|
||||
localStorage.setItem('notes_tts_voice_en', enVal);
|
||||
|
||||
// Sync with listening selector if on screen
|
||||
const listeningVoiceSelect = document.getElementById('listening-voice-select');
|
||||
if (listeningVoiceSelect) {
|
||||
listeningVoiceSelect.value = enVal;
|
||||
}
|
||||
|
||||
closeModal(voiceSettingsModal);
|
||||
showToast('✅ Голоса дикторов сохранены!', 'success');
|
||||
});
|
||||
}
|
||||
|
||||
// Voice test buttons
|
||||
if (voiceSettingsModal) {
|
||||
voiceSettingsModal.querySelectorAll('.voice-test-btn').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const lang = btn.dataset.lang || 'en';
|
||||
const voice = btn.dataset.voice;
|
||||
const text = btn.dataset.text || 'Test voice';
|
||||
playTextSpeech(text, lang, 1.0, voice);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Password Change Modal
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -695,6 +760,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
// 7. Change Password Modal
|
||||
initChangePasswordModal();
|
||||
|
||||
// 7b. Voice Settings Modal (TTS)
|
||||
initVoiceSettingsModal();
|
||||
|
||||
// 8. Hash Change Listener
|
||||
window.addEventListener('hashchange', handleRouting);
|
||||
|
||||
|
||||
@@ -185,8 +185,12 @@ export const api = {
|
||||
|
||||
// Listening Comprehension Trainer
|
||||
listening: {
|
||||
async getExercise(topic = 'all', level = 'B1') {
|
||||
const resp = await fetch(`/api/listening/exercise?topic=${encodeURIComponent(topic)}&level=${encodeURIComponent(level)}`);
|
||||
async getExercise(topic = 'all', level = 'B1', voice = null) {
|
||||
let url = `/api/listening/exercise?topic=${encodeURIComponent(topic)}&level=${encodeURIComponent(level)}`;
|
||||
if (voice) {
|
||||
url += `&voice=${encodeURIComponent(voice)}`;
|
||||
}
|
||||
const resp = await fetch(url);
|
||||
if (resp.ok) return await resp.json();
|
||||
throw new Error(`Failed to load listening exercise: ${resp.status}`);
|
||||
},
|
||||
@@ -206,6 +210,14 @@ export const api = {
|
||||
}
|
||||
},
|
||||
|
||||
// Neural Speech Synthesis (TTS)
|
||||
tts: {
|
||||
async getStatus() {
|
||||
const resp = await fetch('/api/tts/status');
|
||||
return resp.ok ? await resp.json() : null;
|
||||
}
|
||||
},
|
||||
|
||||
// Bookshelf & Reader
|
||||
books: {
|
||||
async getAll() {
|
||||
|
||||
@@ -111,6 +111,7 @@ export function initSrs() {
|
||||
const listeningLevelBadge = document.getElementById('listening-level-badge');
|
||||
const listeningTopicBadge = document.getElementById('listening-topic-badge');
|
||||
const listeningTopicSelect = document.getElementById('listening-topic-select');
|
||||
const listeningVoiceSelect = document.getElementById('listening-voice-select');
|
||||
const listeningSpeedSelect = document.getElementById('listening-speed-select');
|
||||
const listeningNewScenarioBtn = document.getElementById('listening-new-scenario-btn');
|
||||
const listeningTitle = document.getElementById('listening-title');
|
||||
@@ -715,8 +716,9 @@ export function initSrs() {
|
||||
if (listeningTitle) listeningTitle.textContent = 'Загрузка аудио-ситуации...';
|
||||
|
||||
const topic = listeningTopicSelect ? listeningTopicSelect.value : 'all';
|
||||
const voice = listeningVoiceSelect ? listeningVoiceSelect.value : (localStorage.getItem('notes_tts_voice_en') || 'en_US-lessac');
|
||||
try {
|
||||
const data = await api.listening.getExercise(topic, 'B1');
|
||||
const data = await api.listening.getExercise(topic, 'B1', voice);
|
||||
currentListeningExercise = data;
|
||||
|
||||
if (listeningTitle) listeningTitle.textContent = data.title || 'Ситуация для аудирования';
|
||||
@@ -788,6 +790,14 @@ export function initSrs() {
|
||||
});
|
||||
}
|
||||
|
||||
if (listeningVoiceSelect) {
|
||||
listeningVoiceSelect.value = localStorage.getItem('notes_tts_voice_en') || 'en_US-lessac';
|
||||
listeningVoiceSelect.addEventListener('change', () => {
|
||||
localStorage.setItem('notes_tts_voice_en', listeningVoiceSelect.value);
|
||||
loadListeningExercise(true);
|
||||
});
|
||||
}
|
||||
|
||||
if (listeningForm) {
|
||||
listeningForm.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -194,8 +194,14 @@ export function playAudio(url, speed = 1.0) {
|
||||
}
|
||||
}
|
||||
|
||||
export function playTextSpeech(text, lang = 'en', speed = 1.0) {
|
||||
export function playTextSpeech(text, lang = 'en', speed = 1.0, voice = null) {
|
||||
if (!text || !text.trim()) return null;
|
||||
const url = `/api/tts?text=${encodeURIComponent(text.trim())}&lang=${lang}&speed=${speed}`;
|
||||
const isRu = lang && lang.toLowerCase().startsWith('ru');
|
||||
const savedVoice = isRu ? localStorage.getItem('notes_tts_voice_ru') : localStorage.getItem('notes_tts_voice_en');
|
||||
const activeVoice = voice || savedVoice;
|
||||
let url = `/api/tts?text=${encodeURIComponent(text.trim())}&lang=${lang}&speed=${speed}`;
|
||||
if (activeVoice) {
|
||||
url += `&voice=${encodeURIComponent(activeVoice)}`;
|
||||
}
|
||||
return playAudio(url, speed);
|
||||
}
|
||||
Reference in new issue
Block a user