You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import base64
import requests
import wave
def save_audio_to_file ( output_file , audio_data ) :
with wave . open ( output_file , ' wb ' ) as wave_file :
wave_file . setnchannels ( 1 ) # 设置为单声道
wave_file . setsampwidth ( 2 ) # 设置样本宽度( 以字节为单位, 2表示16位)
wave_file . setframerate ( 44100 ) # 设置帧速率
wave_file . writeframes ( audio_data )
def text_to_speech ( text , output_file ) :
# 请求参数
request_data = {
" text " : text ,
" spk_id " : 0 ,
# 语速
" speed " : 0.87 ,
" volume " : 1.0 ,
" sample_rate " : 0 ,
" save_path " : output_file
}
# 发送POST请求
response = requests . post ( " http://192.168.10.138:8090/paddlespeech/tts " , json = request_data )
# 解析返回的JSON
response_json = response . json ( )
if response_json [ " success " ] :
# 获取返回的音频base64编码
base64_audio = response_json [ " result " ] [ " audio " ]
# 将音频保存到文件
with open ( output_file , ' wb ' ) as wave_file :
wave_file . write ( base64 . b64decode ( base64_audio ) )
else :
print ( " TTS request failed: " , response_json [ " message " ] [ " description " ] )
# 要发送的语句列表
statements = [
" 《庆余年》根据猫腻的小说改编,讲述了少年林殊在乱世中成长的故事。他凭借智慧和勇气,卷入复杂的政治斗争,结识志同道合的盟友和强大的对手,经历挑战与考验,最终成为能左右局势的重要人物。剧中不仅有紧张刺激的情节,还探讨了权力、正义与人性的复杂关系,深受观众喜爱。 "
]
# 保存语音的文件名列表
output_files = [ f " E: \\ SadTalker \\ temp \\ wav \\ tts_result_ { i } .wav " for i in range ( len ( statements ) ) ]
# 发送每个语句的请求并保存音频文件
for i in range ( len ( statements ) ) :
text_to_speech ( statements [ i ] , output_files [ i ] )