Spaces:
Running
Running
Update index.html
Browse files116 unmodified lines11711811912012112290 unmodified lines21321421521621721821922022122235 unmodified lines25825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034121 unmodified lines3633643653663673682 unmodified lines37137237337437537615 unmodified lines3923933943953963973983993 unmodified lines403404405406407408409410411412413414113 unmodified lines52852953053153253315 unmodified lines549550551552553554555556557558559560561161 unmodified lines7237247257267277287298 unmodified lines73873974074174274366 unmodified lines81081181281381481581681781881982082182282382482582682782882983083128 unmodified lines860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955626 unmodified lines158215831584158515861587158819 unmodified lines160816091610161116121613161410 unmodified lines1625162616271628162916301631163216335 unmodified lines1639164016411642164316441645164616471648164980 unmodified lines173017311732173317341735173617371738116 unmodified linespadding: 0 2px;transition: all 0.2s;}</style></head><body class="bg-gray-50 min-h-screen">90 unmodified lines</div></div><!-- Voice Selection --><div class="border-t border-gray-200 p-6 bg-gray-50"><div class="flex justify-between items-center mb-4"><h2 class="text-xl font-semibold text-gray-800">Select Google TTS Voice</h2><div class="flex items-center gap-2"><div class="relative"><select id="languageSelect" class="bg-white border border-gray-300 rounded-md py-1 px-3 pr-8 text-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">35 unmodified lines</div></div><!-- Voice Selection Dropdown --><div id="voiceSelectionContainer" class="mb-4"><!-- Voice dropdown will be inserted here --></div><div id="voiceGrid" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"><!-- Voices will be loaded dynamically --><div class="text-center py-8"><div class="loading-spinner mx-auto"></div><p class="text-gray-500 mt-2">Loading Google TTS voices...</p></div></div></div><!-- Voice Controls --><div class="bg-gray-100 p-6"><div class="controls flex flex-wrap justify-between items-center gap-4 mb-4"><div class="voice-options flex flex-wrap items-center gap-4"><div><label for="rateSelect" class="block text-sm font-medium text-gray-700 mb-1">Speed</label><select id="rateSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="0.5">0.5x</option><option value="0.8">0.8x</option><option value="1" selected>1x</option><option value="1.2">1.2x</option><option value="1.5">1.5x</option><option value="2">2x</option></select></div><div><label for="pitchSelect" class="block text-sm font-medium text-gray-700 mb-1">Pitch</label><select id="pitchSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="-20">Low</option><option value="0" selected>Normal</option><option value="20">High</option></select></div><div><label for="modelSelect" class="block text-sm font-medium text-gray-700 mb-1">Voice Model</label><select id="modelSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="studio">Studio Quality - $160.00/1M chars</option><option value="wavenet" selected>WaveNet - $16.00/1M chars</option><option value="neural2">Neural2 - $16.00/1M chars</option><option value="standard">Standard - $4.00/1M chars</option></select></div></div><div class="playback-controls flex items-center gap-2"><button id="playBtn" class="bg-blue-600 hover:bg-blue-700 text-white rounded-full w-12 h-12 flex items-center justify-center transition"><i class="fas fa-play"></i></button><button id="pauseBtn" class="bg-gray-300 hover:bg-gray-400 text-gray-700 rounded-full w-12 h-12 flex items-center justify-center transition" disabled><i class="fas fa-pause"></i></button><button id="stopBtn" class="bg-gray-300 hover:bg-gray-400 text-gray-700 rounded-full w-12 h-12 flex items-center justify-center transition" disabled><i class="fas fa-stop"></i></button><button id="downloadBtn" class="bg-green-600 hover:bg-green-700 text-white rounded-full w-12 h-12 flex items-center justify-center transition" disabled title="Download Audio"><i class="fas fa-download"></i></button></div></div><div class="progress-container"><div class="flex justify-between text-sm text-gray-600 mb-1"><span id="currentTime">0:00</span><span id="totalTime">0:00</span></div><div id="progressContainer" class="w-full bg-gray-200 rounded-full h-4 flex relative cursor-pointer"><!-- Progress bar for audio --><div id="progressBar" class="bg-blue-600 h-4 rounded-full transition-all duration-300" style="width: 0%"></div><!-- Reading progress indicator --><div id="readingProgress" class="absolute inset-0 bg-green-500 bg-opacity-30 h-4 rounded-full transition-all duration-300" style="width: 0%"></div><!-- Current position marker --><div id="currentPositionMarker" class="absolute top-0 w-1 h-4 bg-red-500 transition-all duration-100" style="left: 0%"></div></div></div></div>21 unmodified lineslet currentAudioBlob = null;let currentReadingPosition = 0;let estimatedDuration = 0;const MAX_CHUNK_SIZE = 5000;const PRICING = {2 unmodified linesneural2: 16.00,studio: 160.00};// Global error handlerwindow.addEventListener('error', (e) => {15 unmodified linesconst fileInput = document.getElementById('fileInput');const documentContent = document.getElementById('documentContent');const charCount = document.getElementById('charCount');const voiceGrid = document.getElementById('voiceGrid');const voiceSelectionContainer = document.getElementById('voiceSelectionContainer');const refreshVoicesBtn = document.getElementById('refreshVoicesBtn');const languageSelect = document.getElementById('languageSelect');const rateSelect = document.getElementById('rateSelect');3 unmodified linesconst pauseBtn = document.getElementById('pauseBtn');const stopBtn = document.getElementById('stopBtn');const downloadBtn = document.getElementById('downloadBtn');const currentTime = document.getElementById('currentTime');const totalTime = document.getElementById('totalTime');const progressContainer = document.getElementById('progressContainer');const progressBar = document.getElementById('progressBar');const readingProgress = document.getElementById('readingProgress');const currentPositionMarker = document.getElementById('currentPositionMarker');// Dark mode elementsconst darkModeToggle = document.getElementById('darkModeToggle');113 unmodified lines}}// Auto select voice for modelfunction autoSelectVoiceForModel(model) {if (!availableVoices.length) return;15 unmodified lines}if (preferredVoices.length > 0) {selectedVoice = preferredVoices[0];const voiceSelect = document.getElementById('voiceSelect');if (voiceSelect) {voiceSelect.value = selectedVoice.name;}updateCostEstimator();showToast(`Voice changed to ${selectedVoice.name} for ${model} model`, 'success', 2000);}}161 unmodified linesconst url = URL.createObjectURL(currentAudioBlob);const a = document.createElement('a');a.href = url;a.download = `tts-audio-${new Date().toISOString().split('T')[0]}.mp3`;document.body.appendChild(a);a.click();document.body.removeChild(a);8 unmodified lines// Toast notificationsfunction showToast(message, type = 'success', duration = 3000) {const toast = document.createElement('div');toast.className = `toast ${type}`;toast.textContent = message;66 unmodified lines// Load available voices from Google TTSasync function loadVoices() {if (!apiKey) {voiceGrid.innerHTML = `<div class="col-span-full text-center py-8"><i class="fas fa-key text-gray-400 text-2xl"></i><p class="text-gray-500 mt-2">Please enter your Google Cloud API key</p><p class="text-gray-400 text-sm mt-1">The key must have Text-to-Speech API enabled</p></div>`;return;}voiceGrid.innerHTML = `<div class="col-span-full text-center py-8"><div class="loading-spinner mx-auto"></div><p class="text-gray-500 mt-2">Loading Google TTS voices...</p></div>`;try {const languageCode = languageSelect.value;28 unmodified linesavailableVoices = naturalVoices;if (naturalVoices.length === 0) {voiceGrid.innerHTML = `<div class="col-span-full text-center py-8"><i class="fas fa-microphone-slash text-gray-400 text-2xl"></i><p class="text-gray-500 mt-2">No high-quality voices available for selected language</p></div>`;return;}// Create voice dropdown selectorcreateVoiceDropdown(naturalVoices);// Show success messagevoiceGrid.innerHTML = `<div class="col-span-full text-center py-8"><i class="fas fa-check-circle text-green-500 text-2xl"></i><p class="text-gray-500 mt-2">${naturalVoices.length} voices loaded successfully</p><p class="text-gray-400 text-sm mt-1">Voice can be selected from the dropdown above</p></div>`;// Automatically select the first available voice based on current modelconst currentModel = modelSelect.value || 'wavenet';autoSelectVoiceForModel(currentModel);updateCostEstimator();} catch (error) {console.error('Error loading voices:', error);voiceGrid.innerHTML = `<div class="col-span-full text-center py-8"><i class="fas fa-exclamation-triangle text-red-400 text-2xl"></i><p class="text-gray-500 mt-2">Failed to load voices</p><p class="text-red-500 text-sm mt-1 max-w-md mx-auto">${error.message}</p><button id="retryVoices" class="mt-4 bg-blue-500 hover:bg-blue-600 text-white py-2 px-4 rounded text-sm"><i class="fas fa-sync-alt mr-1"></i> Retry</button></div>`;document.getElementById('retryVoices')?.addEventListener('click', loadVoices);}}// Create voice dropdown selectorfunction createVoiceDropdown(voices) {// Clear existing dropdown if anyvoiceSelectionContainer.innerHTML = '';// Create voice selector dropdownconst voiceDropdownContainer = document.createElement('div');voiceDropdownContainer.className = 'mb-4';voiceDropdownContainer.innerHTML = `<label for="voiceSelect" class="block text-sm font-medium text-gray-700 mb-1">Voice Selection</label><select id="voiceSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 w-full focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="">Select a voice...</option></select>`;voiceSelectionContainer.appendChild(voiceDropdownContainer);const voiceSelect = document.getElementById('voiceSelect');// Populate dropdown with voicesvoices.forEach(voice => {const option = document.createElement('option');option.value = voice.name;option.textContent = `${voice.name} (${voice.ssmlGender})`;voiceSelect.appendChild(option);});// Add event listener for voice selectionvoiceSelect.addEventListener('change', (e) => {const selectedVoiceName = e.target.value;if (selectedVoiceName) {selectedVoice = voices.find(voice => voice.name === selectedVoiceName);updateCostEstimator();showToast(`Voice changed to ${selectedVoice.name}`, 'success', 1500);}});// Auto-select first voiceif (voices.length > 0) {voiceSelect.value = voices[0].name;selectedVoice = voices[0];updateCostEstimator();}}// Synthesize speechasync function synthesizeSpeech(text, voice, rate = 1, pitch = 0, model = 'standard') {if (!apiKey) {throw new Error('API key not provided');626 unmodified lines// Playback controlsplayBtn.addEventListener('click', async () => {if (!selectedVoice) {showToast('Please select a voice from the dropdown', 'error');return;}19 unmodified linesstopPlayback();});// Playback functionsasync function startPlayback() {try {isPlaying = true;10 unmodified lineslet allAudioData = [];for (let i = 0; i < chunks.length; i++) {if (!isPlaying || isPaused) break;const audioData = await synthesizeSpeech(chunks[i],selectedVoice,5 unmodified linesallAudioData.push(audioData);}if (allAudioData.length > 0 && isPlaying) {await combineAndPlayAudio(allAudioData);}} catch (error) {console.error('Error during playback:', error);showToast('Failed to play text: ' + error.message, 'error');stopPlayback();80 unmodified linesdownloadBtn.disabled = !currentAudioBlob;if (isPlaying && !isPaused) {playBtn.innerHTML = '<i class="fas fa-pause"></i>';} else {playBtn.innerHTML = '<i class="fas fa-play"></i>';}}116 unmodified lines11711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218390 unmodified lines27427527627727827928028128228335 unmodified lines31932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843921 unmodified lines4614624634644654664672 unmodified lines47047147247347447547647747847948048148248348448548648715 unmodified lines5035045055065075085095105113 unmodified lines515516517518519520521522523524525526527528529113 unmodified lines64364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567615 unmodified lines692693694695696697698699700701702161 unmodified lines8648658668678688698708718728738748758 unmodified lines88488588688788888989089189266 unmodified lines95996096196296396496596696796896928 unmodified lines998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149626 unmodified lines177617771778177917801781178219 unmodified lines180218031804180518061807180810 unmodified lines1819182018211822182318241825182618271828182918301831183218335 unmodified lines1839184018411842184318441845184618471848184918501851185280 unmodified lines193319341935193619371938193919401941116 unmodified linespadding: 0 2px;transition: all 0.2s;}/* Enhanced voice selector styles */.voice-selector {background: linear-gradient(145deg, #f9fafb, #ffffff);border-radius: 12px;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);}.voice-option {transition: all 0.2s ease;border-radius: 8px;}.voice-option:hover {background-color: #f3f4f6;transform: translateY(-1px);}.voice-option.selected {background-color: #3b82f6;color: white;}.voice-category {border-left: 4px solid #3b82f6;background-color: #f8fafc;}/* Audio controls enhancement */.audio-controls {background: linear-gradient(145deg, #ffffff, #f9fafb);border-radius: 16px;box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);}.control-button {transition: all 0.2s ease;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);}.control-button:hover {transform: translateY(-2px);box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);}.control-button:active {transform: translateY(0);}/* Enhanced progress bar */.progress-bar-container {background: linear-gradient(145deg, #e5e7eb, #f3f4f6);border-radius: 12px;overflow: hidden;box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);}.progress-bar {background: linear-gradient(45deg, #3b82f6, #6366f1);box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);}</style></head><body class="bg-gray-50 min-h-screen">90 unmodified lines</div></div><!-- Enhanced Voice Selection --><div class="border-t border-gray-200 p-6 bg-gray-50"><div class="flex justify-between items-center mb-4"><h2 class="text-xl font-semibold text-gray-800">Voice & Settings</h2><div class="flex items-center gap-2"><div class="relative"><select id="languageSelect" class="bg-white border border-gray-300 rounded-md py-1 px-3 pr-8 text-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500">35 unmodified lines</div></div><!-- Enhanced Voice Selector --><div class="voice-selector p-4 mb-6"><div class="grid grid-cols-1 lg:grid-cols-3 gap-4"><!-- Male Voices --><div class="voice-category p-3 rounded-lg"><h3 class="font-semibold text-gray-700 mb-2 flex items-center"><i class="fas fa-mars text-blue-500 mr-2"></i>Male Voices</h3><div id="maleVoices" class="space-y-2"><!-- Male voices will be populated here --></div></div><!-- Female Voices --><div class="voice-category p-3 rounded-lg"><h3 class="font-semibold text-gray-700 mb-2 flex items-center"><i class="fas fa-venus text-pink-500 mr-2"></i>Female Voices</h3><div id="femaleVoices" class="space-y-2"><!-- Female voices will be populated here --></div></div><!-- Neutral Voices --><div class="voice-category p-3 rounded-lg"><h3 class="font-semibold text-gray-700 mb-2 flex items-center"><i class="fas fa-user text-purple-500 mr-2"></i>Neutral Voices</h3><div id="neutralVoices" class="space-y-2"><!-- Neutral voices will be populated here --></div></div></div></div><!-- Voice Settings Grid --><div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6"><div><label for="rateSelect" class="block text-sm font-medium text-gray-700 mb-1">Speed</label><select id="rateSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 w-full focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="0.5">0.5x (Slow)</option><option value="0.8">0.8x</option><option value="1" selected>1x (Normal)</option><option value="1.2">1.2x</option><option value="1.5">1.5x (Fast)</option><option value="2">2x (Very Fast)</option></select></div><div><label for="pitchSelect" class="block text-sm font-medium text-gray-700 mb-1">Pitch</label><select id="pitchSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 w-full focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="-20">Low (-20st)</option><option value="-10">Lower (-10st)</option><option value="0" selected>Normal (0st)</option><option value="10">Higher (+10st)</option><option value="20">High (+20st)</option></select></div><div><label for="modelSelect" class="block text-sm font-medium text-gray-700 mb-1">Voice Model</label><select id="modelSelect" class="bg-white border border-gray-300 rounded-md py-2 px-3 w-full focus:outline-none focus:ring-blue-500 focus:border-blue-500"><option value="wavenet" selected>WaveNet - $16.00/1M</option><option value="neural2">Neural2 - $16.00/1M</option><option value="studio">Studio - $160.00/1M</option><option value="standard">Standard - $4.00/1M</option></select></div></div></div><!-- Enhanced Audio Controls --><div class="audio-controls p-6 m-4"><div class="flex flex-wrap justify-between items-center gap-4 mb-4"><div class="flex items-center gap-2"><span class="text-sm font-medium text-gray-700">Selected Voice:</span><span id="selectedVoiceName" class="text-sm text-blue-600 font-medium">None</span></div><div class="playback-controls flex items-center gap-3"><button id="playBtn" class="control-button bg-blue-600 hover:bg-blue-700 text-white rounded-full w-14 h-14 flex items-center justify-center transition"><i class="fas fa-play text-lg"></i></button><button id="pauseBtn" class="control-button bg-gray-300 hover:bg-gray-400 text-gray-700 rounded-full w-12 h-12 flex items-center justify-center transition" disabled><i class="fas fa-pause"></i></button><button id="stopBtn" class="control-button bg-gray-300 hover:bg-gray-400 text-gray-700 rounded-full w-12 h-12 flex items-center justify-center transition" disabled><i class="fas fa-stop"></i></button><button id="downloadBtn" class="control-button bg-green-600 hover:bg-green-700 text-white rounded-full w-12 h-12 flex items-center justify-center transition" disabled title="Download Audio"><i class="fas fa-download"></i></button></div></div><!-- Enhanced Progress Container --><div class="progress-container"><div class="flex justify-between text-sm text-gray-600 mb-2"><span id="currentTime">0:00</span><span class="text-xs text-gray-500"><span id="synthesisProgress" class="hidden">Synthesizing... <span id="progressPercentage">0%</span></span></span><span id="totalTime">0:00</span></div><div id="progressContainer" class="progress-bar-container w-full h-4 flex relative cursor-pointer"><!-- Progress bar for audio --><div id="progressBar" class="progress-bar h-full rounded-full transition-all duration-300" style="width: 0%"></div><!-- Reading progress indicator --><div id="readingProgress" class="absolute inset-0 bg-green-500 bg-opacity-30 h-full rounded-full transition-all duration-300" style="width: 0%"></div><!-- Current position marker --><div id="currentPositionMarker" class="absolute top-0 w-1 h-full bg-red-500 transition-all duration-100" style="left: 0%"></div></div></div></div>21 unmodified lineslet currentAudioBlob = null;let currentReadingPosition = 0;let estimatedDuration = 0;let synthesisProgressInterval = null;const MAX_CHUNK_SIZE = 5000;const PRICING = {2 unmodified linesneural2: 16.00,studio: 160.00};// Default male voice preferenceconst defaultMaleVoices = ['en-US-Wavenet-A','en-US-Wavenet-B','en-US-Wavenet-D','en-US-Neural2-A','en-US-Neural2-D','en-US-Standard-A','en-US-Standard-B','en-US-Standard-D'];// Global error handlerwindow.addEventListener('error', (e) => {15 unmodified linesconst fileInput = document.getElementById('fileInput');const documentContent = document.getElementById('documentContent');const charCount = document.getElementById('charCount');const maleVoices = document.getElementById('maleVoices');const femaleVoices = document.getElementById('femaleVoices');const neutralVoices = document.getElementById('neutralVoices');const refreshVoicesBtn = document.getElementById('refreshVoicesBtn');const languageSelect = document.getElementById('languageSelect');const rateSelect = document.getElementById('rateSelect');3 unmodified linesconst pauseBtn = document.getElementById('pauseBtn');const stopBtn = document.getElementById('stopBtn');const downloadBtn = document.getElementById('downloadBtn');const selectedVoiceName = document.getElementById('selectedVoiceName');const currentTime = document.getElementById('currentTime');const totalTime = document.getElementById('totalTime');const progressContainer = document.getElementById('progressContainer');const progressBar = document.getElementById('progressBar');const readingProgress = document.getElementById('readingProgress');const currentPositionMarker = document.getElementById('currentPositionMarker');const synthesisProgress = document.getElementById('synthesisProgress');const progressPercentage = document.getElementById('progressPercentage');// Dark mode elementsconst darkModeToggle = document.getElementById('darkModeToggle');113 unmodified lines}}// Find and select default male voicefunction selectDefaultMaleVoice() {if (!availableVoices.length) return;for (const preferredVoice of defaultMaleVoices) {const voice = availableVoices.find(v => v.name === preferredVoice);if (voice) {selectVoice(voice);return voice;}}// Fallback to first male voice if none of the preferred ones are foundconst maleVoice = availableVoices.find(v => v.ssmlGender === 'MALE');if (maleVoice) {selectVoice(maleVoice);return maleVoice;}// Final fallback to any voiceif (availableVoices.length > 0) {selectVoice(availableVoices[0]);return availableVoices[0];}return null;}// Auto select voice for modelfunction autoSelectVoiceForModel(model) {if (!availableVoices.length) return;15 unmodified lines}if (preferredVoices.length > 0) {// Prefer male voices if availableconst maleVoice = preferredVoices.find(v => v.ssmlGender === 'MALE');const voiceToSelect = maleVoice || preferredVoices[0];selectVoice(voiceToSelect);showToast(`Voice changed to ${voiceToSelect.name} for ${model} model`, 'success', 2000);}}161 unmodified linesconst url = URL.createObjectURL(currentAudioBlob);const a = document.createElement('a');a.href = url;// Create a more descriptive filenameconst timestamp = new Date().toISOString().slice(0, -5).replace(/[T:]/g, '-');const voiceName = selectedVoice ? selectedVoice.name.replace(/[^a-zA-Z0-9]/g, '-') : 'unknown';a.download = `tts-${voiceName}-${timestamp}.mp3`;document.body.appendChild(a);a.click();document.body.removeChild(a);8 unmodified lines// Toast notificationsfunction showToast(message, type = 'success', duration = 3000) {// Remove existing toastsdocument.querySelectorAll('.toast').forEach(toast => toast.remove());const toast = document.createElement('div');toast.className = `toast ${type}`;toast.textContent = message;66 unmodified lines// Load available voices from Google TTSasync function loadVoices() {if (!apiKey) {showLoadingState('Please enter your Google Cloud API key');return;}showLoadingState('Loading Google TTS voices...');try {const languageCode = languageSelect.value;28 unmodified linesavailableVoices = naturalVoices;if (naturalVoices.length === 0) {showErrorState('No high-quality voices available for selected language');return;}// Create categorized voice interfacecreateVoiceInterface(naturalVoices);// Auto-select default male voiceselectDefaultMaleVoice();updateCostEstimator();showToast(`Loaded ${naturalVoices.length} high-quality voices`, 'success');} catch (error) {console.error('Error loading voices:', error);showErrorState(error.message);}}function showLoadingState(message) {[maleVoices, femaleVoices, neutralVoices].forEach(container => {container.innerHTML = `<div class="text-center py-4"><div class="loading-spinner mx-auto mb-2"></div><p class="text-gray-500 text-sm">${message}</p></div>`;});}function showErrorState(message) {[maleVoices, femaleVoices, neutralVoices].forEach(container => {container.innerHTML = `<div class="text-center py-4"><i class="fas fa-exclamation-triangle text-red-400 text-lg mb-2"></i><p class="text-red-500 text-sm">${message}</p></div>`;});}// Create categorized voice interfacefunction createVoiceInterface(voices) {const male = voices.filter(v => v.ssmlGender === 'MALE');const female = voices.filter(v => v.ssmlGender === 'FEMALE');const neutral = voices.filter(v => v.ssmlGender === 'NEUTRAL');maleVoices.innerHTML = male.length ? male.map(voice => createVoiceOption(voice)).join('') : '<p class="text-gray-500 text-sm">No male voices available</p>';femaleVoices.innerHTML = female.length ? female.map(voice => createVoiceOption(voice)).join('') : '<p class="text-gray-500 text-sm">No female voices available</p>';neutralVoices.innerHTML = neutral.length ? neutral.map(voice => createVoiceOption(voice)).join('') : '<p class="text-gray-500 text-sm">No neutral voices available</p>';// Add click listeners to all voice optionsdocument.querySelectorAll('.voice-option').forEach(option => {option.addEventListener('click', () => {const voiceName = option.dataset.voiceName;const voice = voices.find(v => v.name === voiceName);if (voice) {selectVoice(voice);}});});}function createVoiceOption(voice) {const voiceType = getVoiceTypeAndPricing(voice.name);const displayName = voice.name.replace(/-/g, ' ').replace(/en US /i, '').replace(/Wavenet|Neural2|Studio/i, '');return `<div class="voice-option p-3 border border-gray-200 rounded-lg cursor-pointer" data-voice-name="${voice.name}"><div class="flex justify-between items-center"><div><div class="font-medium text-gray-800">${displayName}</div><div class="text-xs text-gray-500">${voiceType.name} • ${voice.languageCodes[0]}</div></div><div class="text-right"><div class="text-xs text-blue-600">$${voiceType.rate}/1M</div><button class="sample-btn text-xs text-gray-600 hover:text-blue-600 mt-1"><i class="fas fa-play text-xs"></i> Sample</button></div></div></div>`;}function selectVoice(voice) {selectedVoice = voice;selectedVoiceName.textContent = voice.name;// Update UI selection statedocument.querySelectorAll('.voice-option').forEach(option => {option.classList.remove('selected');});const selectedOption = document.querySelector(`[data-voice-name="${voice.name}"]`);if (selectedOption) {selectedOption.classList.add('selected');}updateCostEstimator();showToast(`Selected voice: ${voice.name}`, 'success', 1500);// Add sample button click listenerconst sampleBtn = selectedOption?.querySelector('.sample-btn');if (sampleBtn) {sampleBtn.addEventListener('click', (e) => {e.stopPropagation();playSample(voice);});}}// Play voice sampleasync function playSample(voice) {if (!apiKey) {showToast('Please enter your API key first', 'error');return;}if (isPlaying) {stopPlayback();}try {showSynthesisProgress(0);const sampleText = "Hello, this is a sample of my voice. I can read your documents with natural sounding speech.";const audioData = await synthesizeSpeech(sampleText, voice, 1, 0, 'wavenet');hideSynthesisProgress();playAudioData(audioData);} catch (error) {hideSynthesisProgress();console.error('Error playing sample:', error);showToast('Failed to play sample: ' + error.message, 'error');}}// Show/hide synthesis progressfunction showSynthesisProgress(progress) {synthesisProgress.classList.remove('hidden');progressPercentage.textContent = `${Math.round(progress)}%`;}function hideSynthesisProgress() {synthesisProgress.classList.add('hidden');}// Synthesize speech with progress trackingasync function synthesizeSpeech(text, voice, rate = 1, pitch = 0, model = 'standard') {if (!apiKey) {throw new Error('API key not provided');626 unmodified lines// Playback controlsplayBtn.addEventListener('click', async () => {if (!selectedVoice) {showToast('Please select a voice first', 'error');return;}19 unmodified linesstopPlayback();});// Playback functions with enhanced progress trackingasync function startPlayback() {try {isPlaying = true;10 unmodified lineslet allAudioData = [];// Show synthesis progressshowSynthesisProgress(0);for (let i = 0; i < chunks.length; i++) {if (!isPlaying || isPaused) break;const progress = (i / chunks.length) * 100;showSynthesisProgress(progress);const audioData = await synthesizeSpeech(chunks[i],selectedVoice,5 unmodified linesallAudioData.push(audioData);}hideSynthesisProgress();if (allAudioData.length > 0 && isPlaying) {await combineAndPlayAudio(allAudioData);}} catch (error) {hideSynthesisProgress();console.error('Error during playback:', error);showToast('Failed to play text: ' + error.message, 'error');stopPlayback();80 unmodified linesdownloadBtn.disabled = !currentAudioBlob;if (isPlaying && !isPaused) {playBtn.innerHTML = '<i class="fas fa-pause text-lg"></i>';} else {playBtn.innerHTML = '<i class="fas fa-play text-lg"></i>';}}
index.html+356-153