Skip to content

Commit

Permalink
edge-tts合入统一class调用(待测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Sep 28, 2023
1 parent 9a5dd56 commit 823b5f8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 33 deletions.
1 change: 0 additions & 1 deletion data/badwords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,6 @@
办驾驶
张荣坤
鄧朴方
手机
車侖
小便
毛煞逼
Expand Down
42 changes: 24 additions & 18 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,11 @@ async def voice_change_and_put_to_queue(message, voice_tmp_path):
}

# 调用接口合成语音
data_json = self.my_tts.vits_fast_api(data)
voice_tmp_path = self.my_tts.vits_fast_api(data)
# logging.info(data_json)

if data_json is None:
return

if "data" in data_json:
voice_tmp_path = data_json["data"][1]["name"]
else:
logging.error(f"vits-fast合成失败,请检查配置是否正确,接口返回:{data_json}")
if voice_tmp_path is None:
logging.error(f"vits-fast合成失败,请检查配置是否正确")
return

logging.info(f"vits-fast合成成功,合成内容:【{message['content']}】,输出到={voice_tmp_path}")
Expand All @@ -449,12 +444,26 @@ async def voice_change_and_put_to_queue(message, voice_tmp_path):
return
elif message["tts_type"] == "edge-tts":
try:
voice_tmp_path = './out/' + self.common.get_bj_time(4) + '.mp3'
# 过滤" '字符
message["content"] = message["content"].replace('"', '').replace("'", '')
# 使用 Edge TTS 生成回复消息的语音文件
communicate = edge_tts.Communicate(text=message["content"], voice=message["data"]["voice"], rate=message["data"]["rate"], volume=message["data"]["volume"])
await communicate.save(voice_tmp_path)
data = {
"content": message["content"],
"voice": message["data"]["voice"],
"rate": message["data"]["rate"],
"volume": message["volume"]
}

# 调用接口合成语音
voice_tmp_path = await self.my_tts.edge_tts_api(data)

if voice_tmp_path is None:
logging.error(f"edge-tts合成失败,请检查配置是否正确 或 网络问题")
return

# voice_tmp_path = './out/' + self.common.get_bj_time(4) + '.mp3'
# # 过滤" '字符
# message["content"] = message["content"].replace('"', '').replace("'", '')
# # 使用 Edge TTS 生成回复消息的语音文件
# communicate = edge_tts.Communicate(text=message["content"], voice=message["data"]["voice"], rate=message["data"]["rate"], volume=message["data"]["volume"])
# await communicate.save(voice_tmp_path)

logging.info(f"edge-tts合成成功,合成内容:【{message['content']}】,输出到={voice_tmp_path}")

Expand Down Expand Up @@ -957,10 +966,7 @@ async def voice_change_and_put_to_queue(voice_tmp_path):
}

# 调用接口合成语音
data_json = self.my_tts.vits_fast_api(data)
# logging.info(data_json)

voice_tmp_path = data_json["data"][1]["name"]
voice_tmp_path = self.my_tts.vits_fast_api(data)
logging.info(f"vits-fast合成成功,合成内容:【{content}】,输出到={voice_tmp_path}")

await voice_change_and_put_to_queue(voice_tmp_path)
Expand Down
77 changes: 65 additions & 12 deletions utils/audio_handle/my_tts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json, logging
import json, logging, os
import aiohttp, requests
from urllib.parse import urlencode
from gradio_client import Client
import traceback
import edge_tts

from utils.common import Common
from utils.logger import Configure_logger
Expand All @@ -16,6 +18,16 @@ def __init__(self, config_path):
file_path = "./log/log-" + self.common.get_bj_time(1) + ".txt"
Configure_logger(file_path)

try:
self.audio_out_path = self.config.get("play_audio", "out_path")

if not os.path.isabs(self.audio_out_path):
if not self.audio_out_path.startswith('./'):
self.audio_out_path = './' + self.audio_out_path
except Exception as e:
logging.error(traceback.format_exc())
logging.error("请检查播放音频的音频输出路径配置!!!这将影响程序使用!")


# 请求vits的api
async def vits_api(self, data):
Expand Down Expand Up @@ -83,7 +95,8 @@ async def vits_api(self, data):
async with session.get(url) as response:
response = await response.read()
# print(response)
voice_tmp_path = './out/vits_' + self.common.get_bj_time(4) + '.wav'
file_name = 'vits_' + self.common.get_bj_time(4) + '.wav'
voice_tmp_path = self.get_new_audio_path(file_name)
with open(voice_tmp_path, 'wb') as f:
f.write(response)

