feat: text to speech and animation for zap alerts

This commit is contained in:
verbiricha
2023-09-04 11:38:56 +02:00
parent 15be10aa02
commit 29b208b136
8 changed files with 370 additions and 65 deletions

53
src/text2speech.ts Normal file
View File

@ -0,0 +1,53 @@
import { useMemo } from "react";
import { useLocation } from "react-router-dom";
function useQuery() {
const { search } = useLocation();
return useMemo(() => new URLSearchParams(search), [search]);
}
interface TextToSpeechConfig {
voiceURI: string | null;
minSats: number;
}
export function useTextToSpeechParams(): TextToSpeechConfig {
const q = useQuery();
const voiceURI = q.get("voiceURI");
const minSats = Number(q.get("minSats")) ?? 21;
return { voiceURI, minSats };
}
interface TextToSpeechConfigParams {
voiceURI: string | null;
minSats: number | null;
}
export function toTextToSpeechParams(config: TextToSpeechConfigParams): URLSearchParams {
const params = new URLSearchParams();
if (config.voiceURI) {
params.set("voiceURI", config.voiceURI);
}
if (config.minSats) {
params.set("minSats", String(config.minSats));
}
return params;
}
export function getVoices() {
if ("speechSynthesis" in window) {
return speechSynthesis.getVoices();
}
return [];
}
export function speak(voice: SpeechSynthesisVoice, text: string) {
try {
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = voice;
utterance.rate = 0.8;
speechSynthesis.speak(utterance);
} catch (e) {
console.error(e);
}
}