Как изменить стандартный мужской голос по умолчанию на женский или другой - PullRequest
1 голос
/ 20 июня 2019

const btn = document.querySelector('.talk');
const content = document.querySelector('.content');
const greetings = [
				'If you are good im good to ?', 
				'Im doin alright', 
				'Im tired ?'
];
const weather = [
				'Ask the weatherman!', 
				'I recommend checking your phone or the news ' 
				
];
const name = [
	'My name is techwaala', 
	'its techwaala, because I love to code!'
];
const hello = [
	'Why hello! How are you doing today?', 
	'Hey there How are you?'
]
const hru = [
	'thats great!', 
	'Im so sorry to hear that', 
	'Feel better soon!'
]
const SpeechRecognition = 
	window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();

recognition.onstart = function() {
	console.log('voice is activated speak into the mic');
};
recognition.onresult = function(event) {
	const current = event.resultIndex;
	
	const transcript = event.results[current][0].transcript;
	content.textContent = transcript;
	readOutLoud(transcript);
}

btn.addEventListener('click', () => {
	recognition.start();
});

function readOutLoud(message) {
	
	const speech = new SpeechSynthesisUtterance();
	speech.text = 'I dont know what you said';
	if(message.includes('how are you')) {
		 const finalText = 
		 greetings[Math.floor(Math.random() * greetings.length)];
		 speech.text = finalText;
		
	} 
	if(['hey', 'hi', 'hello', 'hi there', 'hey there', 'hi techwala', 'hey techwala','hello techwala']
	.some(word => message.includes(word))) {
    const finalText = hello[Math.floor(Math.random() * hello.length)];
    speech.text = finalText;
	
}
	if(['whats your name', 'your name']
	.some(word => message.includes(word))) {
    const finalText = name[Math.floor(Math.random() * name.length)];
    speech.text = finalText;
}
if(['how\'s the weather', 'what\'s the weather like', 'is it sunny', 'is it raining', 'is it cloudy', 'is it snowing']
	.some(word => message.includes(word))) {
    const finalText = weather[Math.floor(Math.random() * weather.length)];
    speech.text = finalText;
}
	
	speech.volume = 1;
	speech.rate = 1;
	speech.pitch = 1;
	window.speechSynthesis.speak(speech);
		
	} 

Я работаю над веб-страницей, которая говорит, когда вы что-то говорите.По умолчанию он говорит на мужском языке США, но что, если я хотел бы изменить акцент, скажем, на британский женский или французский мужской?Я пытался использовать отзывчивый voice.org, но это не работает для меня.Как бы я это сделал?Кроме того, я не вставлял HTML-код в этот фрагмент, потому что думал, что JavaScript будет единственной необходимой вещью, на которую нужно посмотреть.

1 Ответ

2 голосов
/ 21 июня 2019

Использование speechSynthesis

В документах есть хороший пример, но, по сути, что-то вроде этого, если вы хотите жестко закодировать голос:

var synth = window.speechSynthesis;

// get voices
var voices = synth.getVoices();

// when voices have finished loading
synth.onvoiceschanged = function() {

  var sayThis = new SpeechSynthesisUtterance('Hi how are you?');
  sayThis.onend = function(event) {
    console.log('SpeechSynthesisUtterance.onend');
  }

  sayThis.onerror = function(event) {
    console.error('SpeechSynthesisUtterance.onerror');
  }

  var selectedVoice = 'Google UK English Female';
  for (var i = 0; i < voices.length; i++) {
    if (voices[i].name === selectedVoice) {
      sayThis.voice = voices[i];
      break;
    }
  }
  sayThis.pitch = 1;
  sayThis.rate = 1;

  synth.speak(sayThis);
}
...