Spaces:
Runtime error
Runtime error
File size: 8,426 Bytes
05b45a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
import AudioService from './services/AudioService.js';
import VoiceService from './services/VoiceService.js';
import PlayerState from './state/PlayerState.js';
import PlayerControls from './components/PlayerControls.js';
import VoiceSelector from './components/VoiceSelector.js';
import WaveVisualizer from './components/WaveVisualizer.js';
import TextEditor from './components/TextEditor.js';
export class App {
constructor() {
this.elements = {
generateBtn: document.getElementById('generate-btn'),
generateBtnText: document.querySelector('#generate-btn .btn-text'),
generateBtnLoader: document.querySelector('#generate-btn .loader'),
downloadBtn: document.getElementById('download-btn'),
autoplayToggle: document.getElementById('autoplay-toggle'),
formatSelect: document.getElementById('format-select'),
status: document.getElementById('status'),
cancelBtn: document.getElementById('cancel-btn')
};
this.initialize();
}
async initialize() {
// Initialize services and state
this.playerState = new PlayerState();
this.audioService = new AudioService();
this.voiceService = new VoiceService();
// Initialize components
this.playerControls = new PlayerControls(this.audioService, this.playerState);
this.voiceSelector = new VoiceSelector(this.voiceService);
this.waveVisualizer = new WaveVisualizer(this.playerState);
// Initialize text editor
const editorContainer = document.getElementById('text-editor');
this.textEditor = new TextEditor(editorContainer, {
linesPerPage: 20,
onTextChange: (text) => {
// Optional: Handle text changes here if needed
console.log('Text changed:', text);
}
});
// Initialize voice selector
const voicesLoaded = await this.voiceSelector.initialize();
if (!voicesLoaded) {
this.showStatus('Failed to load voices', 'error');
this.elements.generateBtn.disabled = true;
return;
}
this.setupEventListeners();
this.setupAudioEvents();
}
setupEventListeners() {
// Generate button
this.elements.generateBtn.addEventListener('click', () => this.generateSpeech());
// Download button
this.elements.downloadBtn.addEventListener('click', () => this.downloadAudio());
// Cancel button
this.elements.cancelBtn.addEventListener('click', () => {
this.audioService.cancel();
this.setGenerating(false);
this.elements.downloadBtn.classList.remove('ready');
this.showStatus('Generation cancelled', 'info');
});
// Handle page unload
window.addEventListener('beforeunload', () => {
this.audioService.cleanup();
this.playerControls.cleanup();
this.waveVisualizer.cleanup();
});
}
setupAudioEvents() {
// Handle download button visibility
this.audioService.addEventListener('downloadReady', () => {
this.elements.downloadBtn.classList.add('ready');
});
// Handle buffer errors
this.audioService.addEventListener('bufferError', () => {
this.showStatus('Processing... (Download will be available when complete)', 'info');
});
// Handle completion
this.audioService.addEventListener('complete', () => {
this.setGenerating(false);
// Show preparing status
this.showStatus('Preparing file...', 'info');
// Trigger coffee steam animation
const steamElement = document.querySelector('.cup .steam');
if (steamElement) {
// Remove and re-add the element to restart animation
const parent = steamElement.parentNode;
const clone = steamElement.cloneNode(true);
parent.removeChild(steamElement);
parent.appendChild(clone);
}
});
// Handle download ready
this.audioService.addEventListener('downloadReady', () => {
setTimeout(() => {
this.showStatus('Generation complete', 'success');
}, 500); // Small delay to ensure "Preparing file..." is visible
});
// Handle audio end
this.audioService.addEventListener('ended', () => {
this.playerState.setPlaying(false);
});
// Handle errors
this.audioService.addEventListener('error', (error) => {
this.showStatus('Error: ' + error.message, 'error');
this.setGenerating(false);
this.elements.downloadBtn.style.display = 'none';
});
}
showStatus(message, type = 'info') {
this.elements.status.textContent = message;
this.elements.status.className = 'status ' + type;
setTimeout(() => {
this.elements.status.className = 'status';
}, 5000);
}
setGenerating(isGenerating) {
this.playerState.setGenerating(isGenerating);
this.elements.generateBtn.disabled = isGenerating;
this.elements.generateBtn.classList.toggle('loading', isGenerating);
this.elements.generateBtnLoader.style.display = isGenerating ? 'block' : 'none';
this.elements.generateBtnText.style.visibility = isGenerating ? 'hidden' : 'visible';
this.elements.cancelBtn.style.display = isGenerating ? 'block' : 'none';
}
validateInput() {
const text = this.textEditor.getText().trim();
if (!text) {
this.showStatus('Please enter some text', 'error');
return false;
}
if (!this.voiceService.hasSelectedVoices()) {
this.showStatus('Please select a voice', 'error');
return false;
}
return true;
}
async generateSpeech() {
// Don't check isGenerating state since we want to allow generation after cancel
if (!this.validateInput()) {
return;
}
const text = this.textEditor.getText().trim();
const voice = this.voiceService.getSelectedVoiceString();
const speed = this.playerState.getState().speed;
this.setGenerating(true);
this.elements.downloadBtn.classList.remove('ready');
// Just reset progress bar, don't do full cleanup
this.waveVisualizer.updateProgress(0, 1);
try {
console.log('Starting audio generation...', { text, voice, speed });
// Ensure we have valid input
if (!text || !voice) {
console.error('Invalid input:', { text, voice, speed });
throw new Error('Invalid input parameters');
}
await this.audioService.streamAudio(
text,
voice,
speed,
(loaded, total) => {
console.log('Progress update:', { loaded, total });
this.waveVisualizer.updateProgress(loaded, total);
}
);
} catch (error) {
console.error('Generation error:', error);
if (error.name !== 'AbortError') {
this.showStatus('Error generating speech: ' + error.message, 'error');
this.setGenerating(false);
}
}
}
downloadAudio() {
const downloadUrl = this.audioService.getDownloadUrl();
if (!downloadUrl) {
console.warn('No download URL available');
return;
}
console.log('Starting download from:', downloadUrl);
const format = this.elements.formatSelect.value;
const voice = this.voiceService.getSelectedVoiceString();
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const a = document.createElement('a');
a.href = downloadUrl;
a.download = `${voice}_${timestamp}.${format}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new App();
});
|