File size: 3,781 Bytes
f56a29b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | /**
* Browser Native TTS (Text-to-Speech) Hook
* Uses Web Speech API for client-side text-to-speech
* Completely free, no API key required
*/
import { useState, useCallback, useRef, useEffect } from 'react';
// Note: Window.SpeechSynthesis declaration is already in the global scope
export interface UseBrowserTTSOptions {
onStart?: () => void;
onEnd?: () => void;
onError?: (error: string) => void;
rate?: number; // 0.1 to 10
pitch?: number; // 0 to 2
volume?: number; // 0 to 1
lang?: string; // e.g., 'zh-CN', 'en-US'
}
export function useBrowserTTS(options: UseBrowserTTSOptions = {}) {
const {
onStart,
onEnd,
onError,
rate = 1.0,
pitch = 1.0,
volume = 1.0,
lang = 'zh-CN',
} = options;
const [isSpeaking, setIsSpeaking] = useState(false);
const [isPaused, setIsPaused] = useState(false);
const [availableVoices, setAvailableVoices] = useState<SpeechSynthesisVoice[]>([]);
const utteranceRef = useRef<SpeechSynthesisUtterance | null>(null);
// Load available voices
useEffect(() => {
if (typeof window === 'undefined' || !window.speechSynthesis) {
return;
}
const loadVoices = () => {
const voices = window.speechSynthesis.getVoices();
setAvailableVoices(voices);
};
loadVoices();
// Some browsers load voices asynchronously
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = loadVoices;
}
return () => {
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = null;
}
};
}, []);
const speak = useCallback(
(text: string, voiceURI?: string) => {
if (typeof window === 'undefined' || !window.speechSynthesis) {
onError?.('浏览器不支持 Web Speech API');
return;
}
// Cancel any ongoing speech
window.speechSynthesis.cancel();
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = rate;
utterance.pitch = pitch;
utterance.volume = volume;
utterance.lang = lang;
// Set voice if specified
if (voiceURI) {
const voice = availableVoices.find((v) => v.voiceURI === voiceURI);
if (voice) {
utterance.voice = voice;
}
}
utterance.onstart = () => {
setIsSpeaking(true);
setIsPaused(false);
onStart?.();
};
utterance.onend = () => {
setIsSpeaking(false);
setIsPaused(false);
utteranceRef.current = null;
onEnd?.();
};
utterance.onerror = (event) => {
setIsSpeaking(false);
setIsPaused(false);
utteranceRef.current = null;
onError?.(event.error);
};
utterance.onpause = () => {
setIsPaused(true);
};
utterance.onresume = () => {
setIsPaused(false);
};
utteranceRef.current = utterance;
window.speechSynthesis.speak(utterance);
},
[rate, pitch, volume, lang, availableVoices, onStart, onEnd, onError],
);
const pause = useCallback(() => {
if (typeof window !== 'undefined' && window.speechSynthesis) {
window.speechSynthesis.pause();
}
}, []);
const resume = useCallback(() => {
if (typeof window !== 'undefined' && window.speechSynthesis) {
window.speechSynthesis.resume();
}
}, []);
const cancel = useCallback(() => {
if (typeof window !== 'undefined' && window.speechSynthesis) {
window.speechSynthesis.cancel();
setIsSpeaking(false);
setIsPaused(false);
utteranceRef.current = null;
}
}, []);
return {
speak,
pause,
resume,
cancel,
isSpeaking,
isPaused,
availableVoices,
};
}
|