mirror of
https://github.com/open-webui/computer.git
synced 2026-11-27 07:39:21 -05:00
refac
This commit is contained in:
1 parent
6576c0e55c
commit
20b3af5483
3 files changed
+67
-1
No files matched your search
@@ -27,6 +27,7 @@
|
||||
sttConfigured,
|
||||
ttsConfigured,
|
||||
ttsEnabled,
|
||||
unlockTtsAudioPlayback,
|
||||
voiceModeEnabled,
|
||||
voiceModeSttMode
|
||||
} from '$lib/stores/audio';
|
||||
@@ -808,6 +809,7 @@
|
||||
const next = !$voiceModeEnabled;
|
||||
voiceModeEnabled.set(next);
|
||||
if (next) {
|
||||
void unlockTtsAudioPlayback();
|
||||
voiceWaitingForResponse = false;
|
||||
voiceSawStreaming = false;
|
||||
startVoiceRecognition();
|
||||
|
||||
@@ -37,8 +37,10 @@
|
||||
ttsEnabled,
|
||||
ttsConfigured,
|
||||
ttsFormat,
|
||||
getTtsAudioElement,
|
||||
ttsPlaybackEnabled,
|
||||
ttsVoice,
|
||||
unlockTtsAudioPlayback,
|
||||
voiceModeEnabled
|
||||
} from '$lib/stores/audio';
|
||||
|
||||
@@ -1078,6 +1080,7 @@
|
||||
return;
|
||||
}
|
||||
stopTtsPlayback();
|
||||
void unlockTtsAudioPlayback();
|
||||
speakingMessageId = messageId;
|
||||
ttsPlaybackEnabled.set(true);
|
||||
ttsErrorShown = false;
|
||||
@@ -1177,7 +1180,8 @@
|
||||
if (generation !== ttsGeneration) break;
|
||||
if (ttsObjectUrl) URL.revokeObjectURL(ttsObjectUrl);
|
||||
ttsObjectUrl = URL.createObjectURL(blob);
|
||||
ttsAudio = new Audio(ttsObjectUrl);
|
||||
ttsAudio = getTtsAudioElement() ?? new Audio();
|
||||
ttsAudio.src = ttsObjectUrl;
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const audio = ttsAudio!;
|
||||
audio.onended = () => resolve();
|
||||
|
||||
@@ -27,10 +27,70 @@ export const ttsPlaybackEnabled = writable<boolean>(
|
||||
typeof localStorage !== 'undefined' ? localStorage.getItem('ttsPlaybackEnabled') === 'true' : false
|
||||
);
|
||||
|
||||
const SILENT_WAV =
|
||||
'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEAESsAACJWAAACABAAZGF0YQQAAAAAAA==';
|
||||
|
||||
let ttsAudioElement: HTMLAudioElement | null = null;
|
||||
let ttsAudioContext: AudioContext | null = null;
|
||||
let ttsAudioUnlocked = false;
|
||||
|
||||
ttsPlaybackEnabled.subscribe((v) => {
|
||||
if (typeof localStorage !== 'undefined') localStorage.setItem('ttsPlaybackEnabled', String(v));
|
||||
});
|
||||
|
||||
export function getTtsAudioElement(): HTMLAudioElement | null {
|
||||
if (typeof Audio === 'undefined') return null;
|
||||
if (!ttsAudioElement) {
|
||||
ttsAudioElement = new Audio();
|
||||
ttsAudioElement.preload = 'auto';
|
||||
(ttsAudioElement as HTMLAudioElement & { playsInline?: boolean }).playsInline = true;
|
||||
}
|
||||
return ttsAudioElement;
|
||||
}
|
||||
|
||||
export async function unlockTtsAudioPlayback() {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
const unlocks: Promise<unknown>[] = [];
|
||||
const AudioContextCtor =
|
||||
window.AudioContext || (window as Window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;
|
||||
try {
|
||||
if (AudioContextCtor) {
|
||||
ttsAudioContext ??= new AudioContextCtor();
|
||||
const buffer = ttsAudioContext.createBuffer(1, 1, 22050);
|
||||
const source = ttsAudioContext.createBufferSource();
|
||||
source.buffer = buffer;
|
||||
source.connect(ttsAudioContext.destination);
|
||||
source.start(0);
|
||||
if (ttsAudioContext.state !== 'running') unlocks.push(ttsAudioContext.resume());
|
||||
}
|
||||
} catch {}
|
||||
|
||||
const audio = getTtsAudioElement();
|
||||
if (audio && !ttsAudioUnlocked) {
|
||||
try {
|
||||
audio.src = SILENT_WAV;
|
||||
audio.volume = 0;
|
||||
const started = audio.play();
|
||||
unlocks.push(
|
||||
Promise.resolve(started)
|
||||
.then(() => {
|
||||
ttsAudioUnlocked = true;
|
||||
})
|
||||
.finally(() => {
|
||||
audio.pause();
|
||||
audio.currentTime = 0;
|
||||
audio.removeAttribute('src');
|
||||
audio.load();
|
||||
audio.volume = 1;
|
||||
})
|
||||
);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
await Promise.allSettled(unlocks);
|
||||
}
|
||||
|
||||
export async function refreshAudioState() {
|
||||
try {
|
||||
const data = await fetchJSON<{
|
||||
|
||||
Reference in new issue
Block a user