前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python文本转语音(微软xiaoxiao语音)

python文本转语音(微软xiaoxiao语音)

作者头像
wo.
发布2021-06-15 14:20:42
8.7K1
发布2021-06-15 14:20:42
举报
文章被收录于专栏:了不得的专栏

前言

python文本转语音(微软xiaoxiao语音)

1. 准备工作

话不多说,马上开始,首先登录 Azure portal,

1.1 选择 “认知服务”,添加一个新的 Speech 订阅 ,名称随便起
1.2 位置选择 东南亚
1.3 定价层选择 F0

因为xiaoxiao的语音属于神经网络语音 所以选择地区时要选择 神经网络可用区域 才能使用微软xiaoxiao语音

F0是免费版,每月有一定的免费额度可以使用

1.3 Speech 部署完成后

点击左侧列表中的 “所有资源”连接,进入资源管理面板

1.4 选择资源,查看密钥

在资源面板点击刚才创建好的 MySpeechService,进入详情后点击 “密钥和终结点”,可以看到已经生成好的密钥,等一下调用 Speech 服务的时候需要用到。

python代码

代码语言:javascript
复制
'''
After you've set your subscription key, run this application from your working
directory with this command: python TTSSample.py
'''
import os, requests, time
from xml.etree import ElementTree

# This code is required for Python 2.7
try: input = raw_input
except NameError: pass

'''
If you prefer, you can hardcode your subscription key as a string and remove
the provided conditional statement. However, we do recommend using environment
variables to secure your subscription keys. The environment variable is
set to SPEECH_SERVICE_KEY in our sample.
For example:
subscription_key = "Your-Key-Goes-Here"
'''

# if 'SPEECH_SERVICE_KEY' in os.environ:
#     subscription_key = os.environ['SPEECH_SERVICE_KEY']
# else:
#     print('Environment variable for your subscription key is not set.')
#     exit()


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.timestr = time.strftime("%Y%m%d-%H%M")
        self.access_token = None

    '''
    The TTS endpoint requires an access token. This method exchanges your
    subscription key for an access token that is valid for ten minutes.
    '''
    def get_token(self):
        fetch_token_url = "https://southeastasia.api.cognitive.microsoft.com/sts/v1.0/issueToken" # 终结点
        headers = {
            'Ocp-Apim-Subscription-Key': self.subscription_key
        }
        response = requests.post(fetch_token_url, headers=headers)
        self.access_token = str(response.text)

    def save_audio(self):
        ShortName = 'zh-CN-XiaoxiaoNeural' # 每月 5000个 字符免费
        # ShortName = 'zh-CN-Yaoyao-Apollo' # 每月 500 万个字符免费

        base_url = 'https://southeastasia.tts.speech.microsoft.com/'
        path = 'cognitiveservices/v1'
        constructed_url = base_url + path
        headers = {
            'Authorization': 'Bearer ' + self.access_token,
            'Content-Type': 'application/ssml+xml',
            'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
            'User-Agent': 'YOUR_RESOURCE_NAME'
        }
        xml_body = ElementTree.Element('speak', version='1.0')
        xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-us')
        voice = ElementTree.SubElement(xml_body, 'voice')
        voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-US')
        voice.set('name', ShortName) # Short name for 'Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)'
        voice.text = self.tts
        body = ElementTree.tostring(xml_body)

        response = requests.post(constructed_url, headers=headers, data=body)
        '''
        If a success response is returned, then the binary audio is written
        to file in your working directory. It is prefaced by sample and
        includes the date.
        '''
        if response.status_code == 200:
            with open('sample-' + self.timestr + '.wav', 'wb') as audio:
                audio.write(response.content)
                print("\nStatus code: " + str(response.status_code) + "\nYour TTS is ready for playback.\n")
        else:
            print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")
            print("Reason: " + str(response.reason) + "\n")

if __name__ == "__main__":
    subscription_key = '8e3efb9cc44f4906b5c8921515cf4f3e'
    app = TextToSpeech(subscription_key)
    app.get_token()
    app.save_audio()

官方示例

https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/quickstart-python-text-to-speech

定价

free.png
free.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-02-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 1. 准备工作
    • 1.1 选择 “认知服务”,添加一个新的 Speech 订阅 ,名称随便起
      • 1.2 位置选择 东南亚
        • 1.3 定价层选择 F0
          • 1.3 Speech 部署完成后
            • 1.4 选择资源,查看密钥
            • python代码
            • 官方示例
            • 定价
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档