微软语音合成(tts)服务申请和调用

这篇具有很好参考价值的文章主要介绍了微软语音合成(tts)服务申请和调用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、申请账户:

https://azure.microsoft.com/zh-cn/free/

微软语音合成(tts)服务申请和调用
这里有个视频教程,根据此完成申请流程:
https://www.bilibili.com/video/BV15a4y1W7re?vd_source=bf07f28d37849885d215dc3aea189eba
申请完成后,就可以到这里申请资源:
https://portal.azure.com/#home

点击资源组,里面就有部署好的服务了
微软语音合成(tts)服务申请和调用
微软语音合成(tts)服务申请和调用
点击这里,可以获取 subscription_key,另外还有个就是位置service_region (上图就是east asia),这两个后面会用到。

2、调用服务

在完成微软azure服务账号申请后,就可以进行调用了。代码:

'''
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://westus.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):
        base_url = 'https://westus.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', 'en-US-Guy24kRUS') # 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")

    def get_voices_list(self):
        base_url = 'https://westus.tts.speech.microsoft.com/'
        path = 'cognitiveservices/voices/list'
        constructed_url = base_url + path
        headers = {
            'Authorization': 'Bearer ' + self.access_token,
        }
        response = requests.get(constructed_url, headers=headers)
        if response.status_code == 200:
            print("\nAvailable voices: \n" + response.text)
        else:
            print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")

if __name__ == "__main__":
    app = TextToSpeech(subscription_key)
    app.get_token()
    app.save_audio()
    # Get a list of voices https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech#get-a-list-of-voices
    # app.get_voices_list()

参考文档:
https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/
https://github.com/Azure-Samples/Cognitive-Speech-TTS/blob/28681c8292c95aebb36d3696b8822b4cd17c3c45/Samples-Http/OLD/Python/TTSSample.py文章来源地址https://www.toymoban.com/news/detail-503833.html

到了这里,关于微软语音合成(tts)服务申请和调用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity C# 之 Azure 微软SSML语音合成TTS流式获取音频数据以及表情嘴型 Animation 的简单整理

    目录 Unity C# 之 Azure 微软SSML语音合成TTS流式获取音频数据以及表情嘴型 Animation 的简单整理 一、简单介绍 二、实现原理 三、注意事项 四、实现步骤 五、关键代码 Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。 本节介绍,这里在使用

    2024年02月12日
    浏览(49)
  • 微软(TTS)文本转语音服务API实现

    此博客实现与java实现微软文本转语音(TTS)经验总结_java tts_${简简单单}的博客-CSDN博客之上,首先感谢博客源码的提供,本人在上面添加了一些详细的注释,方便大家跟好的理解和使用,毕竟我已经用原文调试了一下午才调通,一些细节的问题给大家标注出来,免得浪费大家

    2024年02月07日
    浏览(43)
  • Unity调用微软SpeechLib.Dll的C#类实现语音合成功能

    using System.Collections; using System.Collections.Generic; using UnityEngine; using SpeechLib; public class Speech : MonoBehaviour { // Start is called before the first frame update void Start() { } 将Interop.SpeechLib.dll文件导入Unity,然后把上面的脚本挂载到游戏对象上就能测试语音合成的效果了。 经测试 调用v.Speak这个方

    2024年02月13日
    浏览(45)
  • [Unity+OpenAI TTS] 集成openAI官方提供的语音合成服务,构建海王暖男数字人

            最近openAI官方发布了很多新功能,其中就包括了最新发布的TTS语音合成服务的api接口。说到这个语音合成接口,大家可能会比较陌生,但是说到chatgpt官方应用上的聊天机器人,那个台湾腔的海王暖男的声音,可能就有印象了吧。那么从官方文档中,可以发现,openA

    2024年02月04日
    浏览(37)
  • 文字转语音 - 搭建微软tts整合web服务提供api接口(免费)

    微软tts是业界公认文字转语音效果最佳 本文使用docker搭建微软tts服务并提供api接口对外提供服务 对接官方免费在线体验接口,搭建后可免费进行调用使用,不保证永久稳定可用 url :http://127.0.0.1:5003/tts method :POST 参数 类型 描述 text string 语音文字内容 voiceName string 发音人(

    2024年02月11日
    浏览(37)
  • 如何注册微软Azure并获取语音合成服务?

    按步骤,一步步来。 使用条件,以下可选: 1、有信用卡 2、有学生邮箱、学校邮箱。 步骤: 1、打开地址,去注册。 信用卡注册: 文本转语音 – 真实 AI 语音生成器 | Microsoft Azure 学生邮箱: 面向学生的 Azure - 免费帐户额度 | Microsoft Azure 2、注册后去后台,打开语音服务。 后

    2024年02月03日
    浏览(40)
  • 使用微软Azure的tts文本转语音服务出现java.lang.UnsatisfiedLinkError

    最近,在使用微软tts文本转语音的speech服务时,项目正常整合了微软的依赖,服务也正常启动。但是只要调用微软文本转语音服务api时,就会出现如下报错。 该方法是一个native方法,我以为是依赖中的dll文件没有加载到,结果检查不是。 最后分析:native依赖的是底层实现的

    2024年02月14日
    浏览(48)
  • 离线语音交互技术路线之语音合成(TTS)篇

      在ChatGPT大行其道之际,我心血来潮想要研究研究如何实现离线语音交互,把它和ChatGPT相结合,自己尝试实现个语音助手玩玩。本篇文章主要先从整体上分析了离线语音交互的技术实现路线,以及每个环节可能用到的参考技术,然后详细阐述了其中一个环节:语音合成(

    2024年02月09日
    浏览(39)
  • 语音合成工具Coqui TTS安装及体验

    先介绍两种免费的语音合成工具 官网 http://balabolka.site/balabolka.htm 是一种基于微软Speech API (SAPI)的免费语音合成工具,只是简单的发音合成,效果比较生硬 官网 https://coqui.ai/ 是基于深度学习的语音合成软件,效果较好 Windows下安装Coqui TTS 安装环境 要求python运行环境:https://

    2024年02月15日
    浏览(40)
  • Python使用PaddleSpeech实现语音识别(ASR)、语音合成(TTS)

    目录 安装 语音识别 补全标点 语音合成 参考 PaddleSpeech是百度飞桨开发的语音工具 注意,PaddleSpeech不支持过高版本的Python,因为在高版本的Python中,飞桨不再提供paddle.fluid API。这里面我用的是Python3.7 需要通过3个pip命令安装PaddleSpeech: 在使用的时候,urllib3库可能会报错,因

    2024年04月25日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包