From 823b5f81a9f312e639dbc21586a71a6a664d5c95 Mon Sep 17 00:00:00 2001 From: ikaros <327209194@qq.com> Date: Thu, 28 Sep 2023 23:22:39 +0800 Subject: [PATCH] =?UTF-8?q?edge-tts=E5=90=88=E5=85=A5=E7=BB=9F=E4=B8=80cla?= =?UTF-8?q?ss=E8=B0=83=E7=94=A8=EF=BC=88=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/badwords.txt | 1 - utils/audio.py | 42 +++++++++++--------- utils/audio_handle/my_tts.py | 77 ++++++++++++++++++++++++++++++------ utils/my_handle.py | 3 +- 4 files changed, 90 insertions(+), 33 deletions(-) diff --git a/data/badwords.txt b/data/badwords.txt index 35529e97..f3bfa2f2 100644 --- a/data/badwords.txt +++ b/data/badwords.txt @@ -1154,7 +1154,6 @@ 办驾驶 张荣坤 鄧朴方 -手机 車侖 小便 毛煞逼 diff --git a/utils/audio.py b/utils/audio.py index 39e8faf5..ee1efbf9 100644 --- a/utils/audio.py +++ b/utils/audio.py @@ -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}") @@ -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}") @@ -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) diff --git a/utils/audio_handle/my_tts.py b/utils/audio_handle/my_tts.py index a5ed1294..ed3e821c 100644 --- a/utils/audio_handle/my_tts.py +++ b/utils/audio_handle/my_tts.py @@ -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 @@ -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): @@ -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) @@ -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: @@ -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 @@ -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 @@ -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) @@ -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 \ No newline at end of file diff --git a/utils/my_handle.py b/utils/my_handle.py index 64960b0d..214846e9 100644 --- a/utils/my_handle.py +++ b/utils/my_handle.py @@ -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") == "回答":