Fix TTS module initialization and dependency issues. Update module IDs for consistency, improve circular dependency detection, and fix UI Controller event handling.
This commit is contained in:
+38
-35
@@ -23,7 +23,7 @@ self.onmessage = function(e) {
|
||||
break;
|
||||
|
||||
case 'generate':
|
||||
if (!message.data || !message.data.text) {
|
||||
if (!message.text) {
|
||||
self.postMessage({
|
||||
type: 'error',
|
||||
error: 'No text provided for generation'
|
||||
@@ -32,11 +32,17 @@ self.onmessage = function(e) {
|
||||
}
|
||||
|
||||
// Store voice options
|
||||
if (message.data.voice) voiceOptions.voice = message.data.voice;
|
||||
if (message.data.speed) voiceOptions.speed = message.data.speed;
|
||||
if (message.voice) voiceOptions.voice = message.voice;
|
||||
if (message.speed) voiceOptions.speed = message.speed;
|
||||
|
||||
// Generate speech
|
||||
generateSpeech(message.data.text)
|
||||
generateSpeech(message.text)
|
||||
.then(result => {
|
||||
self.postMessage({
|
||||
type: 'generated',
|
||||
result: result
|
||||
}, [result.audio.buffer]);
|
||||
})
|
||||
.catch(error => {
|
||||
self.postMessage({
|
||||
type: 'error',
|
||||
@@ -73,46 +79,43 @@ async function generateSpeech(text) {
|
||||
try {
|
||||
// Load Kokoro if not already loaded
|
||||
if (!kokoroLoaded) {
|
||||
// Load the Kokoro script
|
||||
self.importScripts('/js/kokoro-js.js');
|
||||
|
||||
if (!self.kokoro || !self.kokoro.KokoroTTS) {
|
||||
throw new Error('Kokoro failed to load correctly');
|
||||
try {
|
||||
// Load the Kokoro script
|
||||
self.importScripts('/js/kokoro.js');
|
||||
|
||||
if (!self.Kokoro) {
|
||||
throw new Error('Kokoro failed to load correctly');
|
||||
}
|
||||
|
||||
kokoroLoaded = true;
|
||||
console.log('Kokoro loaded in worker');
|
||||
} catch (loadError) {
|
||||
console.error('Error loading Kokoro in worker:', loadError);
|
||||
throw new Error(`Failed to load Kokoro: ${loadError.message}`);
|
||||
}
|
||||
|
||||
kokoroLoaded = true;
|
||||
}
|
||||
|
||||
// Create a new Kokoro instance for this generation
|
||||
// We can't easily transfer the instance from the main thread, so we create it here
|
||||
const kokoroTTS = self.kokoro.KokoroTTS;
|
||||
|
||||
// Create instance using from_pretrained
|
||||
const tts = await kokoroTTS.from_pretrained("onnx-community/Kokoro-82M-v1.0-ONNX", {
|
||||
dtype: "fp32",
|
||||
device: "wasm",
|
||||
cache: true // Use cache to speed up subsequent loads
|
||||
});
|
||||
|
||||
// Generate speech
|
||||
const result = await tts.generate(text, {
|
||||
// Generate speech using Kokoro
|
||||
const result = await self.Kokoro(text, {
|
||||
voice: voiceOptions.voice,
|
||||
speed: voiceOptions.speed
|
||||
speed: voiceOptions.speed,
|
||||
autoPlay: false
|
||||
});
|
||||
|
||||
// Send the result back to the main thread
|
||||
// We can't transfer the Float32Array directly, so let's transfer the buffer
|
||||
const audioBuffer = result.audio.buffer;
|
||||
// Extract audio data
|
||||
const audioContext = new (self.AudioContext || self.webkitAudioContext)();
|
||||
const audioBuffer = await audioContext.decodeAudioData(result.buffer);
|
||||
|
||||
self.postMessage({
|
||||
type: 'generated',
|
||||
result: {
|
||||
audio: audioBuffer,
|
||||
sampling_rate: result.sampling_rate
|
||||
}
|
||||
}, [audioBuffer]); // Transfer the buffer for better performance
|
||||
// Get audio data as Float32Array
|
||||
const audioData = audioBuffer.getChannelData(0);
|
||||
|
||||
// Return the result
|
||||
return {
|
||||
audio: audioData,
|
||||
sampling_rate: audioBuffer.sampleRate
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error generating speech in worker:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
isProcessing = false;
|
||||
|
||||
Reference in New Issue
Block a user