עריכה - 9/5/21: הטוקן הפסיק לעבוד.
קובץ חדש כאן:
https://gist.github.com/MusiCode1/2db11a3e429e41e02bdf98febd239256
באמצעות הקוד הבא, ניתן להמיר טקסט לדיבור, ללא תשלום,
באמצעות הטוקן של מייקרוסופט אדג'.
https://gist.io/@MusiCode1/951019413c14bb8b9b0c91fd66359445
קובץ חדש כאן:
https://gist.github.com/MusiCode1/2db11a3e429e41e02bdf98febd239256
באמצעות הקוד הבא, ניתן להמיר טקסט לדיבור, ללא תשלום,
באמצעות הטוקן של מייקרוסופט אדג'.
import requests
import time
from xml.etree import ElementTree
import os
def get_file(path):
with open(path, 'r', encoding='utf-8') as f:
r = f.read()
return r
class TextToSpeech(object):
def __init__(self, subscription_key):
self.subscription_key = subscription_key
self.tts = input("What would you like to convert to speech: ")
# self.tts = get_file(os.getcwd() + '\\' + "test.txt")
self.timestr = time.strftime("%Y%m%d-%H%M")
self.access_token = None
self.base_url = 'https://westus.tts.speech.microsoft.com/'
def get_token(self):
path = "sts/v1.0/issueToken"
url = self.base_url + path
headers = {
'Ocp-Apim-Subscription-Key': self.subscription_key
}
response = requests.post(url, headers=headers)
self.access_token = str(response.text)
def save_audio(self):
path = 'cognitiveservices/v1'
url = self.base_url + path
headers = {
'Authorization': 'Bearer ' + self.access_token,
'Content-Type': 'application/ssml+xml',
# 'X-Microsoft-OutputFormat': 'riff-8khz-8bit-mono-mulaw',
'X-Microsoft-OutputFormat': 'audio-24khz-160kbitrate-mono-mp3',
'User-Agent': 'YOUR_RESOURCE_NAME'
}
# <speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US"><voice name="he-IL-HilaNeural"><prosody rate="0%" pitch="0%">You can replace this text with any text you wish. You can either write in this text box or paste your own text here.
xml_body = ElementTree.Element('speak', version='1.0')
xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'he-IL')
xml_body.set('xmlns', 'https://www.w3.org/2001/10/synthesis')
parent = xml_body
voice = ElementTree.SubElement(parent, 'voice')
voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'he-IL')
voice.set('{http://www.w3.org/XML/1998/namespace}gender', 'Male')
# name = 'HilaNeural'
# name = 'Asaf'
name = 'AvriNeural'
voice.set(
'name', 'Microsoft Server Speech Text to Speech Voice (he-IL, ' +
name + ')'
)
# voice.text = self.tts
parent = voice
# prosody = ElementTree.SubElement(voice, 'prosody')
# prosody.set('rate', '0')
# prosody.set('pitch', 'x-high')
#parent = prosody
element1 = ElementTree.SubElement(parent, 'p')
element1.text = self.tts
body = ElementTree.tostring(xml_body)
time_start = time.time()
# response = requests.post(url, headers=headers, data=body, verify='C:\\netfree-ca-anywhere.crt')
response = requests.post(url, headers=headers, data=body)
print(time.time() - time_start)
filename = 'sample-' + self.timestr + '.mp3' # '.alaw'
if response.status_code == 200:
with open(filename, 'wb') as audio:
audio.write(response.content)
print("\nStatus code: " + str(response.status_code) +
"\nYour TTS is ready for playback.\n")
os.startfile(os.getcwd() + '\\' + filename, 'open')
else:
print("\nStatus code: " + str(response.status_code) +
"\nSomething went wrong. Check your subscription key and headers.\n")
print(response.content)
if __name__ == "__main__":
subscription_key = "e6a6934efe57408fa2f3ce41c278b129"
app = TextToSpeech(subscription_key)
app.get_token()
app.save_audio()