nitor-tts
Version:
Browser TTS (using Web speech API) made easy
88 lines (81 loc) • 3.39 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('nitor-tts', ['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['nitor-tts'] = {}));
}(this, (function (exports) { 'use strict';
var NitorTTS = /** @class */ (function () {
function NitorTTS() {
this.speech = new SpeechSynthesisUtterance();
window.speechSynthesis.getVoices();
}
NitorTTS.prototype.hasbrowserSupport = function () {
return this.browserSupport = 'speechSynthesis' in window && 'SpeechSynthesisUtterance' in window;
};
NitorTTS.prototype.init = function (voice, lang, volume, rate, pitch) {
this.voices = window.speechSynthesis.getVoices();
if (this.voices.filter(function (v) { return v.name == voice; })[0]) {
this.speech.voice = this.voices.filter(function (v) { return v.name == voice; })[0];
}
else if (this.voices[0]) {
this.speech.voice = this.voices[0];
}
else {
throw 'Error setting voice. The voice you passed is not valid or the voices have not been loaded yet.';
}
this.speech.lang = lang;
if (volume != undefined) {
if (volume >= 0 && volume <= 1) {
this.speech.volume = volume;
}
else {
throw 'Error setting volume. Please verify your volume value is a number between 0 and 1.';
}
}
if (rate != undefined) {
if (rate >= 0 && rate <= 10) {
this.speech.rate = rate;
}
else {
throw 'Error setting rate. Please verify your volume value is a number between 0 and 10.';
}
}
if (pitch != undefined) {
if (pitch >= 0 && pitch <= 2) {
this.speech.pitch = pitch;
}
else {
throw 'Error setting pitch. Please verify your pitch value is a number between 0 and 2.';
}
}
};
NitorTTS.prototype.speak = function (speechData) {
this.speech.text = speechData;
window.speechSynthesis.speak(this.speech);
};
NitorTTS.prototype.paused = function () {
return speechSynthesis.paused;
};
NitorTTS.prototype.speaking = function () {
return speechSynthesis.speaking;
};
NitorTTS.prototype.pause = function () {
speechSynthesis.pause();
};
NitorTTS.prototype.resume = function () {
speechSynthesis.resume();
};
NitorTTS.prototype.cancel = function () {
speechSynthesis.cancel();
};
return NitorTTS;
}());
/*
* Public API Surface of nitor-tts
*/
/**
* Generated bundle index. Do not edit.
*/
exports.NitorTTS = NitorTTS;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=nitor-tts.umd.js.map