Added support for openai api tts.
This commit is contained in:
+47
-39
@@ -169,16 +169,13 @@ class TTSPlayerModule extends BaseModule {
|
||||
if (!this.isSpeaking() || (this.currentSpeech && this.currentSpeech !== text)) {
|
||||
console.log(`TTS Player: Preloading speech for: "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`);
|
||||
|
||||
// Use the preload method of the TTS factory if available
|
||||
if (typeof ttsFactory.preloadSpeech === 'function') {
|
||||
await ttsFactory.preloadSpeech(text);
|
||||
this.preloadedAudio.set(text, true);
|
||||
// Use the preload method of the TTS factory
|
||||
const preloadData = await ttsFactory.preloadSpeech(text);
|
||||
if (preloadData) {
|
||||
this.preloadedAudio.set(text, preloadData);
|
||||
console.log(`TTS Player: Successfully preloaded speech for: "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`);
|
||||
} else {
|
||||
// Fallback: use normal speak method with a dummy callback
|
||||
ttsFactory.speak(text, () => {
|
||||
ttsFactory.stop(); // Stop immediately after generation
|
||||
this.preloadedAudio.set(text, true);
|
||||
});
|
||||
console.warn(`TTS Player: Failed to preload speech for: "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -195,21 +192,14 @@ class TTSPlayerModule extends BaseModule {
|
||||
}
|
||||
|
||||
/**
|
||||
* Speak text
|
||||
* Speak a sentence
|
||||
* @param {string} text - Text to speak
|
||||
* @param {Function} callback - Optional callback for when speech completes
|
||||
* @returns {boolean} - True if speech started successfully
|
||||
* @param {Function} callback - Callback for when speech completes
|
||||
* @returns {boolean} - Success status
|
||||
*/
|
||||
speak(text, callback = null) {
|
||||
if (!text) return false;
|
||||
|
||||
console.log(`TTS Player: Speaking "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`, this.enabled ? "(TTS enabled)" : "(TTS disabled)");
|
||||
|
||||
// Store the current speech text
|
||||
this.currentSpeech = text;
|
||||
|
||||
// Check if TTS is enabled
|
||||
if (!this.enabled) {
|
||||
console.log("TTS Player: TTS is disabled, not speaking");
|
||||
if (callback) {
|
||||
setTimeout(() => callback({ success: false, reason: 'tts_disabled' }), 0);
|
||||
}
|
||||
@@ -220,31 +210,49 @@ class TTSPlayerModule extends BaseModule {
|
||||
const ttsFactory = this.getModule('tts-factory');
|
||||
if (ttsFactory) {
|
||||
this.pendingCallback = callback;
|
||||
this.currentSpeech = text;
|
||||
|
||||
// Check if this text was preloaded
|
||||
const wasPreloaded = this.preloadedAudio.has(text);
|
||||
if (wasPreloaded) {
|
||||
const preloadedData = this.preloadedAudio.get(text);
|
||||
if (preloadedData) {
|
||||
console.log("TTS Player: Using preloaded speech");
|
||||
this.preloadedAudio.delete(text); // Remove from cache after use
|
||||
|
||||
// Use the preloaded speech data
|
||||
ttsFactory.speakPreloaded(preloadedData, (result) => {
|
||||
// Store the completed result
|
||||
this.currentSpeech = null;
|
||||
|
||||
// Call the callback if provided
|
||||
if (this.pendingCallback) {
|
||||
this.pendingCallback(result);
|
||||
this.pendingCallback = null;
|
||||
}
|
||||
|
||||
// Process next in preload queue if any
|
||||
if (this.preloadQueue.length > 0 && !this.isPreloading) {
|
||||
this.processPreloadQueue();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Start TTS with regular speech if not preloaded
|
||||
ttsFactory.speak(text, (result) => {
|
||||
// Store the completed result
|
||||
this.currentSpeech = null;
|
||||
|
||||
// Call the callback if provided
|
||||
if (this.pendingCallback) {
|
||||
this.pendingCallback(result);
|
||||
this.pendingCallback = null;
|
||||
}
|
||||
|
||||
// Process next in preload queue if any
|
||||
if (this.preloadQueue.length > 0 && !this.isPreloading) {
|
||||
this.processPreloadQueue();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Start TTS with minimal delay to synchronize with text rendering
|
||||
ttsFactory.speak(text, (result) => {
|
||||
// Store the completed result
|
||||
this.currentSpeech = null;
|
||||
|
||||
// Call the callback if provided
|
||||
if (this.pendingCallback) {
|
||||
this.pendingCallback(result);
|
||||
this.pendingCallback = null;
|
||||
}
|
||||
|
||||
// Process next in preload queue if any
|
||||
if (this.preloadQueue.length > 0 && !this.isPreloading) {
|
||||
this.processPreloadQueue();
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
} else {
|
||||
console.error("TTS Player: TTSFactory module not found in registry");
|
||||
|
||||
Reference in New Issue
Block a user