Expand Down Expand Up @@ -123,18 +136,35 @@ def vits_fast_api(self, data):

result = response.content
ret = json.loads(result)
return ret
# async with aiohttp.ClientSession() as session:
# async with session.post(url=API_URL, json=data_json) as response:
# result = await response.read()
# # logging.info(result)
# ret = json.loads(result)
# return ret

file_path = ret["data"][1]["name"]

new_file_path = self.common.move_file(file_path, self.audio_out_path, 'vits_fast_' + self.common.get_bj_time(4))

return new_file_path
except Exception as e:
logging.error(e)
return None


# 请求Edge-TTS接口获取合成后的音频路径
async def edge_tts_api(self, data):
try:
file_name = 'edge_tts_' + self.common.get_bj_time(4) + '.wav'
voice_tmp_path = self.get_new_audio_path(file_name)
# voice_tmp_path = './out/' + self.common.get_bj_time(4) + '.mp3'
# 过滤" '字符
data["content"] = data["content"].replace('"', '').replace("'", '')
# 使用 Edge TTS 生成回复消息的语音文件
communicate = edge_tts.Communicate(text=data["content"], voice=data["voice"], rate=data["rate"], volume=data["volume"])
await communicate.save(voice_tmp_path)

return voice_tmp_path
except Exception as e:
logging.error(e)
return None


# 请求bark-gui的api
def bark_gui_api(self, data):
try:
Expand All @@ -152,7 +182,9 @@ def bark_gui_api(self, data):
fn_index=3
)

return result
new_file_path = self.common.move_file(result, self.audio_out_path, 'bark_gui_' + self.common.get_bj_time(4))

return new_file_path
except Exception as e:
logging.error(f'bark_gui请求失败: {e}')
return None
Expand All @@ -171,7 +203,9 @@ def vall_e_x_api(self, data):
fn_index=5
)

return result[1]
new_file_path = self.common.move_file(result[1], self.audio_out_path, 'vall_e_x_' + self.common.get_bj_time(4))

return new_file_path
except Exception as e:
logging.error(f'vall_e_x_api请求失败: {e}')
return None
Expand All @@ -196,7 +230,11 @@ async def genshinvoice_top_api(self, text):
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as response:
response = await response.read()
voice_tmp_path = './out/genshinvoice_top_' + self.common.get_bj_time(4) + '.wav'
# voice_tmp_path = os.path.join(self.audio_out_path, 'genshinvoice_top_' + self.common.get_bj_time(4) + '.wav')
file_name = 'genshinvoice_top_' + self.common.get_bj_time(4) + '.wav'

voice_tmp_path = self.get_new_audio_path(file_name)

with open(voice_tmp_path, 'wb') as f:
f.write(response)

Expand All @@ -207,3 +245,18 @@ async def genshinvoice_top_api(self, text):
logging.error(f'genshinvoice.top未知错误: {e}')

return None


# 获取新的音频路径
def get_new_audio_path(self, file_name):
# 判断路径是否为绝对路径
if os.path.isabs(self.audio_out_path):
# 如果是绝对路径,直接使用
voice_tmp_path = os.path.join(self.audio_out_path, file_name)
else:
# 如果不是绝对路径,检查是否包含 ./,如果不包含,添加 ./,然后拼接路径
if not self.audio_out_path.startswith('./'):
self.audio_out_path = './' + self.audio_out_path
voice_tmp_path = os.path.normpath(os.path.join(self.audio_out_path, file_name))

return voice_tmp_path
3 changes: 1 addition & 2 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ def local_qa_handle(self, data):

# 根据 弹幕日志类型进行各类日志写入
if My_handle.config.get("comment_log_type") == "问答":
f.write(
f"[{user_name} 提问]:{content}\n[AI回复{user_name}]:{resp_content_joined}\n" + tmp_content)
f.write(f"[{user_name} 提问]:{content}\n[AI回复{user_name}]:{resp_content_joined}\n" + tmp_content)
elif My_handle.config.get("comment_log_type") == "问题":
f.write(f"[{user_name} 提问]:{content}\n" + tmp_content)
elif My_handle.config.get("comment_log_type") == "回答":
Expand Down

0 comments on commit 823b5f8

Please sign in to comment.