UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

98 lines (97 loc) • 3.01 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var speech_synthesis_exports = {}; __export(speech_synthesis_exports, { SpeechSynthesis: () => SpeechSynthesis }); module.exports = __toCommonJS(speech_synthesis_exports); var import_validate = require("./validate.cjs"); function synthesis() { if (typeof window === "undefined") return void 0; const candidate = window.speechSynthesis; return candidate && typeof candidate.speak === "function" ? candidate : void 0; } function readMaxInputLength() { return synthesis() ? 32767 : 0; } function findVoice(synth, name) { return synth.getVoices().find((voice) => voice.voiceURI === name || voice.name === name); } function toVoice(voice) { return { identifier: voice.voiceURI, language: voice.lang, name: voice.name, quality: "default" }; } function speak(text, options) { (0, import_validate.assertSpeakText)(text); (0, import_validate.assertSpeakOptions)(options); const synth = synthesis(); if (!synth) return; const utterance = new SpeechSynthesisUtterance(text); if (options?.language !== void 0) utterance.lang = options.language; if (options?.pitch !== void 0) utterance.pitch = options.pitch; if (options?.rate !== void 0) utterance.rate = options.rate; if (options?.volume !== void 0) utterance.volume = options.volume; if (options?.voice !== void 0) { const voice = findVoice(synth, options.voice); if (voice) utterance.voice = voice; } utterance.onstart = () => options?.onStart?.(); utterance.onend = () => options?.onDone?.(); utterance.onerror = (event) => { if (event.error === "canceled") { options?.onStopped?.(); return; } options?.onError?.(new Error(`SpeechSynthesis.speak: ${event.error}`)); }; synth.speak(utterance); } async function stop() { synthesis()?.cancel(); } async function pause() { synthesis()?.pause(); } async function resume() { synthesis()?.resume(); } async function isSpeaking() { const synth = synthesis(); return synth ? synth.speaking : false; } async function getAvailableVoices() { const synth = synthesis(); if (!synth) return []; return synth.getVoices().map(toVoice); } const SpeechSynthesis = Object.freeze({ get maxInputLength() { return readMaxInputLength(); }, speak, stop, pause, resume, isSpeaking, getAvailableVoices });