【AI实践】阿里云方言文本转语音TTS

最近要做一些普通话和方言demo

找一个免费工具

免费在线文字转语音工具 | edge-tts 在线体验 (bingal.com)

还有一些方言在阿里云上找了下,基于官方demo改了一下

阿里云语音合成接口说明_智能语音交互(ISI)-阿里云帮助中心 (aliyun.com)

如何下载安装、使用语音合成PythonSDK及代码示例_智能语音交互(ISI)-阿里云帮助中心 (aliyun.com)

python 复制代码
# -*- coding: UTF-8 -*-

import time
import threading
import sys

import nls

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



TEXT=['这是第一条测试语料,请欣赏','这是第二条测试语料,请欣赏']

#以下代码会根据上述TEXT文本反复进行语音合成
class TestTts:
    def __init__(self, tid, test_file):
        self.__th = threading.Thread(target=self.__test_run)
        self.__id = tid
        self.__test_file = test_file
   
    def start(self, text):
        self.__text = text
        self.__f = open(self.__test_file, "wb")
        self.__th.start()
    
    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))
        try:
            self.__f.close()
        except Exception as e:
            print("close file failed since:", e)

    def test_on_data(self, data, *args):
        try:
            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))


    def __test_run(self):
      	print("thread:{} start..".format(self.__id))
      	tts = nls.NlsSpeechSynthesizer(url=URL,
      	      	      	      	       token=TOKEN,
      	      	      	      	       appkey=APPKEY,
      	      	      	      	       on_metainfo=self.test_on_metainfo,
      	      	      	      	       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))
      	r = tts.start(self.__text, voice="cuijie", aformat="mp3",speech_rate=-250)
      	print("{}: tts done with result:{}".format(self.__id, r))

def multiruntest(num):
    for i in range(0, num):
        name = "thread" + str(i)
        t = TestTts(name, "tests/output_tts"+str(i)+".mp3")
        t.start(TEXT[i])
    
nls.enableTrace(True)
multiruntest(2)
相关推荐
Amo Xiang10 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm320 分钟前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
B站计算机毕业设计超人32 分钟前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~36 分钟前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
waterHBO3 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate6 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼6 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio8 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal9 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