-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
694 lines (620 loc) · 19.6 KB
/
index.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/**
* @module EasySpeech
* @typicalname EasySpeech
*/
/**
* Cross browser Speech Synthesis with easy API.
* This project was created, because it's always a struggle to get the synthesis
* part of `Web Speech API` running on most major browsers.
*
* Setup is very straight forward (see example).
*
* @example
* import EasySpeech from 'easy-speech'
*
* const example = async () => {
* await EasySpeech.init() // required
* await EasySpeech.speak({ 'Hello, world' })
* }
*
* @see https://wicg.github.io/speech-api/#tts-section
* @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis
* @type {Object}
*/
const EasySpeech = {}
/**
* To support multiple environments (browser, node) we define scope, based
* on what's available with window as priority, since Browsers are main target.
* @private
*/
const scope = globalThis
/**
* @private
* @type {{
* status: String,
initialized: Boolean,
speechSynthesis: null|SpeechSynthesis,
speechSynthesisUtterance: null|SpeechSynthesisUtterance,
speechSynthesisVoice: null|SpeechSynthesisVoice,
speechSynthesisEvent: null|SpeechSynthesisEvent,
speechSynthesisErrorEvent: null|SpeechSynthesisErrorEvent,
voices: null|Array<SpeechSynthesisVoice>,
defaults: {
pitch: Number,
rate: Number,
volume: Number,
voice: null|SpeechSynthesisVoice
},
handlers: {}
* }}
*/
const internal = {
status: 'created'
}
/*******************************************************************************
*
* AVAILABLE WITHOUT INIT
*
******************************************************************************/
/**
* Enable module-internal debugging by passing your own callback function.
* Debug will automatically pass through all updates to `status`
*
* @example
* import EasySpeech from 'easy-speech'
* import Log from '/path/to/my/Log'
*
* EasySpeech.debug(arg => Log.debug('EasySpeech:', arg))
*
* @param {Function} fn A function, which always receives one argument, that
* represents a current debug message
*/
EasySpeech.debug = fn => {
debug = typeof fn === 'function' ? fn : () => {}
}
let debug = () => {}
/**
* Detects all possible occurrences of the main Web Speech API components
* in the global scope.
*
* The returning object will have the following structure (see example).
*
* @example
* EasySpeech.detect()
*
* {
* speechSynthesis: SpeechSynthesis|undefined,
* speechSynthesisUtterance: SpeechSynthesisUtterance|undefined,
* speechSynthesisVoice: SpeechSynthesisVoice|undefined,
* speechSynthesisEvent: SpeechSynthesisEvent|undefined,
* speechSynthesisErrorEvent: SpeechSynthesisErrorEvent|undefined,
* onvoiceschanged: Boolean,
* onboundary: Boolean,
* onend: Boolean,
* onerror: Boolean,
* onmark: Boolean,
* onpause: Boolean,
* onresume: Boolean,
* onstart: Boolean
* }
*
* @returns {object} An object containing all possible features and their status
*/
EasySpeech.detect = () => detectFeatures()
/** @private **/
const detectFeatures = () => {
const features = {}
;[
'speechSynthesis',
'speechSynthesisUtterance',
'speechSynthesisVoice',
'speechSynthesisEvent',
'speechSynthesisErrorEvent'
].forEach(feature => {
features[feature] = detect(feature)
})
features.onvoiceschanged = !!features.speechSynthesis && 'onvoiceschanged' in features.speechSynthesis
const hasUtterance = !!features.speechSynthesisUtterance?.prototype
utteranceEvents.forEach(event => {
const name = `on${event}`
features[name] = hasUtterance && name in features.speechSynthesisUtterance.prototype
})
return features
}
/**
* Common prefixes for browsers that tend to implement their custom names for
* certain parts of their API.
* @private
**/
const prefixes = ['webKit', 'moz', 'ms', 'o']
/**
* Make the first character of a String uppercase
* @private
**/
const capital = s => `${s.charAt(0).toUpperCase()}${s.slice(1)}`
/**
* Find a feature in global scope by checking for various combinations and
* variations of the base-name
* @param {String} baseName name of the component to look for, must begin with
* lowercase char
* @return {Object|undefined} The component from global scope, if found
* @private
**/
const detect = baseName => {
const capitalBaseName = capital(baseName)
const baseNameWithPrefixes = prefixes.map(p => `${p}${capitalBaseName}`)
const found = [baseName, capitalBaseName]
.concat(baseNameWithPrefixes)
.find(inGlobalScope)
return scope[found]
}
/**
* Returns, if a given name exists in global scope
* @private
* @param name
* @return {boolean}
*/
const inGlobalScope = name => !!scope[name]
/**
* Returns a shallow copy of the current internal status. Depending of the
* current state this might return an object with only a single field `status`
* or a complete Object, including detected features, `defaults`, `handlers`
* and supported `voices`.
*
* @example
* import EasySpeech from 'easy-speech'
*
* // uninitialized
* EasySpeech.status() // { status: 'created' }
*
* // after EasySpeech.init
* EasySpeech.status()
*
* {
* status: 'init: complete',
* initialized: true,
* speechSynthesis: speechSynthesis,
* speechSynthesisUtterance: SpeechSynthesisUtterance,
* speechSynthesisVoice: SpeechSynthesisVoice,
* speechSynthesisEvent: SpeechSynthesisEvent,
* speechSynthesisErrorEvent: SpeechSynthesisErrorEvent,
* voices: [...],
* defaults: {
* pitch: 1,
* rate: 1,
* volume: 1,
* voice: null
* },
* handlers: {}
* }
*
* @return {Object} the internal status
*/
EasySpeech.status = () => ({ ...internal })
/**
* Updates the internal status
* @private
* @param {String} s the current status to set
*/
const status = s => {
debug(s)
internal.status = s
}
/**
* This is the function you need to run, before being able to speak.
* It includes:
* - feature detection
* - feature assignment (into internal state)
* - voices loading
* - state update
* - inform caller about success
*
* It will load voices by a variety of strategies:
*
* - detect and that SpeechSynthesis is basically supported, if not -> fail
* - load voices directly
* - if not loaded but `onvoiceschanged` is available: use `onvoiceschanged`
* - if `onvoiceschanged` is not available: fallback to timeout
* - if `onvoiceschanged` is fired but no voices available: fallback to timeout
* - timeout reloads voices in a given `interval` until a `maxTimeout` is reached
* - if voices are loaded until then -> complete
* - if no voices found -> fail
*
* Note: if once initialized you can't re-init (will skip and resolve to
* `false`) unless you run `EasySpeech.reset()`.
*
* @return {Promise<Boolean>}
* @fulfil {Boolean} true, if initialized, false, if skipped (because already
* initialized)
* @reject {Error} - The error `message` property will always begin with
* `EasySpeech: ` and contain one of the following:
*
* - `browser misses features` - The browser will not be able to use speech
* synthesis at all as it misses crucial features
* - `browser has no voices (timeout)` - No voice could be loaded with neither
* of the given strategies; chances are high the browser does not have
* any voices embedded (example: Chromium on *buntu os')
*/
EasySpeech.init = function ({ maxTimeout = 5000, interval = 250 } = {}) {
return new Promise((resolve, reject) => {
if (internal.initialized) { return resolve(false) }
EasySpeech.reset()
status('init: start')
// there may be the case, that the browser needs to load using a timer
// so we declare it at the top to make sure the interval is always cleared
// when we exit the Promise via fail / complete
let timer
const fail = (errorMessage) => {
status(`init: failed (${errorMessage})`)
clearInterval(timer)
internal.initialized = false
return reject(new Error(`EasySpeech: ${errorMessage}`))
}
const complete = () => {
status('init: complete')
clearInterval(timer)
internal.initialized = true
return resolve(true)
}
// before initializing we force-detect all required browser features
const features = detectFeatures()
const hasAllFeatures = !!features.speechSynthesis && !!features.speechSynthesisUtterance
if (!hasAllFeatures) {
return fail('browser misses features')
}
// assign all detected features to our internal definitions
Object.keys(features).forEach(feature => {
internal[feature] = features[feature]
})
// start initializing
const { speechSynthesis } = internal
const voicesLoaded = () => {
const voices = speechSynthesis.getVoices() || []
if (voices.length > 0) {
internal.voices = voices
status(`voices loaded: ${voices.length}`)
return true
}
return false
}
status('init: voices')
// best case: detect if voices can be loaded directly
if (voicesLoaded()) { return complete() }
// last possible fallback method: run a timer until max. timeout and reload
const loadViaTimeout = () => {
status('init: voices (timer)')
let timeout = 0
timer = setInterval(() => {
if (voicesLoaded()) {
return complete()
}
if (timeout > maxTimeout) {
return fail('browser has no voices (timeout)')
}
timeout += interval
}, interval)
}
// detect if voices can be loaded after onveoiceschanged,
// but only if the browser supports this event
if (features.onvoiceschanged) {
status('init: voices (onvoiceschanged)')
speechSynthesis.onvoiceschanged = () => {
if (voicesLoaded()) { return complete() }
// xxx: some browsers (like chrome on android still have not all
// voices loaded at this point, whichs is why we need to enter
// the timeout-based method here.
return loadViaTimeout()
}
} else {
// for all browser not supporting onveoiceschanged we start a timer
// until we reach a certain timeout and try to get the voices
loadViaTimeout()
}
})
}
/**
* Placed as first line in functions that require `EasySpeech.init` before they
* can run.
* @private
*/
const ensureInit = () => {
if (!internal.initialized) {
throw new Error('EasySpeech: not initialized. Run EasySpeech.init() first')
}
}
/*******************************************************************************
*
* AVAILABLE ONLY AFTER INIT
*
******************************************************************************/
/**
* Returns all available voices.
*
* @condition `EasySpeech.init` must have been called and resolved to `true`
* @return {Array<SpeechSynthesisVoice>}
*/
EasySpeech.voices = () => {
ensureInit()
return internal.voices
}
/**
* Attaches global/default handlers to every utterance instance. The handlers
* will run in parallel to any additional handlers, attached when calling
* `EasySpeech.speak`
*
* @condition `EasySpeech.init` must have been called and resolved to `true`
*
* @param {Object} handlers
* @param {function=} handlers.boundary - optional, event handler
* @param {function=} handlers.end - optional, event handler
* @param {function=} handlers.error - optional, event handler
* @param {function=} handlers.mark - optional, event handler
* @param {function=} handlers.pause - optional, event handler
* @param {function=} handlers.resume - optional, event handler
* @param {function=} handlers.start - optional, event handler
*
* @return {Object} a shallow copy of the Object, containing all global handlers
*/
EasySpeech.on = (handlers) => {
ensureInit()
utteranceEvents.forEach(name => {
const handler = handlers[name]
if (validate.handler(handler)) {
internal.handlers[name] = handler
}
})
return { ...internal.handlers }
}
/**
* We use these keys to search for these events in handler objects and defaults
* @private
*/
const utteranceEvents = [
'boundary',
'end',
'error',
'mark',
'pause',
'resume',
'start'
]
/**
* Internal validation of passed parameters
* @private
*/
const validate = {
isNumber: n => typeof n === 'number' && !Number.isNaN(n),
pitch: p => validate.isNumber(p) && p >= 0 && p <= 2,
volume: v => validate.isNumber(v) && v >= 0 && v <= 1,
rate: r => validate.isNumber(r) && r >= 0.1 && r <= 10,
text: t => typeof t === 'string',
handler: h => typeof h === 'function',
// we prefer duck typing here, mostly because there are cases where
// SpeechSynthesisVoice is not defined on global scope but is supported
// when using getVoices().
voice: v => v && v.lang && v.name && v.voiceURI
}
/**
* Sets defaults for utterances. Invalid values will be ignored without error
* or warning.
*
* @see https://wicg.github.io/speech-api/#utterance-attributes
* @param {object=} options - Optional object containing values to set values
* @param {object=} options.voice - Optional `SpeechSynthesisVoice` instance or
* `SpeechSynthesisVoice`-like Object
* @param {number=} options.pitch - Optional pitch value >= 0 and <= 2
* @param {number=} options.rate - Optional rate value >= 0.1 and <= 10
* @param {number=} options.volume - Optional volume value >= 0 and <= 1
*
* @return {object} a shallow copy of the current defaults
*/
EasySpeech.defaults = (options) => {
ensureInit()
if (options) {
internal.defaults = internal.defaults || {}
;['voice', 'pitch', 'rate', 'volume'].forEach(name => {
const value = options[name]
const isValid = validate[name]
if (isValid(value)) {
internal.defaults[name] = value
}
})
}
return { ...internal.defaults }
}
/**
* Determines the current voice and makes sure, there is always a voice returned
* @private
* @param voice
* @return {*|SpeechSynthesisVoice|{}}
*/
const getCurrentVoice = voice => voice || internal.defaults.voice || internal.voices[0] || {}
/**
* Creates a new `SpeechSynthesisUtterance` instance
* @private
* @param text
*/
const createUtterance = text => {
const UtteranceClass = internal.speechSynthesisUtterance
return new UtteranceClass(text)
}
/**
* Speaks a voice by given parameters, constructs utterance by best possible
* combinations of parameters and defaults.
*
* If the given utterance parameters are missing or invalid, defaults will be
* used as fallback.
*
* @example
* const voice = EasySpeech.voices()[10] // get a voice you like
*
* EasySpeech.speak({
* text: 'Hello, world',
* voice: voice,
* pitch: 1.2, // a little bit higher
* rate: 1.7, // a little bit faster
* boundary: event => console.debug('word boundary reached', event.charIndex),
* error: e => notify(e)
* })
*
* @param {object} options - required options
* @param {string} text - required text to speak
* @param {object=} voice - optional `SpeechSynthesisVoice` instance or
* structural similar object (if `SpeechSynthesisUtterance` is not supported)
* @param {number=} options.pitch - Optional pitch value >= 0 and <= 2
* @param {number=} options.rate - Optional rate value >= 0.1 and <= 10
* @param {number=} options.volume - Optional volume value >= 0 and <= 1
* @param {object=} handlers - optional additional local handlers, can be
* directly added as top-level properties of the options
* @param {function=} handlers.boundary - optional, event handler
* @param {function=} handlers.end - optional, event handler
* @param {function=} handlers.error - optional, event handler
* @param {function=} handlers.mark - optional, event handler
* @param {function=} handlers.pause - optional, event handler
* @param {function=} handlers.resume - optional, event handler
* @param {function=} handlers.start - optional, event handler
*
*
* @return {Promise<SpeechSynthesisEvent|SpeechSynthesisErrorEvent>}
* @fulfill {SpeechSynthesisEvent} Resolves to the `end` event
* @reject {SpeechSynthesisEvent} rejects using the `error` event
*/
EasySpeech.speak = ({ text, voice, pitch, rate, volume, ...handlers }) => {
ensureInit()
if (!validate.text(text)) {
throw new Error('EasySpeech: at least some valid text is required to speak')
}
const getValue = options => {
const [name, value] = Object.entries(options)[0]
if (validate[name](value)) {
return value
}
return internal.defaults && internal.defaults[name]
}
return new Promise((resolve, reject) => {
status('init speak')
const utterance = createUtterance(text)
const currentVoice = getCurrentVoice(voice)
utterance.voice = currentVoice
utterance.lang = currentVoice.lang
utterance.voiceURI = currentVoice.voiceURI
utterance.text = text
utterance.pitch = getValue({ pitch })
utterance.rate = getValue({ rate })
utterance.volume = getValue({ volume })
utteranceEvents.forEach(name => {
const fn = handlers[name]
if (validate.handler(fn)) {
utterance.addEventListener(name, fn)
}
if (internal.handlers[name]) {
utterance.addEventListener(name, internal.handlers[name])
}
})
// always attached are start, end and error listeners
// XXX: chrome won't play longer tts texts in one piece and stops after a few
// words. We need to add an intervall here in order prevent this. See:
// https://stackoverflow.com/questions/21947730/chrome-speech-synthesis-with-longer-texts
//
// XXX: this apparently works only on chrome desktop, while it breaks chrome
// mobile (android), which is why we skip this on these devices
utterance.addEventListener('start', () => {
if (internal.speechSynthesisVoice) {
resumeInfinity(utterance)
}
})
utterance.addEventListener('end', endEvent => {
status('speak complete')
clearTimeout(timeoutResumeInfinity)
resolve(endEvent)
})
utterance.addEventListener('error', errorEvent => {
status(`speak failed: ${errorEvent?.message}`)
clearTimeout(timeoutResumeInfinity)
reject(errorEvent)
})
// make sure we have no mem-leak
clearTimeout(timeoutResumeInfinity)
internal.speechSynthesis.cancel()
setTimeout(() => {
internal.speechSynthesis.speak(utterance)
}, 10)
})
}
/**
* Timer variable to clear interval
* @private
*/
let timeoutResumeInfinity
/**
* Fixes long texts in some browsers
* @private
* @param target
*/
function resumeInfinity (target) {
if (!target && timeoutResumeInfinity) {
debug('force-clear timeout')
return clearTimeout(timeoutResumeInfinity)
}
internal.speechSynthesis.pause()
internal.speechSynthesis.resume()
timeoutResumeInfinity = setTimeout(function () {
resumeInfinity(target)
}, 5000)
}
/**
* Cancels the current speaking, if any running
*/
EasySpeech.cancel = () => {
ensureInit()
status('cancelling')
internal.speechSynthesis.cancel()
}
/**
* Resumes to speak, if any paused
*/
EasySpeech.resume = () => {
ensureInit()
status('resuming')
internal.speechSynthesis.resume()
}
/**
* Pauses the current speaking, if any running
*/
EasySpeech.pause = () => {
ensureInit()
status('pausing')
internal.speechSynthesis.pause()
}
/**
* Resets the internal state to a default-uninitialized state
*/
EasySpeech.reset = () => {
Object.assign(internal, {
status: 'reset',
initialized: false,
speechSynthesis: null,
speechSynthesisUtterance: null,
speechSynthesisVoice: null,
speechSynthesisEvent: null,
speechSynthesisErrorEvent: null,
voices: null,
defaults: {
pitch: 1,
rate: 1,
volume: 1,
voice: null
},
handlers: {}
})
}
/**
* EasySpeech is the default export; you can import it with whichever name you
* like
*
* @example
* import EasySpeech from 'easy-speech'
* @example
* import Easy from 'easy-speech'
*/
export default EasySpeech