|
| 1 | +# useSpeechSynthesis |
| 2 | +Hook to use _SpeechSynthesis API_. Refer to [Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis). |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +```tsx |
| 7 | +export const UseSpeechSynthesis = () => { |
| 8 | + const { state, speak, pause, resume, cancel } = useSpeechSynthesis({ |
| 9 | + onError(ev) { |
| 10 | + console.log(ev) |
| 11 | + }, |
| 12 | + }); |
| 13 | + const [form, setForm] = useState<{ text: string, voice: string, lang: string, rate: string, pitch: string }>({ text: "", voice: "", lang: "", rate: "1", pitch: "1" }); |
| 14 | + |
| 15 | + if (!state.isSupported) { |
| 16 | + return <div style={{ display: "flex", justifyContent: "center" }}> |
| 17 | + <p>Speech Synthesis not supported</p> |
| 18 | + </div> |
| 19 | + } |
| 20 | + |
| 21 | + return <div style={{ display: "flex", flexDirection: "column", gap: 10, justifyContent: "center", width: 'fit-content' }}> |
| 22 | + <div> |
| 23 | + <label htmlFor="text">Text to speak</label> |
| 24 | + <input type="text" value={form.text} onChange={e => setForm(f => ({...f, text: e.target.value}))} id="text" name="text"/> |
| 25 | + </div> |
| 26 | + <div> |
| 27 | + <label htmlFor="text">Voices</label> |
| 28 | + <select value={form.voice} onChange={e => setForm(f => ({ ...f, voice: e.target.value }))} disabled={state.status === "speaking"}> |
| 29 | + <> |
| 30 | + { |
| 31 | + (state.voices || []).map(el => ( |
| 32 | + <option key={el.name} value={el.name}>{ `${el.name} - ${el.lang}${el.default ? ' - DEFAULT' : ''}` }</option> |
| 33 | + )) |
| 34 | + } |
| 35 | + </> |
| 36 | + </select> |
| 37 | + </div> |
| 38 | + <div> |
| 39 | + <label htmlFor="text">Voices</label> |
| 40 | + <select value={form.lang} onChange={e => setForm(f => ({ ...f, lang: e.target.value }))} disabled={state.status === "speaking"}> |
| 41 | + <> |
| 42 | + { |
| 43 | + ["it-IT", "en-US", "en-GB", "de-DE", "es-ES", "fr-FR"].map(el => ( |
| 44 | + <option key={el} value={el}>{`${el}`}</option> |
| 45 | + )) |
| 46 | + } |
| 47 | + </> |
| 48 | + </select> |
| 49 | + </div> |
| 50 | + <div> |
| 51 | + <label htmlFor="range">Rate</label> |
| 52 | + <input type="range" id="range" name="range" min="0.1" max="10" step="0.1" value={form.rate} onChange={e => setForm(f => ({...f, rate: e.target.value}))} disabled={state.status === "speaking"}/> |
| 53 | + </div> |
| 54 | + <div> |
| 55 | + <label htmlFor="pitch">Pitch</label> |
| 56 | + <input type="range" id="pitch" name="pitch" value={form.pitch} min="0" max="2" step="0.1" onChange={e => setForm(f => ({ ...f, pitch: e.target.value }))} disabled={state.status === "speaking"}/> |
| 57 | + </div> |
| 58 | + <div style={{ display: "flex", gap: 10 }}> |
| 59 | + <button |
| 60 | + type="button" |
| 61 | + onClick={() => { |
| 62 | + speak({ |
| 63 | + text: form.text, |
| 64 | + lang: form.lang as LanguageBCP47Tags, |
| 65 | + voice: (state.voices || []).filter(v => v.name === form.voice)[0], |
| 66 | + rate: Number(form.rate), |
| 67 | + pitch: Number(form.pitch), |
| 68 | + }) |
| 69 | + }} |
| 70 | + > |
| 71 | + Speak |
| 72 | + </button> |
| 73 | + <button |
| 74 | + type="button" |
| 75 | + onClick={() => { |
| 76 | + state.status === "paused" ? resume() : pause(); |
| 77 | + }} |
| 78 | + disabled={!["speaking", "paused"].includes(state.status)} |
| 79 | + > |
| 80 | + {state.status === "paused" ? "Resume" : "Pause"} |
| 81 | + </button> |
| 82 | + <button |
| 83 | + type="button" |
| 84 | + onClick={cancel} |
| 85 | + disabled={state.status === "end"} |
| 86 | + > |
| 87 | + Cancel |
| 88 | + </button> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +> The component use _useSpeechSynthesis_ hook to read a text from an input text. Other fields are renders to setting properties as _voice_ _rate_ and _pitch_ of SpeechRecognition. |
| 95 | +
|
| 96 | + |
| 97 | +## API |
| 98 | + |
| 99 | +```tsx |
| 100 | +useSpeechSynthesis(opts?: UseSpeechSynthesisProps): ReturnType<UseSpeechSynthesis> |
| 101 | +``` |
| 102 | + |
| 103 | +> ### Params |
| 104 | +> |
| 105 | +> - __opts?__: _UseSpeechSynthesisProps_ |
| 106 | +options. |
| 107 | +> - __opts.onSpeak?__: _() => void_ |
| 108 | +function that will be executed when _speak_ event is fired. |
| 109 | +> - __opts.onStart?__: _SpeechSynthesisUtterance["onstart"]_ |
| 110 | +function that will be executed when _start_ event is fired. |
| 111 | +> - __opts.onPause?__: _SpeechSynthesisUtterance["onpause"]_ |
| 112 | +function that will be executed when _pause_ event is fired. |
| 113 | +> - __opts.onResume?__: _SpeechSynthesisUtterance["onresume"]_ |
| 114 | +function that will be executed when _resume_ event is fired. |
| 115 | +> - __opts.onBoundary?__: _SpeechSynthesisUtterance["onboundary"]_ |
| 116 | +function that will be executed when _boundary_ event is fired. |
| 117 | +> - __opts.onMark?__: _SpeechSynthesisUtterance["onmark"]_ |
| 118 | +function that will be executed when _mark_ event is fired. |
| 119 | +> - __opts.onError?__: _SpeechSynthesisUtterance["onerror"]_ |
| 120 | +function that will be executed when _error_ event is fired. |
| 121 | +> - __opts.onEnd?__: _SpeechSynthesisUtterance["onend"]_ |
| 122 | +function that will be executed when _end_ event is fired. |
| 123 | +> - __opts.onCancel?__: _SpeechSynthesisonCancel_ |
| 124 | +function that will be executed when _cancel_ event is fired. |
| 125 | +> - __opts.lang?__: _LanguageBCP47Tags_ |
| 126 | +[MDN Reference](https://developer.mozilla.org/docs/Web/API/SpeechSynthesisUtterance/lang. |
| 127 | +> - __opts.pitch?__: _SpeechSynthesisUtterance["pitch"]_ |
| 128 | +[MDN Reference](https://developer.mozilla.org/docs/Web/API/SpeechSynthesisUtterance/pitch). |
| 129 | +> - __opts.rate?__: _SpeechSynthesisUtterance["rate"]_ |
| 130 | +[MDN Reference](https://developer.mozilla.org/docs/Web/API/SpeechSynthesisUtterance/rate). |
| 131 | +> - __opts.voice?__: _SpeechSynthesisUtterance["voice"]_ |
| 132 | +[MDN Reference](https://developer.mozilla.org/docs/Web/API/SpeechSynthesisUtterance/voice). |
| 133 | +> - __opts.volume?__: _SpeechSynthesisUtterance["volume"]_ |
| 134 | +[MDN Reference](https://developer.mozilla.org/docs/Web/API/SpeechSynthesisUtterance/volume). |
| 135 | +> |
| 136 | +
|
| 137 | +> ### Returns |
| 138 | +> |
| 139 | +> __return__: _ReturnType<UseSpeechSynthesis>_ |
| 140 | +> - __state__: object with these properties: |
| 141 | +> - _isSupported_: Returns a boolean value indicating SpeechSynthesis availability. |
| 142 | +> - _status_: Returns the current status of SpeechSynthesis. |
| 143 | +> - _voices_: Returns the list of available voices. |
| 144 | +> - __speak__: Function to start speaking. |
| 145 | +> - __pause__: Function to keep in pause speaking. |
| 146 | +> - __resume__: Function to resume speaking. |
| 147 | +> - __cancel__: Function to cancel speaking. |
| 148 | +> |
0 commit comments