1/**
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.
5 */
6
7var text;
8var ttsStatus;
9var ttsStatusBox;
10var lang;
11var enqueue;
12var voices;
13var voiceInfo;
14var voiceArray;
15var utteranceIndex = 0;
16
17function load() {
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');
25
26  chrome.tts.getVoices(function(va) {
27    voiceArray = 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);
33    }
34  });
35  voices.addEventListener('change', function() {
36    var i = voices.selectedIndex - 1;
37    if (i >= 0) {
38      voiceInfo.innerText = JSON.stringify(voiceArray[i], null, 2);
39    } else {
40      voiceInfo.innerText = '';
41    }
42  }, false);
43}
44
45function speak(str, options, highlightText) {
46  if (!options) {
47    options = {};
48  }
49  if (enqueue.value) {
50    options.enqueue = Boolean(enqueue.value);
51  }
52  var voiceIndex = voices.selectedIndex - 1;
53  if (voiceIndex >= 0) {
54    options.voiceName = voiceArray[voiceIndex].voiceName;
55  }
56  var rateValue = Number(rate.value);
57  if (rateValue >= 0.1 && rateValue <= 10.0) {
58    options.rate = rateValue;
59  }
60  var pitchValue = Number(pitch.value);
61  if (pitchValue >= 0.0 && pitchValue <= 2.0) {
62    options.pitch = pitchValue;
63  }
64  var volumeValue = Number(volume.value);
65  if (volumeValue >= 0.0 && volumeValue <= 1.0) {
66    options.volume = volumeValue;
67  }
68  utteranceIndex++;
69  console.log(utteranceIndex + ': ' + JSON.stringify(options));
70  options.onEvent = function(event) {
71    console.log(utteranceIndex + ': ' + JSON.stringify(event));
72    if (highlightText) {
73      text.setSelectionRange(0, event.charIndex);
74    }
75    if (event.type == 'end' ||
76        event.type == 'interrupted' ||
77        event.type == 'cancelled' ||
78        event.type == 'error') {
79      chrome.tts.isSpeaking(function(isSpeaking) {
80        if (!isSpeaking) {
81          ttsStatus.innerHTML = 'Idle';
82          ttsStatusBox.style.background = '#fff';
83        }
84      });
85    }
86  };
87  chrome.tts.speak(
88      str, options, function() {
89    if (chrome.runtime.lastError) {
90      console.log('TTS Error: ' + chrome.runtime.lastError.message);
91    }
92  });
93  ttsStatus.innerHTML = 'Busy';
94  ttsStatusBox.style.background = '#ffc';
95}
96
97function stop() {
98  chrome.tts.stop();
99}
100
101function speakUserText() {
102  var options = {};
103  if (lang.value) {
104    options.lang = lang.value;
105  }
106  speak(text.value, options, true);
107}
108
109document.addEventListener('DOMContentLoaded', load);
110