forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttsdemo.js
128 lines (118 loc) · 3.51 KB
/
ttsdemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
var text;
var ttsStatus;
var ttsStatusBox;
var lang;
var enqueue;
var voices;
var voiceInfo;
var voiceArray;
var utteranceIndex = 0;
function load() {
text = document.getElementById('srctext');
ttsStatus = document.getElementById('ttsStatus');
ttsStatusBox = document.getElementById('ttsStatusBox');
lang = document.getElementById('lang');
enqueue = document.getElementById('enqueue');
voices = document.getElementById('voices');
voiceInfo = document.getElementById('voiceInfo');
document.getElementById('speakUserTextButton')
.addEventListener('click', speakUserText);
document.getElementById('stopButton')
.addEventListener('click', stop);
const speechOptions = [
{ id: 'speakAlpha', text: 'Alpha' },
{ id: 'speakBravo', text: 'Bravo' },
{ id: 'speakCharlie', text: 'Charlie' },
{ id: 'speakDelta', text: 'Delta' },
{ id: 'speakEcho', text: 'Echo' },
{ id: 'speakFoxtrot', text: 'Foxtrot' },
];
for (const option of speechOptions) {
document.getElementById(option.id)
.addEventListener('focus', function(){ speak(option.text); });
}
chrome.tts.getVoices(function(va) {
voiceArray = va;
for (var i = 0; i < voiceArray.length; i++) {
var opt = document.createElement('option');
opt.setAttribute('value', voiceArray[i].voiceName);
opt.innerText = voiceArray[i].voiceName;
voices.appendChild(opt);
}
});
voices.addEventListener('change', function() {
var i = voices.selectedIndex - 1;
if (i >= 0) {
voiceInfo.innerText = JSON.stringify(voiceArray[i], null, 2);
} else {
voiceInfo.innerText = '';
}
}, false);
}
function speak(str, options, highlightText) {
if (!options) {
options = {};
}
if (enqueue.value) {
options.enqueue = Boolean(enqueue.value);
}
var voiceIndex = voices.selectedIndex - 1;
if (voiceIndex >= 0) {
options.voiceName = voiceArray[voiceIndex].voiceName;
}
var rateValue = Number(rate.value);
if (rateValue >= 0.1 && rateValue <= 10.0) {
options.rate = rateValue;
}
var pitchValue = Number(pitch.value);
if (pitchValue >= 0.0 && pitchValue <= 2.0) {
options.pitch = pitchValue;
}
var volumeValue = Number(volume.value);
if (volumeValue >= 0.0 && volumeValue <= 1.0) {
options.volume = volumeValue;
}
utteranceIndex++;
console.log(utteranceIndex + ': ' + JSON.stringify(options));
options.onEvent = function(event) {
console.log(utteranceIndex + ': ' + JSON.stringify(event));
if (highlightText) {
text.setSelectionRange(0, event.charIndex);
}
if (event.type == 'end' ||
event.type == 'interrupted' ||
event.type == 'cancelled' ||
event.type == 'error') {
chrome.tts.isSpeaking(function(isSpeaking) {
if (!isSpeaking) {
ttsStatus.innerHTML = 'Idle';
ttsStatusBox.style.background = '#fff';
}
});
}
};
chrome.tts.speak(
str, options, function() {
if (chrome.runtime.lastError) {
console.log('TTS Error: ' + chrome.runtime.lastError.message);
}
});
ttsStatus.innerHTML = 'Busy';
ttsStatusBox.style.background = '#ffc';
}
function stop() {
chrome.tts.stop();
}
function speakUserText() {
var options = {};
if (lang.value) {
options.lang = lang.value;
}
speak(text.value, options, true);
}
document.addEventListener('DOMContentLoaded', load);