阿里云语音合成示例

1.到官网下载SDK并安装

2.测试代码如下:

import nls
import nls.speech_synthesizer

URL = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "your_token"  # 参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY = "your_appkey"  # 获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist


class MyTTS:

    def on_metainfo(self, message, *args):
        print("on_metainfo message=>{}".format(message))

    def on_error(self, message, *args):
        print("on_error args=>{}".format(args))

    def on_close(self, *args):
        print("on_close: args=>{}".format(args))
        try:
            self.__file.close()
        except Exception as e:
            print("close failed:", e)

    def on_data(self, data, *args):
        try:
            self.__file.write(data)
        except Exception as e:
            print("write data failed:", e)

    def on_completed(self, message, *args):
        print("on_completed:args=>{} message=>{}".format(args, message))

    def run(self, text, voice, file):
        self.__file = open(file, "wb")
        self.__text = text
        self.__voice = voice
        tts = nls.NlsSpeechSynthesizer(
            url=URL,
            token=TOKEN,
            appkey=APPKEY,
            on_metainfo=self.on_metainfo,
            on_data=self.on_data,
            on_completed=self.on_completed,
            on_error=self.on_error,
            on_close=self.on_close
        )
        result = tts.start(self.__text, voice=self.__voice, aformat="mp3")
        print("tts done with result:{}".format(result))


voice_list = ["zhimiao_emo", "zhimi_emo", "zhiyan_emo", "xiaogang", "sicheng", "aicheng", "xiaoyun", "ruoxi", "aida",
              "ninger", "ruilin", "siyue", "aiya", "aixia", "aiyu", "sitong", "xiaobei", "aiwei", "aibao"]

synthesizer = MyTTS()
# 每个声音都测试一下
for voice in voice_list:
    synthesizer.run(text="这是测试用句", voice=voice, file=f"{voice}.mp3")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值