forked from vueuse/vueuse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
162 lines (136 loc) · 3.84 KB
/
index.ts
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
152
153
154
155
156
157
158
159
160
161
162
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveRef, tryOnScopeDispose } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { computed, ref, shallowRef, unref, watch } from 'vue-demi'
import { useSupported } from '../useSupported'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
export type Status = 'init' | 'play' | 'pause' | 'end'
export type VoiceInfo = Pick<SpeechSynthesisVoice, 'lang' | 'name'>
export interface UseSpeechSynthesisOptions extends ConfigurableWindow {
/**
* Language for SpeechSynthesis
*
* @default 'en-US'
*/
lang?: MaybeComputedRef<string>
/**
* Gets and sets the pitch at which the utterance will be spoken at.
*
* @default 1
*/
pitch?: SpeechSynthesisUtterance['pitch']
/**
* Gets and sets the speed at which the utterance will be spoken at.
*
* @default 1
*/
rate?: SpeechSynthesisUtterance['rate']
/**
* Gets and sets the voice that will be used to speak the utterance.
*/
voice?: SpeechSynthesisVoice
/**
* Gets and sets the volume that the utterance will be spoken at.
*
* @default 1
*/
volume?: SpeechSynthesisUtterance['volume']
}
/**
* Reactive SpeechSynthesis.
*
* @see https://vueuse.org/useSpeechSynthesis
* @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis SpeechSynthesis
* @param options
*/
export function useSpeechSynthesis(text: MaybeComputedRef<string>, options: UseSpeechSynthesisOptions = {}) {
const {
pitch = 1,
rate = 1,
volume = 1,
window = defaultWindow,
} = options
const synth = window && (window as any).speechSynthesis as SpeechSynthesis
const isSupported = useSupported(() => synth)
const isPlaying = ref(false)
const status = ref<Status>('init')
const voiceInfo = {
lang: options.voice?.lang || 'default',
name: options.voice?.name || '',
}
const spokenText = resolveRef(text || '')
const lang = resolveRef(options.lang || 'en-US')
const error = shallowRef(undefined) as Ref<SpeechSynthesisErrorEvent | undefined>
const toggle = (value = !isPlaying.value) => {
isPlaying.value = value
}
const bindEventsForUtterance = (utterance: SpeechSynthesisUtterance) => {
utterance.lang = unref(lang)
options.voice && (utterance.voice = options.voice)
utterance.pitch = pitch
utterance.rate = rate
utterance.volume = volume
utterance.onstart = () => {
isPlaying.value = true
status.value = 'play'
}
utterance.onpause = () => {
isPlaying.value = false
status.value = 'pause'
}
utterance.onresume = () => {
isPlaying.value = true
status.value = 'play'
}
utterance.onend = () => {
isPlaying.value = false
status.value = 'end'
}
utterance.onerror = (event) => {
error.value = event
}
utterance.onend = () => {
isPlaying.value = false
utterance.lang = unref(lang)
}
}
const utterance = computed(() => {
isPlaying.value = false
status.value = 'init'
const newUtterance = new SpeechSynthesisUtterance(spokenText.value)
bindEventsForUtterance(newUtterance)
return newUtterance
})
const speak = () => {
synth!.cancel()
utterance && synth!.speak(utterance.value)
}
if (isSupported.value) {
bindEventsForUtterance(utterance.value)
watch(lang, (lang) => {
if (utterance.value && !isPlaying.value)
utterance.value.lang = lang
})
watch(isPlaying, () => {
if (isPlaying.value)
synth!.resume()
else
synth!.pause()
})
}
tryOnScopeDispose(() => {
isPlaying.value = false
})
return {
isSupported,
isPlaying,
status,
voiceInfo,
utterance,
error,
toggle,
speak,
}
}
export type UseSpeechSynthesisReturn = ReturnType<typeof useSpeechSynthesis>