Refactored modules and updated loader.

This commit is contained in:
2025-04-06 18:35:04 +00:00
parent fc693ae695
commit 0ab639fd25
37 changed files with 3530 additions and 5989 deletions
+25 -30
View File
@@ -6,7 +6,7 @@ import { ApiTTSModuleBase } from './api-tts-module-base.js';
export class ElevenLabsTTSModule extends ApiTTSModuleBase {
constructor() {
super('elevenlabs', 'ElevenLabs TTS');
super('elevenlabs-tts', 'ElevenLabs TTS');
// Voice options specific to ElevenLabs
this.voiceOptions = {
@@ -112,35 +112,35 @@ export class ElevenLabsTTSModule extends ApiTTSModuleBase {
const response = await fetch(`${this.apiBaseUrl}/voices`, {
method: 'GET',
headers: {
'xi-api-key': apiKey,
'Content-Type': 'application/json'
'Accept': 'application/json',
'xi-api-key': apiKey
}
});
if (!response.ok) {
console.error(`ElevenLabs TTS: API error: ${response.status} ${response.statusText}`);
return true; // Use defaults, but don't fail initialization
console.error(`ElevenLabs TTS: API error ${response.status} ${response.statusText}`);
return true; // Continue with default voices
}
const data = await response.json();
if (data && data.voices && Array.isArray(data.voices)) {
// Transform API response to our internal format
// Map API voices to our format
this.voices = data.voices.map(voice => ({
id: voice.voice_id,
name: voice.name,
language: 'en', // ElevenLabs doesn't provide language info
preview: voice.preview_url
language: voice.language || 'en',
gender: 'unknown',
preview_url: voice.preview_url
}));
return true;
}
return true; // Continue with default voices
} catch (error) {
console.error('ElevenLabs TTS: Error loading voices:', error);
return true; // Continue with default voices
}
// If API call failed, we still return true since we have default voices
return true;
}
/**
@@ -149,12 +149,18 @@ export class ElevenLabsTTSModule extends ApiTTSModuleBase {
* @returns {boolean} - Success status
*/
selectVoiceForLocale(locale) {
if (!this.voices || this.voices.length === 0) {
return this.selectDefaultVoice();
// Extract language code from locale (e.g., 'en-US' -> 'en')
const langCode = locale.split('-')[0].toLowerCase();
// For English locales, select 'Rachel' if available
if (langCode === 'en') {
const defaultVoice = this.voices.find(v => v.id === 'pNInz6obpgDQGcFmaJgB');
if (defaultVoice) {
this.voiceOptions.voice = defaultVoice.id;
return true;
}
}
// ElevenLabs doesn't provide language info for voices
// Simply use the first voice as default
return this.selectDefaultVoice();
}
@@ -254,17 +260,6 @@ export class ElevenLabsTTSModule extends ApiTTSModuleBase {
}
}
// Register the module with the module registry
// Module registry MUST be accessed via window, not direct import
if (window.moduleRegistry) {
try {
// Create instance first, then register it
const elevenLabsTTSModule = new ElevenLabsTTSModule();
window.moduleRegistry.register(elevenLabsTTSModule);
console.log('ElevenLabs TTS Module registered successfully');
} catch (err) {
console.error('Failed to register ElevenLabs TTS Module:', err);
}
} else {
console.error('Module registry not available when attempting to register ElevenLabs TTS Module');
}
const elevenLabsTTSModule = new ElevenLabsTTSModule();
export { elevenLabsTTSModule };