使用来自 python 的 bing tts api 时,特殊字符会被截断

Special characters get truncated when using bing tts api from python

我修改了在 https://github.com/Microsoft/Cognitive-Speech-TTS/tree/master/Samples-Http/Python 中找到的 python 示例,以在西班牙语更改中合成语音

"<speak version='1.0' xml:lang='en-us'><voice xml:lang='en-us' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)'>

"<speak version='1.0' xml:lang='es-ES'><voice xml:lang='es-ES' xml:gender='Male' name='Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)'>

但在合成过程中,像“ñ”这样的非 ASCII 字符会在某些步骤被截断,因此它们不会出现在最终的音频文件中。

我已通过打印请求字符串检查这不是 python 问题,字符显示正确。

如果您查看 HTTP 请求,您会发现 http.client 库没有正确编码字符串。最简单的解决方法是自己编码:

ssml = "<speak version='1.0' xml:lang='es-ES'>...</speak>"
body = ssml.encode('utf8')