Comparing version 1.0.2 to 1.1.0
@@ -7,3 +7,3 @@ import Speech from '../src/speak-tts.js' | ||
voices.forEach((voice) => { | ||
html += '<option value="' + voice.lang + '"">' + voice.name + ' (' + voice.lang + ')</option>' | ||
html += `<option value="${voice.lang}" data-name="${voice.name}">${voice.name} (${voice.lang})</option>` | ||
}) | ||
@@ -20,2 +20,3 @@ list.innerHTML = html | ||
Speech.setLanguage(languages.value) | ||
Speech.setVoice(languages.options[languages.selectedIndex].dataset.name) | ||
Speech.speak({ | ||
@@ -45,2 +46,2 @@ text: textarea.value, | ||
const text = (Speech.browserSupport()) ? 'Hurray, your browser supports speech synthesis' : "Your browser does NOT support speech synthesis. Try using Chrome of Safari instead !" | ||
document.getElementById("support").innerHTML = text | ||
document.getElementById("support").innerHTML = text |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
@@ -34,8 +36,8 @@ | ||
'pitch': 1, | ||
'voice': null, | ||
'onVoicesLoaded': function onVoicesLoaded(data) {} | ||
}; | ||
//Fallback cache voices for ios | ||
// iOS 8 | ||
var iOS8voices = [{ name: "pt-BR", voiceURI: "pt-BR", lang: "pt-BR", localService: true, default: true }, { name: "fr-CA", voiceURI: "fr-CA", lang: "fr-CA", localService: true, default: true }, { name: "sk-SK", voiceURI: "sk-SK", lang: "sk-SK", localService: true, default: true }, { name: "th-TH", voiceURI: "th-TH", lang: "th-TH", localService: true, default: true }, { name: "ro-RO", voiceURI: "ro-RO", lang: "ro-RO", localService: true, default: true }, { name: "no-NO", voiceURI: "no-NO", lang: "no-NO", localService: true, default: true }, { name: "fi-FI", voiceURI: "fi-FI", lang: "fi-FI", localService: true, default: true }, { name: "pl-PL", voiceURI: "pl-PL", lang: "pl-PL", localService: true, default: true }, { name: "de-DE", voiceURI: "de-DE", lang: "de-DE", localService: true, default: true }, { name: "nl-NL", voiceURI: "nl-NL", lang: "nl-NL", localService: true, default: true }, { name: "id-ID", voiceURI: "id-ID", lang: "id-ID", localService: true, default: true }, { name: "tr-TR", voiceURI: "tr-TR", lang: "tr-TR", localService: true, default: true }, { name: "it-IT", voiceURI: "it-IT", lang: "it-IT", localService: true, default: true }, { name: "pt-PT", voiceURI: "pt-PT", lang: "pt-PT", localService: true, default: true }, { name: "fr-FR", voiceURI: "fr-FR", lang: "fr-FR", localService: true, default: true }, { name: "ru-RU", voiceURI: "ru-RU", lang: "ru-RU", localService: true, default: true }, { name: "es-MX", voiceURI: "es-MX", lang: "es-MX", localService: true, default: true }, { name: "zh-HK", voiceURI: "zh-HK", lang: "zh-HK", localService: true, default: true }, { name: "sv-SE", voiceURI: "sv-SE", lang: "sv-SE", localService: true, default: true }, { name: "hu-HU", voiceURI: "hu-HU", lang: "hu-HU", localService: true, default: true }, { name: "zh-TW", voiceURI: "zh-TW", lang: "zh-TW", localService: true, default: true }, { name: "es-ES", voiceURI: "es-ES", lang: "es-ES", localService: true, default: true }, { name: "zh-CN", voiceURI: "zh-CN", lang: "zh-CN", localService: true, default: true }, { name: "nl-BE", voiceURI: "nl-BE", lang: "nl-BE", localService: true, default: true }, { name: "en-GB", voiceURI: "en-GB", lang: "en-GB", localService: true, default: true }, { name: "ar-SA", voiceURI: "ar-SA", lang: "ar-SA", localService: true, default: true }, { name: "ko-KR", voiceURI: "ko-KR", lang: "ko-KR", localService: true, default: true }, { name: "cs-CZ", voiceURI: "cs-CZ", lang: "cs-CZ", localService: true, default: true }, { name: "en-ZA", voiceURI: "en-ZA", lang: "en-ZA", localService: true, default: true }, { name: "en-AU", voiceURI: "en-AU", lang: "en-AU", localService: true, default: true }, { name: "da-DK", voiceURI: "da-DK", lang: "da-DK", localService: true, default: true }, { name: "en-US", voiceURI: "en-US", lang: "en-US", localService: true, default: true }, { name: "en-IE", voiceURI: "en-IE", lang: "en-IE", localService: true, default: true }, { name: "he-IL", voiceURI: "he-IL", lang: "he-IL", localService: true, default: true }, { name: "hi-IN", voiceURI: "hi-IN", lang: "hi-IN", localService: true, default: true }, { name: "el-GR", voiceURI: "el-GR", lang: "el-GR", localService: true, default: true }, { name: "ja-JP", voiceURI: "ja-JP", lang: "ja-JP", localService: true, default: true }]; | ||
//Fallback cache voices for ios | ||
// iOS 8 | ||
};var iOS8voices = [{ name: "pt-BR", voiceURI: "pt-BR", lang: "pt-BR", localService: true, default: true }, { name: "fr-CA", voiceURI: "fr-CA", lang: "fr-CA", localService: true, default: true }, { name: "sk-SK", voiceURI: "sk-SK", lang: "sk-SK", localService: true, default: true }, { name: "th-TH", voiceURI: "th-TH", lang: "th-TH", localService: true, default: true }, { name: "ro-RO", voiceURI: "ro-RO", lang: "ro-RO", localService: true, default: true }, { name: "no-NO", voiceURI: "no-NO", lang: "no-NO", localService: true, default: true }, { name: "fi-FI", voiceURI: "fi-FI", lang: "fi-FI", localService: true, default: true }, { name: "pl-PL", voiceURI: "pl-PL", lang: "pl-PL", localService: true, default: true }, { name: "de-DE", voiceURI: "de-DE", lang: "de-DE", localService: true, default: true }, { name: "nl-NL", voiceURI: "nl-NL", lang: "nl-NL", localService: true, default: true }, { name: "id-ID", voiceURI: "id-ID", lang: "id-ID", localService: true, default: true }, { name: "tr-TR", voiceURI: "tr-TR", lang: "tr-TR", localService: true, default: true }, { name: "it-IT", voiceURI: "it-IT", lang: "it-IT", localService: true, default: true }, { name: "pt-PT", voiceURI: "pt-PT", lang: "pt-PT", localService: true, default: true }, { name: "fr-FR", voiceURI: "fr-FR", lang: "fr-FR", localService: true, default: true }, { name: "ru-RU", voiceURI: "ru-RU", lang: "ru-RU", localService: true, default: true }, { name: "es-MX", voiceURI: "es-MX", lang: "es-MX", localService: true, default: true }, { name: "zh-HK", voiceURI: "zh-HK", lang: "zh-HK", localService: true, default: true }, { name: "sv-SE", voiceURI: "sv-SE", lang: "sv-SE", localService: true, default: true }, { name: "hu-HU", voiceURI: "hu-HU", lang: "hu-HU", localService: true, default: true }, { name: "zh-TW", voiceURI: "zh-TW", lang: "zh-TW", localService: true, default: true }, { name: "es-ES", voiceURI: "es-ES", lang: "es-ES", localService: true, default: true }, { name: "zh-CN", voiceURI: "zh-CN", lang: "zh-CN", localService: true, default: true }, { name: "nl-BE", voiceURI: "nl-BE", lang: "nl-BE", localService: true, default: true }, { name: "en-GB", voiceURI: "en-GB", lang: "en-GB", localService: true, default: true }, { name: "ar-SA", voiceURI: "ar-SA", lang: "ar-SA", localService: true, default: true }, { name: "ko-KR", voiceURI: "ko-KR", lang: "ko-KR", localService: true, default: true }, { name: "cs-CZ", voiceURI: "cs-CZ", lang: "cs-CZ", localService: true, default: true }, { name: "en-ZA", voiceURI: "en-ZA", lang: "en-ZA", localService: true, default: true }, { name: "en-AU", voiceURI: "en-AU", lang: "en-AU", localService: true, default: true }, { name: "da-DK", voiceURI: "da-DK", lang: "da-DK", localService: true, default: true }, { name: "en-US", voiceURI: "en-US", lang: "en-US", localService: true, default: true }, { name: "en-IE", voiceURI: "en-IE", lang: "en-IE", localService: true, default: true }, { name: "he-IL", voiceURI: "he-IL", lang: "he-IL", localService: true, default: true }, { name: "hi-IN", voiceURI: "hi-IN", lang: "hi-IN", localService: true, default: true }, { name: "el-GR", voiceURI: "el-GR", lang: "el-GR", localService: true, default: true }, { name: "ja-JP", voiceURI: "ja-JP", lang: "ja-JP", localService: true, default: true }]; | ||
@@ -130,2 +132,20 @@ // IOS9 | ||
var setVoice = function setVoice(voice) { | ||
var voices = window.speechSynthesis.getVoices(); | ||
// set voice by ID/index | ||
if (typeof voice === 'number') { | ||
voice = voices[i]; | ||
} | ||
// set voice by name | ||
if (typeof voice === 'string') { | ||
voice = voices.find(function (v) { | ||
return v.name === voice; | ||
}); | ||
} | ||
// Set the voice in conf if found | ||
if ((typeof voice === 'undefined' ? 'undefined' : _typeof(voice)) === 'object') { | ||
CONF.voice = voice; | ||
} | ||
}; | ||
var speak = function speak(data) { | ||
@@ -202,2 +222,10 @@ var text = data.text, | ||
// Get configured voice, or first for current language | ||
var voice = function (lang) { | ||
if (CONF.voice) return CONF.voice; | ||
return window.speechSynthesis.getVoices().find(function (voice) { | ||
return voice.lang.replace('_', '-') === lang; // handle android specificites | ||
}); | ||
}(lang); | ||
// Stop current speech | ||
@@ -211,5 +239,2 @@ stop(); | ||
var utterance = new window.SpeechSynthesisUtterance(); | ||
var voice = window.speechSynthesis.getVoices().find(function (voice) { | ||
return voice.lang.replace('_', '-') === lang; // handle android specificites | ||
}); | ||
utterance.lang = lang; | ||
@@ -220,6 +245,5 @@ utterance.volume = parseFloat(CONF.volume); // 0 to 1 | ||
utterance.text = sentence; | ||
utterance.voice = voice; | ||
if (voice) { | ||
utterance.voice = voice; | ||
} else { | ||
if (!voice) { | ||
if (onError) onError({ msg: 'no voice available' }); | ||
@@ -232,2 +256,3 @@ return; | ||
}; | ||
utterance.onend = function (e) { | ||
@@ -246,3 +271,4 @@ if (onEnd && isLast) onEnd(); | ||
stop: stop, | ||
setLanguage: setLanguage | ||
setLanguage: setLanguage, | ||
setVoice: setVoice | ||
}; | ||
@@ -249,0 +275,0 @@ }(window); |
{ | ||
"name": "speak-tts", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Browser TTS (using speechSynthesis API) made easy", | ||
@@ -5,0 +5,0 @@ "main": "lib/speak-tts.js", |
233
README.md
@@ -41,2 +41,3 @@ Speech synthesis made easy - Browser based text to speech (TTS) | ||
- lang : if you don't pass a language, the language of the given text will be automatically detected thanks to franc (https://github.com/wooorm/franc). If you pass a language, this will be used for all audio outputs (nevertheless the language of the selected text) | ||
- voice : the voice to use. If you do not pass a voice, the first one available for `lang` will be used | ||
@@ -47,6 +48,7 @@ ```bash | ||
'onVoicesLoaded': (data) => {console.log('voices', data.voices)}, | ||
'lang': 'en-GB', // specify en-GB language (no detection applied) | ||
'lang': 'en-US', // specify en-US language (no detection applied) | ||
'volume': 0.5, | ||
'rate': 0.8, | ||
'pitch': 0.8 | ||
'pitch': 0.8, | ||
'voice': 'Samantha' | ||
}) | ||
@@ -56,3 +58,3 @@ ``` | ||
```bash | ||
```javascript | ||
if(Speech.browserSupport()) { | ||
@@ -65,6 +67,6 @@ console.log("speech synthesis supported") | ||
```bash | ||
```javascript | ||
Speech.speak({ | ||
text: 'Hello, how are you today ?', | ||
onError: (e) => {console.log('sorry an error occured.', e)}, // optionnal error callback | ||
onError: (e) => {console.log('sorry an error occurred.', e)}, // optionnal error callback | ||
onEnd: () => {console.log('your text has successfully been spoken.')} // optionnal onEnd callback | ||
@@ -76,10 +78,16 @@ }) | ||
```bash | ||
Speech.setLanguage('en-GB') // set language to english | ||
Speech.setLanguage(null) // activate language autodetection | ||
```javascript | ||
Speech.setLanguage('en-US') // set language to US English | ||
Speech.setLanguage(null) // activate language auto-detection | ||
``` | ||
Set the voice (note that the voice must be supported by the client browser) : | ||
```javascript | ||
Speech.setVoice('Fiona') // set voice to 'Fiona' | ||
``` | ||
Stop talking: | ||
```bash | ||
```javascript | ||
Speech.stop() | ||
@@ -89,76 +97,159 @@ ``` | ||
## Supported languages | ||
"ar-SA" // arabic | ||
"cs-CZ" // czech | ||
"da-DK" // danish | ||
"de-DE" // german | ||
"el-GR" // greek | ||
"en-AU" // australian | ||
"en-GB" // english | ||
"en-IE" | ||
"en-US" | ||
"en-US" | ||
"en-ZA" | ||
"es-ES" // spanish | ||
"es-MX" | ||
"fi-FI" // finish | ||
"fr-CA" | ||
"fr-FR" // french | ||
"he-IL" // hebrew | ||
"hi-IN" // hindi | ||
"hu-HU" // hungarian | ||
"id-ID" // indonesian | ||
"it-IT" // italian | ||
"ja-JP" // japanese | ||
"ko-KR" // korean | ||
"nl-NL" // dutch | ||
"no-NO" // norwegian | ||
"pl-PL" // polish | ||
"pt-BR" // portuguese brazilian | ||
"pt-PT" // portuguese | ||
"ro-RO" // romanian | ||
"ru-RU" // russian | ||
"sk-SK" // slovak | ||
"sv-SE" // swedish | ||
"th-TH" // thai | ||
"tr-TR" // turkish | ||
"zh-CN" // chinese (S) | ||
"zh-HK" // chinese hong kong | ||
"zh-TW" // chinese (T'en-US'; | ||
``` | ||
ar-SA | ||
cs-CZ | ||
da-DK | ||
de-DE | ||
el-GR | ||
en | ||
en-AU | ||
en-GB | ||
en-IE | ||
en-IN | ||
en-US | ||
en-ZA | ||
es-AR | ||
es-ES | ||
es-MX | ||
es-US | ||
fi-FI | ||
fr-CA | ||
fr-FR | ||
he-IL | ||
hi-IN | ||
hu-HU | ||
id-ID | ||
it-IT | ||
ja-JP | ||
ko-KR | ||
nb-NO | ||
nl-BE | ||
nl-NL | ||
pl-PL | ||
pt-BR | ||
pt-PT | ||
ro-RO | ||
ru-RU | ||
sk-SK | ||
sv-SE | ||
th-TH | ||
tr-TR | ||
zh-CN | ||
zh-HK | ||
zh-TW | ||
``` | ||
## Supported Voices | ||
``` | ||
Alex | ||
Alice | ||
Alva | ||
Amelie | ||
Anna | ||
Carmit | ||
Damayanti | ||
Daniel | ||
Diego | ||
Ellen | ||
Fiona | ||
Fred | ||
Ioana | ||
Joana | ||
Jorge | ||
Juan | ||
Kanya | ||
Karen | ||
Kyoko | ||
Laura | ||
Lekha | ||
Luca | ||
Luciana | ||
Maged | ||
Mariska | ||
Mei-Jia | ||
Melina | ||
Milena | ||
Moira | ||
Monica | ||
Nora | ||
Paulina | ||
Samantha | ||
Sara | ||
Satu | ||
Sin-ji | ||
Tessa | ||
Thomas | ||
Ting-Ting | ||
Veena | ||
Victoria | ||
Xander | ||
Yelda | ||
Yuna | ||
Yuri | ||
Zosia | ||
Zuzana | ||
Google Deutsch | ||
Google US English | ||
Google UK English Female | ||
Google UK English Male | ||
Google español | ||
Google español de Estados Unidos | ||
Google français | ||
Google हिन्दी | ||
Google Bahasa Indonesia | ||
Google italiano | ||
Google 日本語 | ||
Google 한국의 | ||
Google Nederlands | ||
Google polski | ||
Google português do Brasil | ||
Google русский | ||
Google 普通话(中国大陆) | ||
Google 粤語(香港) | ||
Google 國語(臺灣) | ||
``` | ||
## Full demo code (es6 example) | ||
```bash | ||
```javascript | ||
import Speech from 'speak-tts' | ||
const _addVoicesList = (voices) => { | ||
const list = window.document.createElement('div') | ||
let html = '<h2>Available Voices</h2><select id="languages"><option value="">autodetect language</option>' | ||
voices.forEach((voice) => { | ||
html += '<option value="' + voice.lang + '"">' + voice.name + ' (' + voice.lang + ')</option>' | ||
}) | ||
list.innerHTML = html | ||
window.document.body.appendChild(list) | ||
const list = window.document.createElement('div') | ||
let html = '<h2>Available Voices</h2><select id="languages"><option value="">autodetect language</option>' | ||
voices.forEach((voice) => { | ||
html += `<option value="${voice.lang}" data-name="${voice.name}">${voice.name} (${voice.lang})</option>` | ||
}) | ||
list.innerHTML = html | ||
window.document.body.appendChild(list) | ||
} | ||
function _prepareSpeakButton() { | ||
const speakButton = document.getElementById('play') | ||
const textarea = document.getElementById('text') | ||
const languages = document.getElementById('languages') | ||
speakButton.addEventListener('click', () => { | ||
Speech.setLanguage(languages.value) | ||
Speech.speak({ | ||
text: textarea.value | ||
}) | ||
}) | ||
const speakButton = document.getElementById('play') | ||
const textarea = document.getElementById('text') | ||
const languages = document.getElementById('languages') | ||
speakButton.addEventListener('click', () => { | ||
Speech.setLanguage(languages.value) | ||
Speech.setVoice(languages.options[languages.selectedIndex].dataset.name) | ||
Speech.speak({ | ||
text: textarea.value, | ||
onEnd: () => { | ||
console.log('end of text') | ||
} | ||
}) | ||
}) | ||
} | ||
Speech.init({ | ||
onVoicesLoaded: (data) => { | ||
console.log("loaded voices", data.voices) | ||
_addVoicesList(data.voices) | ||
_prepareSpeakButton() | ||
Speech.speak({ | ||
text: 'Hello, how are you today ?' | ||
}) | ||
} | ||
onVoicesLoaded: (data) => { | ||
console.log("loaded voices", data.voices) | ||
_addVoicesList(data.voices) | ||
_prepareSpeakButton() | ||
Speech.speak({ | ||
text: 'Hello, how are you today ?', | ||
onEnd: () => { | ||
console.log('end of text') | ||
}, | ||
}) | ||
} | ||
}); | ||
@@ -165,0 +256,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
664866
14
2178
269