开通用户可享有所有支持试用的产品免费试用3个月
import os
import nls.speech_synthesizer
import random
URL = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN="yourToken"
APPKEY="yourAppkey"
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()
print(f"File {self.__file.name} closed successfully.")
except Exception as e:
print("close failed:", e)
def on_data(self, data, *args):
try:
self.__file.write(data)
print(f"Data written to {self.__file.name}.")
except Exception as e:
print("write data failed:", e)
def on_completed(self, message, *args):
print("on_completed:args=>{} message=>{}".format(args, message))
try:
self.__file.close()
print(f"File {self.__file.name} closed successfully.")
except Exception as e:
print("close failed:", e)
def run(self, text, voice, file):
try:
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
)
tts.start(self.__text, voice=self.__voice, aformat="mp3")
print(f"Speech synthesis for voice '{self.__voice}' initiated.")
except Exception as e:
print(f"Failed to start TTS for voice '{voice}': {e}")
voice_list = [
# Mixed Chinese and English
"abin", "zhixiaobai", "zhixiaoxia", "zhixiaomei", "zhigui", "zhishuo", "aixia", "cally",
"zhifeng_emo", "zhibing_emo", "zhimiao_emo", "zhimi_emo", "zhiyan_emo", "zhibei_emo",
"zhitian_emo", "xiaoyun", "xiaogang", "ruoxi", "siqi", "sijia", "sicheng", "aiqi",
"aijia", "aicheng", "aida", "ninger", "ruilin", "siyue", "aiya", "aimei", "aiyu",
"aiyue", "aijing", "xiaomei", "ailun", "maoxiaomei", "mashu", "xiaoxian",
"aifei", "yaqun", "qiaowei", "dahu", "guijie", "stella", "stanley", "kenny",
"rosa", "chuangirl", "lydia", "aishuo",
# Pure Chinese
"aina", "yina", "sijing", "sitong", "xiaobei", "aitong", "aiwei", "aibao",
"zhimao", "yuer", "qingqing", "cuijie", "xiaoze", "aikan", "zhiyuan", "zhiya",
"zhiyue", "zhida", "zhistella", "jielidou", "laotie", "laomei",
# Pure English
"harry", "abby", "andy", "eric", "emily", "luna", "luca", "wendy", "annie",
"william", "olivia", "beth", "cindy", "donna", "eva", "brian", "becca", "ava",
# Japanese
"tomoka", "tomoya",
# Indonesian
"indah",
# Cantonese, English, Chinese mixed
"taozi", "jiajia", "shanshan",
# Malay
"farah",
# Filipino
"tala",
# Vietnamese
"tien",
# Korean
"Kyong",
# Russian
"masha",
# Spanish
"camila",
# Italian
"perla",
# Hong Kong
"kelly",
# French
"clara", "hanna",
# German
"waan",
# Thai
"betty"
]
synthesizer = MyTTS()
# Specify save directory
save_directory = r"F:\tts"
os.makedirs(save_directory, exist_ok=True)
# Read text from txt file and generate audio
txt_file = r"F:\tts\1.txt"
with open(txt_file, 'r', encoding='utf-8') as file:
for line_number, text in enumerate(file, 1):
if len(text.strip()) == 0:
continue
random_voice = random.choice(voice_list)
file_path = os.path.join(save_directory, f"fake_ali{line_number}_{random_voice}.mp3")
synthesizer.run(text=text.strip(), voice=random_voice, file=file_path)