-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 use @diffusionstudio/vits-web for better TTS
- Loading branch information
Showing
8 changed files
with
341 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,111 @@ | ||
import { useCallback, useState } from "react"; | ||
import { useCallback, useState, useEffect } from "react"; | ||
import Worker from "../workers/ttsWorker?worker"; | ||
import { VoiceId } from "@diffusionstudio/vits-web"; | ||
|
||
export const useSpeechSynthesis = () => { | ||
type WorkerMessage = | ||
| { type: "loadingProgress"; progress: number } | ||
| { type: "loadingComplete" } | ||
| { type: "availableVoices"; voices: VoiceId[] } | ||
| { type: "result"; audio: Blob } | ||
| { type: "error"; message: string }; | ||
|
||
export const useSpeechSynthesis = ({ | ||
enabled = true, | ||
selectedVoice, | ||
}: { | ||
enabled?: boolean; | ||
selectedVoice: VoiceId; | ||
}) => { | ||
const [isSpeaking, setIsSpeaking] = useState(false); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [loadingProgress, setLoadingProgress] = useState(0); | ||
const [worker, setWorker] = useState<Worker | null>(null); | ||
const [availableVoices, setAvailableVoices] = useState<VoiceId[]>([]); | ||
|
||
useEffect(() => { | ||
const ttsWorker = new Worker(); | ||
|
||
ttsWorker.onmessage = (event: MessageEvent<WorkerMessage>) => { | ||
switch (event.data.type) { | ||
case "loadingProgress": | ||
setLoadingProgress(event.data.progress); | ||
break; | ||
case "loadingComplete": | ||
setIsLoading(false); | ||
break; | ||
case "availableVoices": | ||
setAvailableVoices(event.data.voices); | ||
break; | ||
case "error": | ||
console.error("Error in TTS worker:", event.data.message); | ||
break; | ||
} | ||
}; | ||
setWorker(ttsWorker); | ||
|
||
const speak = useCallback((text: string): Promise<void> => { | ||
return new Promise((resolve) => { | ||
const utterance = new SpeechSynthesisUtterance(text); | ||
utterance.onstart = () => setIsSpeaking(true); | ||
utterance.onend = () => { | ||
setIsSpeaking(false); | ||
resolve(); | ||
}; | ||
window.speechSynthesis.speak(utterance); | ||
}); | ||
ttsWorker.postMessage({ type: "getVoices" }); | ||
|
||
return () => { | ||
ttsWorker.terminate(); | ||
setWorker(null); | ||
}; | ||
}, []); | ||
|
||
return { speak, isSpeaking }; | ||
useEffect(() => { | ||
if (worker && selectedVoice && enabled) { | ||
setIsLoading(true); | ||
worker.postMessage({ type: "init", voiceId: selectedVoice }); | ||
} | ||
|
||
return () => { | ||
if (worker) { | ||
worker.postMessage({ type: "terminate" }); | ||
} | ||
}; | ||
}, [worker, selectedVoice, enabled]); | ||
|
||
const speak = useCallback( | ||
async (text: string): Promise<void> => { | ||
if (!worker) return; | ||
|
||
const emojiRegex = /(\p{Emoji_Presentation}|\p{Emoji}\uFE0F)/gu; | ||
const cleanedText = text.replace(emojiRegex, ""); | ||
|
||
return new Promise((resolve, reject) => { | ||
let audio: HTMLAudioElement | null = null; | ||
|
||
const messageHandler = (event: MessageEvent<WorkerMessage>) => { | ||
switch (event.data.type) { | ||
case "result": | ||
audio = new Audio(URL.createObjectURL(event.data.audio)); | ||
audio.onended = () => { | ||
setIsSpeaking(false); | ||
cleanup(); | ||
resolve(); | ||
}; | ||
setIsSpeaking(true); | ||
void audio.play(); | ||
break; | ||
case "error": | ||
cleanup(); | ||
reject(new Error(event.data.message)); | ||
break; | ||
} | ||
}; | ||
|
||
const cleanup = () => { | ||
worker.removeEventListener("message", messageHandler); | ||
if (audio) { | ||
audio.onended = null; | ||
} | ||
}; | ||
|
||
worker.addEventListener("message", messageHandler); | ||
worker.postMessage({ type: "speak", text: cleanedText }); | ||
}); | ||
}, | ||
[worker], | ||
); | ||
|
||
return { speak, isSpeaking, isLoading, loadingProgress, availableVoices, selectedVoice }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useMemo } from "react"; | ||
import { CountryCode, VoiceId } from "@diffusionstudio/vits-web"; | ||
|
||
type FormattedVoice = { | ||
id: VoiceId; | ||
flag: string; | ||
name: string; | ||
quality: { | ||
text: string; | ||
color: string; | ||
}; | ||
}; | ||
|
||
const getFlag = (locale: string): string => { | ||
const flagEmojis: Record<CountryCode, string> = { | ||
ar_JO: "🇯🇴", | ||
ca_ES: "🇪🇸", | ||
cs_CZ: "🇨🇿", | ||
da_DK: "🇩🇰", | ||
de_DE: "🇩🇪", | ||
el_GR: "🇬🇷", | ||
en_GB: "🇬🇧", | ||
en_US: "🇺🇸", | ||
es_ES: "🇪🇸", | ||
es_MX: "🇲🇽", | ||
fa_IR: "🇮🇷", | ||
fi_FI: "🇫🇮", | ||
fr_FR: "🇫🇷", | ||
hu_HU: "🇭🇺", | ||
is_IS: "🇮🇸", | ||
it_IT: "🇮🇹", | ||
ka_GE: "🇬🇪", | ||
kk_KZ: "🇰🇿", | ||
lb_LU: "🇱🇺", | ||
ne_NP: "🇳🇵", | ||
nl_BE: "🇧🇪", | ||
nl_NL: "🇳🇱", | ||
no_NO: "🇳🇴", | ||
pl_PL: "🇵🇱", | ||
pt_BR: "🇧🇷", | ||
pt_PT: "🇵🇹", | ||
ro_RO: "🇷🇴", | ||
ru_RU: "🇷🇺", | ||
sk_SK: "🇸🇰", | ||
sl_SI: "🇸🇮", | ||
sr_RS: "🇷🇸", | ||
sv_SE: "🇸🇪", | ||
sw_CD: "🇨🇩", | ||
tr_TR: "🇹🇷", | ||
uk_UA: "🇺🇦", | ||
vi_VN: "🇻🇳", | ||
zh_CN: "🇨🇳", | ||
}; | ||
return flagEmojis[locale as CountryCode] || "🏳️"; | ||
}; | ||
|
||
const getQualityColor = (quality: string): string => { | ||
switch (quality.toLowerCase()) { | ||
case "x_low": | ||
return "text-red-800"; | ||
case "low": | ||
return "text-red-500"; | ||
case "medium": | ||
return "text-orange-500"; | ||
case "high": | ||
return "text-green-500"; | ||
default: | ||
return "text-gray-500"; | ||
} | ||
}; | ||
|
||
export const useVoiceFormatting = (voices: VoiceId[]): FormattedVoice[] => { | ||
const formattedVoices = useMemo(() => { | ||
return voices | ||
.filter((voice) => voice.startsWith("en_")) | ||
.map((voice) => { | ||
const [locale, name, quality] = voice.split("-"); | ||
return { | ||
id: voice, | ||
flag: getFlag(locale || "en_US"), | ||
name: (name?.charAt(0)?.toUpperCase() ?? "") + (name?.slice(1) ?? "") || "Unknown", | ||
quality: { | ||
text: (quality?.charAt(0)?.toUpperCase() ?? "") + (quality?.slice(1) ?? "") || "Unknown", | ||
color: getQualityColor(quality || "Unknown"), | ||
}, | ||
}; | ||
}); | ||
}, [voices]); | ||
|
||
return formattedVoices; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.