Skip to content

Instantly share code, notes, and snippets.

@UsernameFull
Last active December 11, 2021 11:39
Show Gist options
  • Save UsernameFull/d6ceb987913c3c1a08e83487207f8e70 to your computer and use it in GitHub Desktop.
Save UsernameFull/d6ceb987913c3c1a08e83487207f8e70 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: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.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):
#print(m)
if b'turn.end' in m.data:
self.close()
self.fp.close()
elif b'Path:audio\r\n' in m.data:
song_bytes = m.data.split(b'Path:audio\r\n')[1]
self.fp.write(song_bytes)
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 = './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