├── README.md
└── TextToSpeech.js
/README.md:
--------------------------------------------------------------------------------
1 | # Speech-Synthesis-API-wrapper
2 |
3 |
4 | #How to use?
5 | ```
6 | npm install speaktome
7 | ```
8 | than
9 | ```
10 |
11 | ```
12 |
13 |
14 | CDN
15 | ```
16 |
17 | ```
18 |
19 |
20 |
21 | #API
22 | ```
23 | /*
24 | * @param {string} text
25 | * @param {number} voice 0~19
26 | * @param {number} volume 0~1
27 | * @param {number} rate 0.1~2
28 | * @param {number} pitch 0~2
29 | * @param {boolean} showElapsedTime
30 | */
31 |
32 |
33 |
34 | TextToSpeech(text,voice,volume,rate,pitch,showElapsedTime);
35 | ```
36 |
--------------------------------------------------------------------------------
/TextToSpeech.js:
--------------------------------------------------------------------------------
1 | /*
2 | * @param {string} text
3 | * @param {number} voice 0~19
4 | * @param {number} volume 0~1
5 | * @param {number} rate 0.1~2
6 | * @param {number} pitch 0~2
7 | * @param {boolean} showElapsedTime
8 |
9 | */
10 |
11 |
12 |
13 | if ('speechSynthesis' in window) {
14 |
15 | var TextToSpeech = function(text,voice,volume,rate,pitch,showElapsedTime){
16 |
17 | var msg = new SpeechSynthesisUtterance();
18 | var voices = window.speechSynthesis.getVoices();
19 |
20 | if(typeof text=="string"){
21 | msg.text = text;
22 | }else{
23 | console.error("text must be a string at first argument")
24 | return
25 | };
26 | if (typeof voice !== 'undefined'){
27 | if(typeof voice=="number"){
28 | if(0<=voice&&voice<=19){
29 | msg.voice = voices[voice];
30 | }else{
31 | console.error("voice number must between 0~19 at second argument")
32 | }
33 | }else{
34 | console.error("voice must be number at second argument")
35 | }
36 | }
37 | if (typeof volume !== 'undefined'){
38 | if(typeof volume=="number"){
39 |
40 | if(0<=volume&&volume<=1){
41 | msg.volume = volume;
42 | }else{
43 | console.error("volume must greater than zero and smaller than 1 at third argument")
44 | return
45 | }
46 | }else{
47 | console.error("volume must be a number at third argument")
48 | return
49 | };
50 | }
51 | if (typeof rate !== 'undefined'){
52 | if(typeof rate=="number"){
53 | if(0.1<=rate&&rate<=2){
54 | msg.rate = rate;
55 | }else{
56 | console.error("rate must greater than 0.1 and smaller than 2")
57 | return
58 | }
59 | }else{
60 | console.error("rate must be a number at fourth argument")
61 | return
62 | };
63 | }
64 | if (typeof pitch !== 'undefined'){
65 | if(typeof pitch=="number"){
66 | if(0.1<=pitch&&pitch<=2){
67 | msg.pitch = pitch;
68 | }else{
69 | console.error("pitch must greater than 0 and smaller than 2 at fifth argument")
70 | return
71 | }
72 | }else{
73 | console.error("pitch must be a number at fifth argument")
74 | return
75 | };
76 | }
77 | if (typeof showElapsedTime !== 'undefined'){
78 | if(typeof showElapsedTime=="boolean"){
79 | if(showElapsedTime==true){
80 | msg.onend = function(e) {
81 | console.log('Finished in ' + event.elapsedTime + ' seconds.');
82 | };
83 | }
84 | }else{
85 | console.error("showElapsedTime must be true of false at sixth argument")
86 | }
87 |
88 | }
89 |
90 | speechSynthesis.speak(msg);
91 | }
92 |
93 |
94 |
95 | }
96 |
97 |
--------------------------------------------------------------------------------