0

I have searched and searched to no avail. There is no clear documentation for this API at least from wht ive found. I want to make a custom object that can hold the properties for this function and be able to run it

console.log(window.speechSynthesis.getVoices());
var voices = window.speechSynthesis.getVoices();
var kline = new Object();

kline.speakAloud = function(message) {
 Speech = new SpeechSynthesisUtterance(message);
 Speech.voice = voices[2];
 Speech.gender = "male";
 Speech.voiceURI = "Google UK English Male";
 Speech.volume = 1; // 0 to 1
 Speech.rate = 1; // 0.1 to 10
 Speech.pitch = 2; //0 to 2
 Speech.lang = 'en-GB';
 window.speechSynthesis.speak(Speech);  
};
kline.speakText = function(message) {
 document.getElementById("Output").innerHTML = message;
};
 
 
 //Arrays for Algorithmic Input Processing 
 
 var Greetings = ["hello", "hey", "hi", "sup"];
 var Functions = ["say", "search", "math", "", "", "", "", ""];


function Run() {
 message = document.getElementById("Input").value.toLowerCase();
 console.log("Successfully ran function" + '\n' + "Input:" + document.getElementById("Input").value + '\n' + "Proccesed input:" + message);
 
 //If statement 
 if (message === ("hello")) { // greating
  kline.speakAloud("testing");
  kline.speakText("testing");
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
}

If you need to html i can post this as well, its basically a input box and a button to call Run(). The code works but I can not get it to be a male voice. I would like to keep in contained in this same object with a way to call it using methods, if anybody has a way to make it sound male or knows the documentation of this please post this as i hope other people searching for documentation will find your lovely answer.

Im also running linux 15 and chrome version 48. I would like if possible the ability to run this on other browsers. lets take baby steps though.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
tecksup
  • 51
  • 1
  • 13

1 Answers1

1

The most complete documentation is in the Web Speech API Specification.

As described in this answer, the voices array is only loaded after the window.speechSynthesis.onvoiceschanged event has fired. If you move your initialization code to this event then the voices will be available.

var voices = []
var kline = new Object();

window.speechSynthesis.onvoiceschanged = function() {
    voices = window.speechSynthesis.getVoices();
    console.log(voices);
    
    kline.speakAloud = function(message) {
    Speech = new SpeechSynthesisUtterance(message);
    Speech.voice = voices[2];
    Speech.voiceURI = "Google UK English Male";
    Speech.volume = 1; // 0 to 1
    Speech.rate = 1; // 0.1 to 10
    Speech.pitch = 0; //0 to 2
    Speech.lang = 'en-GB';
    window.speechSynthesis.speak(Speech);  
  };
  kline.speakText = function(message) {
    document.getElementById("Output").innerHTML = message;
  };
  
};
 
 //Arrays for Algorithmic Input Processing 
 
 var Greetings = ["hello", "hey", "hi", "sup"];
 var Functions = ["say", "search", "math", "", "", "", "", ""];


function Run() {
 message = document.getElementById("Input").value.toLowerCase();
 console.log("Successfully ran function" + '\n' + "Input:" + document.getElementById("Input").value + '\n' + "Proccesed input:" + message);
 
 //If statement 
 if (message === ("hello")) { // greating
  kline.speakAloud("testing");
  kline.speakText("testing");
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
 else if (message === ("X")) { //
  kline.speakAloud("");
  kline.speakText(""); 
 }
}
<input type="text" id="Input" value="hello"/>
<input type="text" id="Output"/>
<button id="run" onclick="Run()">
Run
</button>
Community
  • 1
  • 1
Sarah Elan
  • 2,465
  • 1
  • 23
  • 45
  • I put this into my program, as u made it much more organized but its still not speaking in a male voice, let me know if it is for you cause I've heard there are problems with certain OS. – tecksup Feb 12 '16 at 17:04
  • Make sure that voices[2] is the one you want. On my system Google UK English Male is voices[4]. – Sarah Elan Feb 12 '16 at 17:20
  • what OS do you have(windows, Linux, Mac, etc.) cause i changed it to 4 and its still a female voice. no different from what i had when i used my original code. – tecksup Feb 12 '16 at 17:28
  • Use the developer tools to look at the voices array in the console. It is different in each OS and browser. – Sarah Elan Feb 12 '16 at 17:54