Fixed Ducking. Refined UI.
This commit is contained in:
@@ -18,6 +18,7 @@ class AudioManagerModule extends BaseModule {
|
||||
this.sfxVolume = 1.0;
|
||||
this.ttsVolume = 1.0;
|
||||
this.musicDuckingFactor = 1.0;
|
||||
this.musicFadeToken = 0;
|
||||
this.activeTtsPlaybackCount = 0;
|
||||
this.ttsQueueEmpty = true;
|
||||
this.pendingMusicPlayback = null;
|
||||
@@ -106,6 +107,11 @@ class AudioManagerModule extends BaseModule {
|
||||
this.duckMusicForSpeech();
|
||||
});
|
||||
|
||||
this.addEventListener(document, 'tts:audio-started', () => {
|
||||
this.ttsQueueEmpty = false;
|
||||
this.duckMusicForSpeech();
|
||||
});
|
||||
|
||||
this.addEventListener(document, 'tts:playback-end', () => {
|
||||
this.activeTtsPlaybackCount = Math.max(0, this.activeTtsPlaybackCount - 1);
|
||||
this.restoreMusicIfSpeechFinished();
|
||||
@@ -116,6 +122,11 @@ class AudioManagerModule extends BaseModule {
|
||||
this.restoreMusicIfSpeechFinished();
|
||||
});
|
||||
|
||||
this.addEventListener(document, 'tts:speechCompleted', () => {
|
||||
this.activeTtsPlaybackCount = Math.max(0, this.activeTtsPlaybackCount - 1);
|
||||
this.restoreMusicIfSpeechFinished();
|
||||
});
|
||||
|
||||
const unlock = () => this.unlockPendingAudio();
|
||||
document.addEventListener('pointerdown', unlock, { passive: true });
|
||||
document.addEventListener('keydown', unlock);
|
||||
@@ -252,6 +263,14 @@ class AudioManagerModule extends BaseModule {
|
||||
audio.pause();
|
||||
audio.currentTime = 0;
|
||||
});
|
||||
|
||||
this.stopCurrentMusic();
|
||||
this.queuedMusic = null;
|
||||
this.pendingMusicPlayback = null;
|
||||
this.activeTtsPlaybackCount = 0;
|
||||
this.ttsQueueEmpty = true;
|
||||
this.musicDuckingFactor = 1.0;
|
||||
this.musicFadeToken += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,7 +322,7 @@ class AudioManagerModule extends BaseModule {
|
||||
}
|
||||
|
||||
if (this.currentLoop) {
|
||||
this.currentLoop.volume = this.masterVolume * this.musicVolume;
|
||||
this.currentLoop.volume = this.getMusicVolume();
|
||||
}
|
||||
|
||||
if (this.currentMusic) {
|
||||
@@ -329,7 +348,7 @@ class AudioManagerModule extends BaseModule {
|
||||
|
||||
duckMusicForSpeech() {
|
||||
console.log('AudioManager: Ducking music for TTS playback');
|
||||
this.fadeMusicTo(0.7, 500);
|
||||
this.fadeMusicTo(0.3, 500);
|
||||
}
|
||||
|
||||
restoreMusicAfterSpeech() {
|
||||
@@ -350,11 +369,15 @@ class AudioManagerModule extends BaseModule {
|
||||
}
|
||||
|
||||
const audio = this.currentMusic;
|
||||
const token = ++this.musicFadeToken;
|
||||
const startVolume = audio.volume;
|
||||
const targetVolume = this.getUnduckedMusicVolume() * this.musicDuckingFactor;
|
||||
const start = performance.now();
|
||||
|
||||
const tick = () => {
|
||||
if (token !== this.musicFadeToken || this.currentMusic !== audio) {
|
||||
return;
|
||||
}
|
||||
const progress = Math.min(1, (performance.now() - start) / duration);
|
||||
audio.volume = startVolume + ((targetVolume - startVolume) * progress);
|
||||
if (progress < 1) {
|
||||
|
||||
Reference in New Issue
Block a user