问题描述
我正在将 SpeechRecognition 与我的麦克风一起使用,并通过 SpeechSynthesis 将数据转发给我.
I am using SpeechRecognition with my microphone and having data relayed back to me with speechSynthesis.
我在页面加载时将声音设为女声,并希望能够通过说男声"切换为男声,然后转播我现在是男人".后来我也希望能够做相反的事情 - 当它设置为男声时,说女声"然后它切换回来.
I made the the voice be a female voice on page load and want to be able to switch to a male voice by saying "male voice", which will then relay "I am now a man". I also later want to be able to do the opposite - when it set to a male voice, say "female voice" and it switches back.
我目前可以这样做,但男声只会说一次,因为声音不会被保存,只会作为参数传递.因此,接下来说的话将回到女声:
I can currently do that but the male voice will only be spoken once as the voice is not being saved, only passed as an argument. Therefore, then next thing spoken will be back in the female voice:
let voices = [];
window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices();
};
function loadVoices(message, voice) {
const msg = new SpeechSynthesisUtterance();
msg.voice = voice || voices[48]; // female voice
msg.text = message;
speechSynthesis.speak(msg);
};
// asking for voice change here
if (transcript.includes('male voice')) {
let message = ('I am now a man');
let voice = voices[50]; // male voice
loadVoices(message, voice);
}
我尝试使用一个全局变量,使 msg.voice
指向一个全局变量,但这不起作用,而且语音恢复为默认值(电子语音):
I tried having a global variable such that msg.voice
points to a global variable, but this does not work, plus the voice reverts back to default (electronic voice):
let voiceGender = voices[48];
function loadVoices(message) {
const msg = new SpeechSynthesisUtterance();
msg.voice = voiceGender // now a variable pointing to another.
msg.text = message;
speechSynthesis.speak(msg);
};
if (transcript.includes('male voice')) {
let message = ('I am now a man');
let voiceGender = voices[50]; // changing the global variable
loadVoices(message);
}
如果我在 loadVoices()
内声明 voiceGender
,那么我不能从另一个函数内的 if
更改它.
If I declare voiceGender
inside of loadVoices()
, then I can't change it from the if
which is inside another function.
如何设置 Javascript 结构以实现此目的?
How can I set the Javascript structure so that I can achieve this?
推荐答案
我通过在 loadVoices 函数中添加一个函数和一个带有条件的布尔值来解决它:
I solved it by adding a function and a boolean with conditionals in the loadVoices function such that:
// on pageload the voice is set to a female voice
let femaleVoice = true;
function loadVoices(message) {
const msg = new SpeechSynthesisUtterance();
// checks the boolean
if (femaleVoice) {
msg.voice = voices[48];
} else {
msg.voice = voices[50];
}
msg.text = message;
speechSynthesis.speak(msg);
};
// changes the boolean / changes the gender of the SpeechSynthesisUtterance voice
function changeVoice() {
if (femaleVoice) {
femaleVoice = false;
} else {
femaleVoice = true;
}
}
if (transcript.includes('male voice') || transcript.includes('female voice') ) {
// calls the function to change the boolean.
changeVoice();
let message = ('I now have a different voice');
loadVoices(message);
}
它确实添加了比最初想要的多一点但绝对有效.
It does add a tiny bit more lines than originally wanted but definitely works.
这篇关于使用 SpeechRecognition 更改语音合成语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!