Reference: Semi broken tts before refactoring
This commit is contained in:
@@ -14,6 +14,9 @@ class AudioManagerModule extends BaseModule {
|
||||
this.masterVolume = 1.0;
|
||||
this.musicVolume = 1.0;
|
||||
this.sfxVolume = 1.0;
|
||||
|
||||
// Add persistence-manager as a dependency
|
||||
this.dependencies = ['persistence-manager'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,6 +278,97 @@ class AudioManagerModule extends BaseModule {
|
||||
}, 50);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Play speech audio from a Blob
|
||||
* @param {Blob} audioBlob - The audio Blob to play
|
||||
* @param {Object} options - Playback options
|
||||
* @returns {Promise<boolean>} - Resolves with success status
|
||||
*/
|
||||
async playSpeech(audioBlob, options = {}) {
|
||||
if (!audioBlob || !(audioBlob instanceof Blob)) {
|
||||
console.error('AudioManager: Invalid speech audio blob provided');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// Create object URL from Blob
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
|
||||
// Stop any current non-looping audio
|
||||
if (this.currentAudio) {
|
||||
this.currentAudio.pause();
|
||||
this.currentAudio.currentTime = 0;
|
||||
}
|
||||
|
||||
// Create new audio element
|
||||
const audio = new Audio(audioUrl);
|
||||
|
||||
// Get speech volume from options or preferences
|
||||
const persistenceManager = this.getModule('persistence-manager');
|
||||
let speechVolume = 1.0;
|
||||
|
||||
if (persistenceManager) {
|
||||
// Get speech volume from preferences (0.0 to 1.0)
|
||||
speechVolume = persistenceManager.getPreference('tts', 'volume') || 1.0;
|
||||
}
|
||||
|
||||
// Override with options if provided
|
||||
if (options.volume !== undefined) {
|
||||
speechVolume = options.volume;
|
||||
}
|
||||
|
||||
// Apply master volume and speech volume
|
||||
audio.volume = this.masterVolume * speechVolume;
|
||||
|
||||
// Set up cleanup
|
||||
audio.onended = () => {
|
||||
URL.revokeObjectURL(audioUrl);
|
||||
if (this.currentAudio === audio) {
|
||||
this.currentAudio = null;
|
||||
}
|
||||
if (options.onComplete && typeof options.onComplete === 'function') {
|
||||
options.onComplete();
|
||||
}
|
||||
|
||||
// Dispatch event for speech completion
|
||||
window.dispatchEvent(new CustomEvent('tts:speak-completed'));
|
||||
};
|
||||
|
||||
// Handle errors
|
||||
audio.onerror = (error) => {
|
||||
console.error('AudioManager: Error playing speech audio:', error);
|
||||
URL.revokeObjectURL(audioUrl);
|
||||
if (this.currentAudio === audio) {
|
||||
this.currentAudio = null;
|
||||
}
|
||||
if (options.onError && typeof options.onError === 'function') {
|
||||
options.onError(error);
|
||||
}
|
||||
|
||||
// Dispatch event for speech error
|
||||
window.dispatchEvent(new CustomEvent('tts:speak-error', {
|
||||
detail: { error: error }
|
||||
}));
|
||||
};
|
||||
|
||||
// Store as current audio
|
||||
this.currentAudio = audio;
|
||||
|
||||
// Play the audio
|
||||
await audio.play();
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('AudioManager: Error setting up speech audio:', error);
|
||||
|
||||
// Dispatch event for speech error
|
||||
window.dispatchEvent(new CustomEvent('tts:speak-error', {
|
||||
detail: { error: error }
|
||||
}));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the singleton instance
|
||||
|
||||
Reference in New Issue
Block a user