Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

音频部分相关的优化(音频输出路径自定义等) #344

Merged
merged 7 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,3 @@ cookie/*.json

# 数据库
data/data.db

main2.py
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"after_prompt": "",
"comment_log_type": "回答",
"play_audio": {
"enable": true
"enable": true,
"out_path": "out"
},
"show_box": {
"read_comment": true,
Expand Down
3 changes: 2 additions & 1 deletion config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"after_prompt": "",
"comment_log_type": "回答",
"play_audio": {
"enable": true
"enable": true,
"out_path": "out"
},
"show_box": {
"read_comment": true,
Expand Down
4 changes: 1 addition & 3 deletions data/badwords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
起気
乞錷
朲氣
朲芞
の朲
呷朲
陳希同
Expand Down Expand Up @@ -465,7 +466,6 @@
砍刀
阳精
带炼
鲁迅
金币网
李延芝
开他敏
Expand Down Expand Up @@ -1154,7 +1154,6 @@
办驾驶
张荣坤
鄧朴方
手机
車侖
小便
毛煞逼
Expand Down Expand Up @@ -4686,7 +4685,6 @@
陆肆
屌七
山心恨
断水
幹死
淫汁
小鸡巴
Expand Down
7 changes: 6 additions & 1 deletion docs/投资人/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{
name: "存在感患者",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230722160559_QQ%E5%9B%BE%E7%89%8720230722234802.jpg",
amount: "¥406"
amount: "¥410.8"
},
{
name: "月零宫白",
Expand Down Expand Up @@ -141,6 +141,11 @@
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230925111651_QQ%E5%9B%BE%E7%89%8720230925190851.jpg",
amount: "¥100"
},
{
name: "Jerry",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230926154835_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230926234750.jpg",
amount: "¥100"
},
{
name: "很养眼的丑男",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230722162732_QQ%E5%9B%BE%E7%89%8720230723000544.jpg",
Expand Down
Binary file modified docs/投资人/投资人.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,15 @@ def play_audio_gui_create():
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "音频输出路径",
"label_tip": "音频文件合成后存储的路径,支持相对路径或绝对路径",
"data": play_audio_config["out_path"],
"main_obj_name": "play_audio",
"index": 1
}
data_json.append(tmp_json)

widgets = self.create_widgets_from_json(data_json)

# 动态添加widget到对应的gridLayout
Expand Down Expand Up @@ -3030,7 +3039,8 @@ def reorganize_copywriting_config_data(copywriting_config_data):
config_data["database"] = reorganize_grid_data(database_data, database_keys_mapping)

play_audio_keys_mapping = {
"enable": 0
"enable": 0,
"out_path": 1
}

# 重组play_audio数据并写回json
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
13 changes: 13 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ def split_path_and_filename(self, file_path):

# 移动文件到指定路径 src dest
def move_file(self, source_path, destination_path, rename=None, format="wav"):
"""移动文件到指定路径

Args:
source_path (str): 文件路径含文件名
destination_path (_type_): 目标文件夹
rename (str, optional): 文件名. Defaults to None.
format (str, optional): 文件格式(实际上只是个假拓展名). Defaults to "wav".

Returns:
str: 输出到的完整路径含文件名
"""
# if os.path.exists(destination_path):
# # 如果目标位置已存在同名文件,则先将其移动到回收站
# send2trash(destination_path)
Expand All @@ -399,6 +410,8 @@ def move_file(self, source_path, destination_path, rename=None, format="wav"):
shutil.move(source_path, destination_path)
print(f"文件移动成功:{source_path} -> {destination_path}")

return destination_path


# 从文件路径中提取出带有扩展名的文件名
# 返回:str
Expand Down
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
Loading