Skip to content

Instantly share code, notes, and snippets.

@c93614
Forked from xjohjrdy/xiaoxiao_tts.py
Created December 12, 2021 09:23
Show Gist options
  • Save c93614/0f59ae08841d1219325810d3d89c9ef2 to your computer and use it in GitHub Desktop.
Save c93614/0f59ae08841d1219325810d3d89c9ef2 to your computer and use it in GitHub Desktop.
返回的音频没有文件头,所以我随便加的一个文件头,但显示的音频时长有问题。如果播放器不能正常播放,可以使用Chrome播放。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from ws4py.client.threadedclient import WebSocketClient
import binascii
class WSClient(WebSocketClient):
def __init__(self, url, text, filename):
self.fp = open(filename, 'wb')
self.fp.write(binascii.unhexlify('524946460000000057415645666d74201000000001000200803e000000fa0000040010006461746100000000'))
self.text = text
super(WSClient, self).__init__(url)
def opened(self):
self.send('Content-Type:application/json; charset=utf-8\r\n\r\nPath:speech.config\r\n\r\n{"context":{"synthesis":{"audio":{"metadataoptions":{"sentenceBoundaryEnabled":"false","wordBoundaryEnabled":"true"},"outputFormat":"audio-24khz-48kbitrate-mono-mp3"}}}}\r\n')
self.send("X-RequestId:fe83fbefb15c7739fe674d9f3e81d38f\r\nContent-Type:application/ssml+xml\r\nPath:ssml\r\n\r\n<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='Microsoft Server Speech Text to Speech Voice (zh-CN, XiaoxiaoNeural)'><prosody pitch='+0Hz' rate ='+0%' volume='+0%'>"+self.text+"</prosody></voice></speak>\r\n")
def received_message(self, m):
if 'turn.end' in m.data:
self.close()
self.fp.close()
elif 'Path:audio\r\n' in m.data:
self.fp.write(m.data.split('Path:audio\r\n')[1])
else:
# print(m)
pass
if __name__ == '__main__':
url = 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1?TrustedClientToken=6A5AA1D4EAFF4E9FB37E23D68491D6F4'
text = '浙江温州,浙江温州,最大皮革厂,江南皮革厂倒闭了!老板黄鹤吃喝嫖赌,欠下了3.5个亿,带着他的小姨子跑了。我们没有办法,拿着钱包抵工资。原价都是三百多、二百多、一百多的钱包,通通二十块,通通二十块!黄鹤你不是人,我们辛辛苦苦给你干了大半年,你不发工资,你还我血汗钱,还我血汗钱!'
filename = '/tmp/test.wav'
ws = WSClient(url, text, filename)
ws.connect()
ws.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment