python调用阿里云长语音合成api

阿里云长语音合成

直接上代码,参考着自己调通就行了

长语音合成需要在阿里云付费开通
long_tts=True,选择是否开启长语音合成


import io
import time
import threading
import sys

import nls
import requests
import time


URL = "wss://nls-gateway-cn-shenzhen.aliyuncs.com/ws/v1"


TOKEN = token  # 参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY = "you appkey"  # 获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist


# TEXT = "今天天气很好"

# 以下代码会根据上述TEXT文本反复进行语音合成
class DemoTts:
    def __init__(self, tid):
        # self.__th = threading.Thread(target=self.__test_run)
        self.__id = tid

    def start(self, text,output_wav,voice):
        self.__text = text
        # self.__f = io.BytesIO()
        self.__f = open(output_wav, "wb")
        self.__test_run(voice)

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

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

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

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

    def test_on_completed(self, message, *args):
        print("on_completed:args=>{} message=>{}".format(args, message))
        message.json()
        print(message)
    def __test_run(self,voice):
        print("thread:{} start..".format(self.__id))
        tts = nls.NlsSpeechSynthesizer(url=URL,
                                       token=TOKEN,
                                       appkey=APPKEY,
                                       on_metainfo=self.test_on_metainfo,
                                       long_tts=True,
                                       on_data=self.test_on_data,
                                       on_completed=self.test_on_completed,
                                       on_error=self.test_on_error,
                                       on_close=self.test_on_close,
                                       callback_args=[self.__id])
        print("{}: session start".format(self.__id))
        ex = {"enable_ptts": True}
        r = tts.start(self.__text, voice="zhixiaobai", aformat="wav", ex=ex)
        print("{}: tts 完成:{}\n".format(self.__id, r))
        # return self.__f




def tts_input_text(text,voice,output_wav):
    TEXT = text
    output_wav += "/out.wav"
    nls.enableTrace(True)
    t = DemoTts("name")
    t.start(TEXT,output_wav,voice)


def tts_upload_file(file_path, voice, output_wav):
    with open(file_path, "r", encoding="utf-8") as file:
        # 读取文件内容
        text = file.read()
    output_wav += "/out.wav"
    TEXT = text
    nls.enableTrace(True)
    t = DemoTts("name")
    t.start(TEXT,output_wav,voice)



def run(num, file_path, voice, output_wav):
    num = input("!!!  输入1或2\n1-输入文本\n2-上传文件") 
    if num == '1':
        text = input() 
    elif num == '2':
        file_path = input("请输入文件完整路径") 
        with open(file_path, "r", encoding="utf-8") as file:
            # 读取文件内容
            text = file.read()

    voice = input("输入声音voice")
    
    TEXT = text
    output_wav = input("填写输入文件的保存路径")

    nls.enableTrace(True)
    t = DemoTts("name")
    t.start(TEXT,output_wav,voice)




  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsh_abc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值