2 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
15 var utteranceIndex = 0;
18 text = document.getElementById('srctext');
19 ttsStatus = document.getElementById('ttsStatus');
20 ttsStatusBox = document.getElementById('ttsStatusBox');
21 lang = document.getElementById('lang');
22 enqueue = document.getElementById('enqueue');
23 voices = document.getElementById('voices');
24 voiceInfo = document.getElementById('voiceInfo');
26 chrome.tts.getVoices(function(va) {
28 for (var i = 0; i < voiceArray.length; i++) {
29 var opt = document.createElement('option');
30 opt.setAttribute('value', voiceArray[i].voiceName);
31 opt.innerText = voiceArray[i].voiceName;
32 voices.appendChild(opt);
35 voices.addEventListener('change', function() {
36 var i = voices.selectedIndex - 1;
38 voiceInfo.innerText = JSON.stringify(voiceArray[i], null, 2);
40 voiceInfo.innerText = '';
45 function speak(str, options, highlightText) {
50 options.enqueue = Boolean(enqueue.value);
52 var voiceIndex = voices.selectedIndex - 1;
53 if (voiceIndex >= 0) {
54 options.voiceName = voiceArray[voiceIndex].voiceName;
56 var rateValue = Number(rate.value);
57 if (rateValue >= 0.1 && rateValue <= 10.0) {
58 options.rate = rateValue;
60 var pitchValue = Number(pitch.value);
61 if (pitchValue >= 0.0 && pitchValue <= 2.0) {
62 options.pitch = pitchValue;
64 var volumeValue = Number(volume.value);
65 if (volumeValue >= 0.0 && volumeValue <= 1.0) {
66 options.volume = volumeValue;
69 console.log(utteranceIndex + ': ' + JSON.stringify(options));
70 options.onEvent = function(event) {
71 console.log(utteranceIndex + ': ' + JSON.stringify(event));
73 text.setSelectionRange(0, event.charIndex);
75 if (event.type == 'end' ||
76 event.type == 'interrupted' ||
77 event.type == 'cancelled' ||
78 event.type == 'error') {
79 chrome.tts.isSpeaking(function(isSpeaking) {
81 ttsStatus.innerHTML = 'Idle';
82 ttsStatusBox.style.background = '#fff';
88 str, options, function() {
89 if (chrome.runtime.lastError) {
90 console.log('TTS Error: ' + chrome.runtime.lastError.message);
93 ttsStatus.innerHTML = 'Busy';
94 ttsStatusBox.style.background = '#ffc';
101 function speakUserText() {
104 options.lang = lang.value;
106 speak(text.value, options, true);
109 document.addEventListener('DOMContentLoaded', load);