You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

文本转语音指定Male性别却输出Female语音,求代码调整方案

解决Bing Text-to-Speech指定Male语音却输出Female的问题

嘿,我一眼就揪出问题所在啦!你代码里的<voice>标签虽然设置了xml:gender="Male",但**name属性指定的却是一个女性语音**——Microsoft Server Speech Text to Speech Voice (en-US, ZiraRus)是微软官方提供的英文(美国)女声,不管你怎么设置xml:gender,语音服务都会优先采用name指定的具体语音实例,这就是为什么始终输出女声的原因。

快速修复方案

name属性替换成对应的英文(美国)男声ID即可,比如最常用的:
Microsoft Server Speech Text to Speech Voice (en-US, Guy24kRUS)

修改后的SSML Payload代码片段如下:

<speak version="1.0" xml:lang="en-US">
  <voice xml:lang="en-US" xml:gender="Male" name="Microsoft Server Speech Text to Speech Voice (en-US, Guy24kRUS)">
    ${text}
  </voice>
</speak>

补充说明

  • xml:gender属性其实是可选配置,只要name指定的语音本身是男性,服务会自动匹配性别,保留该属性也不会有冲突。
  • 如果需要其他区域或风格的男性语音,可以选择对应兼容你API版本的语音ID(比如神经语音的en-US-JasonNeural,不过要确认你的服务端点支持)。

以下是完整的修改后函数代码:

textToSpeech("This is a test to check the conversion of text to speech");
function textToSpeech(text: string) {
 const requestOptions: request.CoreOptions = {
 headers: {
 "Ocp-Apim-Subscription-Key": config.speech.bingSpeech.key1,
 }
 };
 request.post(
 `${config.speech.bingSpeech.authEndPoint}/issueToken`,
 requestOptions,
 (err, response, body) => {
 const accessToken = response.body;
 const payLoad = `
 <speak version="1.0" xml:lang="en-US">
 <voice xml:lang="en-US" xml:gender="Male" name="Microsoft Server Speech Text to Speech Voice (en-US, Guy24kRUS)">
 ${text}
 </voice>
 </speak>
 `;
 const requestOptions: request.CoreOptions = {
 headers: {
 "X-Microsoft-OutputFormat": "audio-16khz-128kbitrate-mono-mp3",
 "Content-Type": "application/ssml+xml",
 "Host": "speech.platform.bing.com",
 "Content-Length": payLoad.length,
 "Authorization": "Bearer " + accessToken,
 "User-Agent": "NodeJS"
 },
 body: payLoad
 };
 request.post(
 config.speech.bingSpeech.synthesizeUrl,
 requestOptions
 ).pipe(fs.createWriteStream(__dirname + "/output.mp3"));
 }
 )
}

内容的提问来源于stack exchange,提问作者Sibeesh Venu

火山引擎 最新活动