Commits

Ivan Vučica  committed e169fa3 Draft

Hacks for Google Translate TTS.

  • Participants
  • Parent commits 05f3fda

Comments (0)

Files changed (2)

File console_tts_engine.js

 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+// Hacks for Google Translate TTS: (c) 2013 Ivan Vučica.
+
 var timeoutId;
 var ttsId = -1;
 var ttsWindow;
 var curOptions;
 
 function areNewOptions(options) {
-  var properties = ['voiceName', 'lang', 'gender', 'rate', 'pitch', 'volume'];
+  var properties = ['voiceName', 'lang', 'volume']; //, 'gender', 'rate', 'pitch', 'volume'];
 
   for (var i = 0; i < properties.length; ++i) {
     if (options[properties[i]] != curOptions[properties[i]]) {
 function logOptions() {
   getTtsElement("voiceName").innerHTML = curOptions.voiceName;
   getTtsElement("lang").innerHTML = curOptions.lang;
-  getTtsElement("gender").innerHTML = curOptions.gender;
-  getTtsElement("rate").innerHTML = curOptions.rate;
-  getTtsElement("pitch").innerHTML = curOptions.pitch;
+  //getTtsElement("gender").innerHTML = curOptions.gender;
+  //getTtsElement("rate").innerHTML = curOptions.rate;
+  //getTtsElement("pitch").innerHTML = curOptions.pitch;
   getTtsElement("volume").innerHTML = curOptions.volume;
 }
 
 var speakListener = function(utterance, options, sendTtsEvent) {
   clearTimeout(timeoutId);
 
-  sendTtsEvent({'type': 'start', 'charIndex': 0});
-
+  console.log(options);
+  
+  var sentences = [];
+  var newSentence = "";
+  var prevCharacter;
+  for(var charId in utterance)
+  {
+	var character = utterance[charId];
+	var prevCharCode = prevCharacter ? prevCharacter.charCodeAt(0) : 0;
+	newSentence += character;
+	if((character == ".") || // TODO: support ... and 1.54; e.g.: && prevCharCode && (prevCharCode < 48 || prevCharCode > 57) && charId < utterance.length && (utterance.charCodeAt(charId+1) < 48 || utterance.charCodeAt(charId+1) > 57)) || 
+	   (character == "?") || // TODO: support ?? and ?!
+	   (character == "!") // TODO: support !! and !?
+	   )
+	 {
+	   sentences.push(newSentence);
+	   newSentence = "";
+	 }
+	 prevCharacter = character;
+  }
+  if(newSentence.length) sentences.push(newSentence);
+  
+  var lang = options ? options.lang : undefined;
+  if(!lang) lang = "en";
+  var volume = options ? options.volume : undefined;
+  if(typeof volume == "undefined") volume = 1.0;
+  
+  //var sentences = utterance.split(". ");
+  var previousAudio = false;
+  var charIndex = 0;
+  for(var sentenceId in sentences)
+  {
+	var sentence = sentences[sentenceId];
+	//sentence += ". ";
+	
+	var audio = document.createElement("audio");
+	document.body.appendChild(audio);
+	audio.src = "http://translate.google.com/translate_tts?ie=UTF-8&prev=input&tl=" + lang + "&q=" + encodeURIComponent(sentence);
+	audio.volume = volume;
+	audio.load();
+	
+	if(!previousAudio)
+	{
+		audio.gttsData = {sentence: sentence, startIndex: 0};
+		audio.addEventListener("play", 
+		function()
+		{
+			sendTtsEvent({'type': 'start', 'charIndex': this.gttsData.startIndex});
+		});
+	
+		audio.play();
+	}
+	else
+	{
+		var sentenceToPass = sentence + "";
+		var indexToPass = charIndex;
+		var audioToPass = audio;
+		if(!previousAudio.gttsData) previousAudio.gttsData = {};
+		previousAudio.gttsData.nextAudio = audio;
+		previousAudio.gttsData.index = indexToPass;
+		
+		if(!audio.gttsData) audio.gttsData = {};
+		audio.gttsData.sentence = sentence;
+		
+		previousAudio.addEventListener("ended", 
+		function()
+		{
+			sendTtsEvent({'type': 'sentence', 'charIndex': this.gttsData.index});
+			this.gttsData.nextAudio.play();
+		});
+	}
+	
+	charIndex += sentence.length;
+	previousAudio = audio;
+  }
+  previousAudio.addEventListener("ended", 
+  function()
+  {
+	sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
+  });
+  
+  return;
   if (ttsId == -1) {
     // Create a new window that overlaps the bottom 40% of the current window
     chrome.windows.getCurrent(function(curWindow) {
 
 chrome.ttsEngine.onSpeak.addListener(speakListener);
 chrome.ttsEngine.onStop.addListener(stopListener);
-chrome.windows.onRemoved.addListener(removedListener);
+//chrome.windows.onRemoved.addListener(removedListener);

File manifest.json

 {
-  "name": "Console TTS Engine",
+  "name": "TTS Engine using Google Translate",
   "manifest_version": 2,
-  "version": "2.1",
-  "description": "A \"silent\" TTS engine that prints text to a small window rather than synthesizing speech.",
-  "permissions": ["ttsEngine", "tabs"],
+  "version": "1.0",
+  "description": "Uses Google Translate to provide a voice.",
+  "homepage_url": "http://ivan.vucica.net/chrome-googletranslatetts/",
+  "permissions": ["ttsEngine"], //, "tabs"],
   "background": {
     "persistent": false,
     "scripts": ["console_tts_engine.js"]
   "tts_engine": {
     "voices": [
       {
-        "voice_name": "Console",
-        "event_types": ["start", "word", "sentence", "end"]
+        "voice_name": "Google Translate",
+        "event_types": ["start", "sentence", "end"]
       }
     ]
   }