├── demo
├── .DS_Store
├── css
│ ├── .DS_Store
│ ├── page.css
│ └── cssgram.min.css
├── csstest.jpg
├── images
│ ├── jtt.jpg
│ └── .DS_Store
├── regonition.html
├── index.html
└── demo.html
├── web-speech-regonition.md
└── README.md
/demo/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackPu/text-to-speech/HEAD/demo/.DS_Store
--------------------------------------------------------------------------------
/demo/css/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackPu/text-to-speech/HEAD/demo/css/.DS_Store
--------------------------------------------------------------------------------
/demo/csstest.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackPu/text-to-speech/HEAD/demo/csstest.jpg
--------------------------------------------------------------------------------
/demo/images/jtt.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackPu/text-to-speech/HEAD/demo/images/jtt.jpg
--------------------------------------------------------------------------------
/demo/images/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JackPu/text-to-speech/HEAD/demo/images/.DS_Store
--------------------------------------------------------------------------------
/web-speech-regonition.md:
--------------------------------------------------------------------------------
1 | # 使用 JavaScript 进行语音识别
2 |
3 | 之前写过了[语音合成,即是进行文字发音](https://github.com/JackPu/text-to-speech),当然现在也支持了语音识别,
4 | 即将你所说的转化成文字。Chrome 浏览器在版本25之后开始对这一特性的支持。
5 |
6 | ### 基础用法
7 |
8 | ``` javascript
9 | var recognition = new webkitSpeechRecognition();
10 | recognition.onresult = function(event) {
11 | console.log(event)
12 | }
13 | recognition.start();
14 |
15 | ```
16 | 这里操作实际会让用户授权页面开启麦克风,如果用户允许的话,用户可以开始说话了,如果你停说话了,onresult注册的时间
17 | 则会被触发,并会讲捕获的音频返回成一个JavaScript对象。
18 |
19 |
20 | #### 响应流
21 | [DEMO地址](https://www.google.com/intl/en/chrome/demos/speech.html)
22 |
23 | 你需要等待用户准备好对话,并且直到对话结束;
24 |
25 | ``` js
26 | var recognition = new webkitSpeechRecognition();
27 | recognition.continuous = true;
28 | recognition.interimResults = true;
29 | recognition.onresult = function(event) {
30 | console.log(event)
31 | }
32 | recognition.start();
33 | ```
34 | 这样你可以在用户开始讲话时,提前渲染结果。
35 |
36 | 你可以自动以识别的语言,默认情况为所在地区语言。
37 |
38 | #### x-webkit-speech
39 |
40 | Webkit 内核的浏览器支持语音输入
41 |
42 | ``` html
43 |
44 | ```
45 | 它会识别音频并进行转化为文字
46 |
47 | ### 安全性
48 |
49 | http协议下浏览器每次都会提醒用户去确认语音操作,然而https的页面,没有这样一个麻烦的操作。
50 | JavaScript上下文,整个页面,都能过访问到捕获的音频。
51 |
52 | ### 小结
53 | JavaScript的语音识别总体还并未大范围使用,而且受限于浏览器支持,因此只有少数需求或许能够使用到把。
54 |
55 |
56 | ### 参考
57 |
58 | + [The HTML5 Speech Recognition API](http://shapeshed.com/html5-speech-recognition-api/)
59 |
60 | + [Voice Driven Web Apps: Introduction to the Web Speech API](https://developers.google.com/web/updates/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API?hl=en)
61 |
62 |
--------------------------------------------------------------------------------
/demo/css/page.css:
--------------------------------------------------------------------------------
1 | *{
2 | margin:0;
3 | padding: 0;
4 | }
5 |
6 | body{
7 | font-size: 16px;
8 | font-family: Microsoft Yahei, SimHei, arial, sans-serif;
9 | }
10 | .page{
11 | position: relative;
12 | width: 100%;
13 | }
14 | img{
15 | max-width: 100%;
16 | }
17 | a{
18 | color: #1ba1e2;
19 | text-decoration: none;
20 | }
21 | h1{
22 | text-align: center;
23 | padding-top: 30px;
24 | }
25 |
26 | h1 img{
27 | width: 480px;
28 |
29 | }
30 | h2{
31 | width: 960px;
32 | margin: 0 auto;
33 | line-height: 35px;
34 | font-size: 18px;
35 | padding: 15px;
36 | background: #f1f1f1;
37 | }
38 | ul{
39 | padding-left: 40px;
40 | }
41 | .img-wrap{
42 | width: 660px;
43 | margin: 60px auto;
44 | display: flex;
45 | justify-content: space-between;
46 | }
47 | .img-wrap .item{
48 | width: 320px;
49 | text-align: center;
50 | }
51 | p{
52 | padding: 10px;
53 | color:#454545;
54 | }
55 | .img-wrap .item .filter-use{
56 | color: #1ba1e2;
57 | }
58 |
59 | .m-form{
60 | text-align: center;
61 | background: #f3f3f3;
62 | padding: 20px;
63 | }
64 |
65 | .search{
66 | width: 180px;
67 | height: 32px;
68 | border:1px solid #ddd;
69 | }
70 | .doc{
71 | width: 960px;
72 | margin: 20px auto;
73 | }
74 | .ft{
75 | padding-top: 40px;
76 | padding-bottom: 30px;
77 | }
78 | pre{
79 | border-width: 0px;
80 | }
81 | @media all and (max-width:960px) {
82 | h2,.doc,.img-wrap{
83 | width:95%;
84 | flex-wrap: wrap;
85 | }
86 | .img-wrap .item{
87 | width: 100%'
88 | }
89 | }
90 | /** view doc https://highlightjs.org **/
91 | .hljs {
92 | display: block;
93 | overflow-x: auto;
94 | padding: 0.5em;
95 | background: #f7f7f7;
96 | }
97 | .hljs,
98 | .hljs-subst {
99 | color: #2c3e50;
100 | }
101 | .hljs-comment {
102 | color: #999;
103 | }
104 | .hljs-keyword,
105 | .hljs-attribute,
106 | .hljs-selector-tag,
107 | .hljs-meta-keyword,
108 | .hljs-doctag,
109 | .hljs-name {
110 | font-weight: bold;
111 | }
112 | .hljs-type,
113 | .hljs-string,
114 | .hljs-number,
115 | .hljs-selector-id,
116 | .hljs-selector-class,
117 | .hljs-quote,
118 | .hljs-template-tag,
119 | .hljs-deletion {
120 | color: #e74c3c;
121 | }
122 | .hljs-title,
123 | .hljs-section {
124 | color: #e74c3c;
125 | font-weight: bold;
126 | }
127 | .hljs-regexp,
128 | .hljs-symbol,
129 | .hljs-variable,
130 | .hljs-template-variable,
131 | .hljs-link,
132 | .hljs-selector-attr,
133 | .hljs-selector-pseudo {
134 | color: #bc6060;
135 | }
136 | .hljs-literal {
137 | color: #78a960;
138 | }
139 | .hljs-built_in,
140 | .hljs-bullet,
141 | .hljs-code,
142 | .hljs-addition {
143 | color: #16a085;
144 | }
145 | .hljs-meta {
146 | color: #2980b9;
147 | }
148 | .hljs-meta-string {
149 | color: #2980b9;
150 | }
151 | .hljs-emphasis {
152 | font-style: italic;
153 | }
154 | .hljs-strong {
155 | font-weight: bold;
156 | }
--------------------------------------------------------------------------------
/demo/regonition.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Use JavaScipt to Speech Your Text / Events of Jack Pu
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | Fork me on GitHub
18 |
19 |
20 |
Use JavaScript to Speech Your Text
21 |
使用JavaScript 拼读你的文本
22 |
Demo:
23 |
35 |
36 |
37 |
38 | 更多说明:
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Use JavaScipt to Speech Your Text/ 使用JavaScript 拼读你的文本
2 |
3 | 在w3c草案中增加了对[Web Speech Api](https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html)的支持;主要作用在
4 | 两个非常重要的方面:
5 | + [语音识别](https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#speechreco-section) (将所说的转换成文本文字 / speech to text);
6 | + [语音合成](https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#tts-section) (将文本文字读出来 / text to speech);
7 |
8 | 而chrome在版本33发布后宣布对该特性的支持;今天重要介绍第二部分。[第一部分](https://github.com/JackPu/text-to-speech/blob/master/web-speech-regonition.md)
9 | 这里可以查看
10 | [演示地址](http://events.jackpu.com/text-to-speech/demo.html)
11 |
12 | 如果你只需要使用英文,推荐使用[vanspeak](https://github.com/Vanthink-UED/vanspeak)这个插件,兼容不支持发音的浏览器。
13 |
14 | ### 开始使用
15 |
16 | ``` js
17 | // 你可以直接打开你的控制台粘贴下面代码
18 | var words = new SpeechSynthesisUtterance('Hello captain');
19 | window.speechSynthesis.speak(words);
20 | ```
21 |
22 | 当然你还可以修改很多参数去调整你的发音:
23 |
24 | + `volume`:声音;
25 | + `rate`:发音速度;
26 | + `pitch`:音调;
27 | + `voice`:声音;
28 | + `language`:语言(en,zh,ja...[更多参考](http://www.mathguide.de/info/tools/languagecode.html))
29 |
30 | ``` js
31 | var msg = new SpeechSynthesisUtterance();
32 | var voices = window.speechSynthesis.getVoices();
33 | msg.voice = voices[10]; //
34 | msg.voiceURI = 'native';
35 | msg.volume = 1; // 0 to 1
36 | msg.rate = 1; // 0.1 to 10
37 | msg.pitch = 2; //0 to 2
38 | msg.text = 'I am Stark';
39 | msg.lang = 'en';
40 |
41 | msg.onend = function(e) {
42 | console.log('Finished in ' + event.elapsedTime + ' seconds.');
43 | };
44 |
45 | speechSynthesis.speak(msg);
46 | ```
47 |
48 | #### 设置发音
49 | 你可以通过下面函数获取可以使用的发音列表名称
50 | ``` js
51 | speechSynthesis.getVoices().forEach(function(voice) {
52 | console.log(voice.name, voice.default ? '(default)' :'');
53 | });
54 | ```
55 | 大概你可以获取下面的一个列表
56 | ``` js
57 | // 省略一部分结果
58 | Google Deutsch
59 | Google US English
60 | Google UK English Female
61 | Google UK English Male
62 | Google 日本語
63 | Google 普通话(中国大陆)
64 | Google 國語(臺灣)
65 | ```
66 | 接下来我们可以试验下改变发音名称
67 | ``` js
68 | var msg = new SpeechSynthesisUtterance('hey captain,sometime I just want to break you perfect teeth');
69 | msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Google US English'; })[0];
70 | speechSynthesis.speak(msg);
71 | ```
72 |
73 | 除了英文,我们还可以使用其他语言
74 | ``` JS
75 | // 使用日语
76 | var msg = new SpeechSynthesisUtterance('おはようございます');
77 | msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Google 日本語'; })[0];
78 | speechSynthesis.speak(msg);
79 | // or 使用中文
80 | var msg = new SpeechSynthesisUtterance('美国队长3');
81 | msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Google 普通话(中国大陆)'; })[0];
82 | speechSynthesis.speak(msg);
83 | ```
84 |
85 |
86 | ### 浏览器支持
87 |
88 | + Chrome 33+
89 | + iOS7 safari部分支持 (测试iOS8支持,iOS9不支持)
90 |
91 | 兼容性检测
92 |
93 | ```js
94 | if ('speechSynthesis' in window) {
95 | // Synthesis support. Make your web apps talk!
96 | }
97 |
98 | ```
99 | 如果对于不支持的浏览器,我们可以使用老的方法,即将需要发音的单词发送到服务端进行处理,返回一个音频,类似如下:
100 | ``` js
101 | // 使用来自谷歌翻译的音频
102 | var audio = new Audio();
103 | audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=' + encodeURI('hello captain');
104 | audio.play();
105 | ```
106 |
107 | ### 推荐框架
108 |
109 | 当然我们如果追求快速开发的话,我们现在依旧有成熟的框架来支持这个功能,让他实现更多浏览器的支持。
110 | + [ResponsiveVoice.JS](http://responsivevoice.org/) 是一款基于html5的跨平台的发音支持类库,支持超过56种语言和168种
111 | 声音,分为免费版和商业版。[Demo](http://events.jackpu.com/text-to-speech/)
112 |
113 | + [speak.js](https://github.com/kripken/speak.js/) 基于eSpeack改造而来的一款js单词拼读类库.
114 |
115 | + [meSpeak.js ](http://www.masswerk.at/mespeak/)是一个100%的客户端发音类库,支持chrome和safari,并且无需要任何html元素;
116 |
117 | + [say.js](https://github.com/marak/say.js/)一款基于node.js的发音扩展类库;
118 |
119 | + [vanspeak](https://github.com/Vanthink-UED/vanspeak) 一款国内开源的发音类库,支持原生和音频调用;
120 |
121 | 持续更新中...
122 |
123 | ### 参考
124 |
125 | [1] [Web apps that talk - Introduction to the Speech Synthesis API
126 | ](https://developers.google.com/web/updates/2014/01/Web-apps-that-talk-Introduction-to-the-Speech-Synthesis-API?hl=en)
127 |
128 | [2] [using-google-text-to-speech-in-javascript](http://stackoverflow.com/questions/15653145/using-google-text-to-speech-in-javascript)
129 |
130 | [3] [A More Awesome Web: Features You've Always Wanted - Google I/O 2013
131 | ](https://www.youtube.com/watch?time_continue=1695&v=N_wTBKMuJis)
132 |
133 | [4] [HTML Speech API Examples](https://lists.w3.org/Archives/Public/public-xg-htmlspeech/2011Nov/att-0008/web-speech-sample-code.html)
134 |
135 |
136 |
137 |
--------------------------------------------------------------------------------
/demo/css/cssgram.min.css:
--------------------------------------------------------------------------------
1 | .aden{-webkit-filter:hue-rotate(-20deg) contrast(.9) saturate(.85) brightness(1.2);filter:hue-rotate(-20deg) contrast(.9) saturate(.85) brightness(1.2)}.aden::after{background:-webkit-linear-gradient(left,rgba(66,10,14,.2),transparent);background:linear-gradient(to right,rgba(66,10,14,.2),transparent);mix-blend-mode:darken}.perpetua::after,.reyes::after{mix-blend-mode:soft-light;opacity:.5}.inkwell{-webkit-filter:sepia(.3) contrast(1.1) brightness(1.1) grayscale(1);filter:sepia(.3) contrast(1.1) brightness(1.1) grayscale(1)}.perpetua::after{background:-webkit-linear-gradient(top,#005b9a,#e6c13d);background:linear-gradient(to bottom,#005b9a,#e6c13d)}.reyes{-webkit-filter:sepia(.22) brightness(1.1) contrast(.85) saturate(.75);filter:sepia(.22) brightness(1.1) contrast(.85) saturate(.75)}.reyes::after{background:#efcdad}.gingham{-webkit-filter:brightness(1.05) hue-rotate(-10deg);filter:brightness(1.05) hue-rotate(-10deg)}.gingham::after{background:-webkit-linear-gradient(left,rgba(66,10,14,.2),transparent);background:linear-gradient(to right,rgba(66,10,14,.2),transparent);mix-blend-mode:darken}.toaster{-webkit-filter:contrast(1.5) brightness(.9);filter:contrast(1.5) brightness(.9)}.toaster::after{background:-webkit-radial-gradient(circle,#804e0f,#3b003b);background:radial-gradient(circle,#804e0f,#3b003b);mix-blend-mode:screen}.walden{-webkit-filter:brightness(1.1) hue-rotate(-10deg) sepia(.3) saturate(1.6);filter:brightness(1.1) hue-rotate(-10deg) sepia(.3) saturate(1.6)}.walden::after{background:#04c;mix-blend-mode:screen;opacity:.3}.hudson{-webkit-filter:brightness(1.2) contrast(.9) saturate(1.1);filter:brightness(1.2) contrast(.9) saturate(1.1)}.hudson::after{background:-webkit-radial-gradient(circle,#a6b1ff 50%,#342134);background:radial-gradient(circle,#a6b1ff 50%,#342134);mix-blend-mode:multiply;opacity:.5}.earlybird{-webkit-filter:contrast(.9) sepia(.2);filter:contrast(.9) sepia(.2)}.earlybird::after{background:-webkit-radial-gradient(circle,#d0ba8e 20%,#360309 85%,#1d0210 100%);background:radial-gradient(circle,#d0ba8e 20%,#360309 85%,#1d0210 100%);mix-blend-mode:overlay}.mayfair{-webkit-filter:contrast(1.1) saturate(1.1);filter:contrast(1.1) saturate(1.1)}.mayfair::after{background:-webkit-radial-gradient(40% 40%,circle,rgba(255,255,255,.8),rgba(255,200,200,.6),#111 60%);background:radial-gradient(circle at 40% 40%,rgba(255,255,255,.8),rgba(255,200,200,.6),#111 60%);mix-blend-mode:overlay;opacity:.4}.lofi{-webkit-filter:saturate(1.1) contrast(1.5);filter:saturate(1.1) contrast(1.5)}.lofi::after{background:-webkit-radial-gradient(circle,transparent 70%,#222 150%);background:radial-gradient(circle,transparent 70%,#222 150%);mix-blend-mode:multiply}._1977{-webkit-filter:contrast(1.1) brightness(1.1) saturate(1.3);filter:contrast(1.1) brightness(1.1) saturate(1.3)}._1977:after{background:rgba(243,106,188,.3);mix-blend-mode:screen}.brooklyn{-webkit-filter:contrast(.9) brightness(1.1);filter:contrast(.9) brightness(1.1)}.brooklyn::after{background:-webkit-radial-gradient(circle,rgba(168,223,193,.4) 70%,#c4b7c8);background:radial-gradient(circle,rgba(168,223,193,.4) 70%,#c4b7c8);mix-blend-mode:overlay}.xpro2{-webkit-filter:sepia(.3);filter:sepia(.3)}.xpro2::after{background:-webkit-radial-gradient(circle,#e6e7e0 40%,rgba(43,42,161,.6) 110%);background:radial-gradient(circle,#e6e7e0 40%,rgba(43,42,161,.6) 110%);mix-blend-mode:color-burn}.nashville{-webkit-filter:sepia(.2) contrast(1.2) brightness(1.05) saturate(1.2);filter:sepia(.2) contrast(1.2) brightness(1.05) saturate(1.2)}.nashville::after{background:rgba(0,70,150,.4);mix-blend-mode:lighten}.nashville::before{background:rgba(247,176,153,.56);mix-blend-mode:darken}.lark{-webkit-filter:contrast(.9);filter:contrast(.9)}.lark::after{background:rgba(242,242,242,.8);mix-blend-mode:darken}.lark::before{background:#22253f;mix-blend-mode:color-dodge}.moon{-webkit-filter:grayscale(1) contrast(1.1) brightness(1.1);filter:grayscale(1) contrast(1.1) brightness(1.1)}.moon::before{background:#a0a0a0;mix-blend-mode:soft-light}.moon::after{background:#383838;mix-blend-mode:lighten}.clarendon{-webkit-filter:contrast(1.2) saturate(1.35);filter:contrast(1.2) saturate(1.35)}.clarendon:before{background:rgba(127,187,227,.2);mix-blend-mode:overlay}.willow{-webkit-filter:grayscale(.5) contrast(.95) brightness(.9);filter:grayscale(.5) contrast(.95) brightness(.9)}.willow::before{background-color:radial-gradient(40%,circle,#d4a9af 55%,#000 150%);mix-blend-mode:overlay}.willow::after{background-color:#d8cdcb;mix-blend-mode:color}.rise{-webkit-filter:brightness(1.05) sepia(.2) contrast(.9) saturate(.9);filter:brightness(1.05) sepia(.2) contrast(.9) saturate(.9)}.rise::after{background:-webkit-radial-gradient(circle,rgba(232,197,152,.8),transparent 90%);background:radial-gradient(circle,rgba(232,197,152,.8),transparent 90%);mix-blend-mode:overlay;opacity:.6}.rise::before{background:-webkit-radial-gradient(circle,rgba(236,205,169,.15) 55%,rgba(50,30,7,.4));background:radial-gradient(circle,rgba(236,205,169,.15) 55%,rgba(50,30,7,.4));mix-blend-mode:multiply}._1977:after,._1977:before,.aden:after,.aden:before,.brooklyn:after,.brooklyn:before,.clarendon:after,.clarendon:before,.earlybird:after,.earlybird:before,.gingham:after,.gingham:before,.hudson:after,.hudson:before,.inkwell:after,.inkwell:before,.lark:after,.lark:before,.lofi:after,.lofi:before,.mayfair:after,.mayfair:before,.moon:after,.moon:before,.nashville:after,.nashville:before,.perpetua:after,.perpetua:before,.reyes:after,.reyes:before,.rise:after,.rise:before,.slumber:after,.slumber:before,.toaster:after,.toaster:before,.walden:after,.walden:before,.willow:after,.willow:before,.xpro2:after,.xpro2:before{content:'';display:block;height:100%;width:100%;top:0;left:0;position:absolute;pointer-events:none}._1977,.aden,.brooklyn,.clarendon,.earlybird,.gingham,.hudson,.inkwell,.lark,.lofi,.mayfair,.moon,.nashville,.perpetua,.reyes,.rise,.slumber,.toaster,.walden,.willow,.xpro2{position:relative}._1977 img,.aden img,.brooklyn img,.clarendon img,.earlybird img,.gingham img,.hudson img,.inkwell img,.lark img,.lofi img,.mayfair img,.moon img,.nashville img,.perpetua img,.reyes img,.rise img,.slumber img,.toaster img,.walden img,.willow img,.xpro2 img{width:100%;z-index:1}._1977:before,.aden:before,.brooklyn:before,.clarendon:before,.earlybird:before,.gingham:before,.hudson:before,.inkwell:before,.lark:before,.lofi:before,.mayfair:before,.moon:before,.nashville:before,.perpetua:before,.reyes:before,.rise:before,.slumber:before,.toaster:before,.walden:before,.willow:before,.xpro2:before{z-index:2}._1977:after,.aden:after,.brooklyn:after,.clarendon:after,.earlybird:after,.gingham:after,.hudson:after,.inkwell:after,.lark:after,.lofi:after,.mayfair:after,.moon:after,.nashville:after,.perpetua:after,.reyes:after,.rise:after,.slumber:after,.toaster:after,.walden:after,.willow:after,.xpro2:after{z-index:3}.slumber{-webkit-filter:saturate(.66) brightness(1.05);filter:saturate(.66) brightness(1.05)}.slumber::after{background:rgba(125,105,24,.5);mix-blend-mode:soft-light}.slumber::before{background:rgba(69,41,12,.4);mix-blend-mode:lighten}
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Use JavaScipt to Speech Your Text / Events of Jack Pu
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | Fork me on GitHub
18 |
19 |
20 |
Use JavaScript to Speech Your Text
21 |
使用JavaScript 拼读你的文本
22 |
Demo:
23 |
47 |
速度对比
48 |
49 |
50 | 样句
51 | 预加载时间
52 | 时间
53 | 操作
54 |
55 |
56 | Hello I am Stark
57 | 未测试(0s)
58 | 未测试(0s)
59 | 测试
60 |
61 |
62 | Hello-I-am-Stark
63 | 未测试(0s)
64 | 未测试(0s)
65 | 测试
66 |
67 |
68 | The cake in the photo had "Love Wins Fag" written on it in frosting — a message that would likely be upsetting to many
69 | 未测试(0s)
70 | 未测试(0s)
71 | 测试
72 |
73 |
74 | The-cake-in-the-photo-had-"Love Wins Fag"-written-on-it-in-frosting — a-message-that-would-likely-be-upsetting-to-many
75 | 未测试(0s)
76 | 未测试(0s)
77 | 测试
78 |
79 |
80 |
81 |
82 | 更多说明:
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
151 |
152 |
153 |
154 |
155 |
--------------------------------------------------------------------------------
/demo/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Use JavaScipt to Speech Your Text / Events of Jack Pu
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | Fork me on GitHub
21 |
22 |
23 |
24 |
25 |
53 |
54 |
55 |
56 |
Use JavaScript to Speech Your Text
57 |
使用JavaScript 拼读你的文本
58 |
在w3c草案中增加了对Web Speech Api 的支持;主要作用在 两个非常重要的方面:
59 |
60 |
61 | 语音识别 (将所说的转换成文本文字 / speech to text);
62 | 语音合成 (将文本文字读出来 / text to speech);
63 |
64 |
而chrome在版本33发布后宣布对该特性的支持;今天重要介绍第二部分。
65 |
开始使用
66 |
speack "hello captain"
67 |
68 | var words = new SpeechSynthesisUtterance('Hello captain' );
69 | window .speechSynthesis.speak(words);
70 |
71 |
Demo2:
72 |
当然你还可以修改很多参数去调整你的发音:
73 |
change accent "I am Stark"
74 |
75 | volume:声音;
76 | rate:发音速度;
77 | pitch:音调;
78 | voice:声音;
79 | language:语言(en,zh,ja…更多参考 )
80 |
81 |
var msg = new SpeechSynthesisUtterance();
82 | var voices = window .speechSynthesis.getVoices();
83 | msg.voice = voices[10 ];
84 | msg.voiceURI = 'native' ;
85 | msg.volume = 1 ;
86 | msg.rate = 1 ;
87 | msg.pitch = 2 ;
88 | msg.text = 'I am Stark' ;
89 | msg.lang = 'en' ;
90 |
91 | msg.onend = function (e ) {
92 | console .log('Finished in ' + event.elapsedTime + ' seconds.' );
93 | };
94 |
95 | speechSynthesis.speak(msg);
96 |
97 |
设置发音
98 |
你可以通过下面函数获取可以使用的发音列表名称
99 |
speechSynthesis.getVoices().forEach(function (voice ) {
100 | console .log(voice.name, voice.default ? '(default)' :'' );
101 | });
102 |
103 |
大概你可以获取下面的一个列表
104 |
105 | Google Deutsch
106 | Google US English
107 | Google UK English Female
108 | Google UK English Male
109 | Google 日本語
110 | Google 普通话(中国大陆)
111 | Google 國語(臺灣)
112 |
113 |
接下来我们可以试验下改变发音名称
114 |
change voice
115 |
var msg = new SpeechSynthesisUtterance('hey captain,sometime I just want to break you perfect teeth' );
116 | msg.voice = speechSynthesis.getVoices().filter(function (voice ) { return voice.name == 'Google US English' ; })[0 ];
117 | speechSynthesis.speak(msg);
118 |
119 |
除了英文,我们还可以使用其他语言
120 |
121 | speak 'おはようございます'
122 | 说出“美国队长”
123 |
124 |
125 |
126 | var msg = new SpeechSynthesisUtterance('おはようございます' );
127 | msg.voice = speechSynthesis.getVoices().filter(function (voice ) { return voice.name == 'Google 日本語' ; })[0 ];
128 | speechSynthesis.speak(msg);
129 |
130 | var msg = new SpeechSynthesisUtterance('美国队长3' );
131 | msg.voice = speechSynthesis.getVoices().filter(function (voice ) { return voice.name == 'Google 普通话(中国大陆)' ; })[0 ];
132 | speechSynthesis.speak(msg);
133 |
134 |
浏览器支持
135 |
136 | Chrome 33+
137 | iOS7 safari部分支持 (测试iOS8支持,iOS9不支持)
138 |
139 |
兼容性检测
140 |
if ('speechSynthesis' in window ) {
141 |
142 | }
143 |
144 |
145 |
如果对于不支持的浏览器,我们可以使用老的方法,即将需要发音的单词发送到服务端进行处理,返回一个音频,类似如下:
146 |
147 | var audio = new Audio();
148 | audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=' + encodeURI ('hello captain' );
149 | audio.play();
150 |
151 |
推荐框架
152 |
当然我们如果追求快速开发的话,我们现在依旧有成熟的框架来支持这个功能,让他实现更多浏览器的支持。
153 |
154 |
155 | ResponsiveVoice.JS 是一款基于html5的跨平台的发音支持类库,支持超过56种语言和168种 声音,分为免费版和商业版。
156 | Demo
157 |
158 |
159 |
160 | speak.js 基于eSpeack改造而来的一款js单词拼读类库.
161 |
162 |
163 | meSpeak.js 是一个100%的客户端发音类库,支持chrome和safari,并且无需要任何html元素;
164 |
165 |
166 | say.js 一款基于node.js的发音扩展类库。
167 |
168 |
169 |
持续更新中…
170 |
参考
171 |
[1] Web apps that talk - Introduction to the Speech Synthesis API
172 |
173 |
174 |
[2] using-google-text-to-speech-in-javascript
175 |
176 |
[3] A More Awesome Web: Features You’ve Always Wanted - Google I/O 2013
177 |
178 |
179 |
[4] HTML Speech API Examples
180 |
181 |
182 |
183 | 更多说明:
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
199 |
200 |
201 |
242 |
243 |
244 |
245 |
--------------------------------------------------------------------------------