Skip to content

Commit

Permalink
Merge pull request #788 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
闲时任务优化,新增待播放音频阈值和计时缩减值,main新增callback接口接收音频播放状态;优化gradio_tts解析能力
  • Loading branch information
Ikaros-521 authored Apr 29, 2024
2 parents 5a0fee3 + 593326a commit 28a7e2b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
6 changes: 4 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,10 @@
],
"idle_time_task": {
"enable": false,
"idle_time_min": 60,
"idle_time_max": 120,
"idle_time_min": 30,
"idle_time_max": 60,
"wait_play_audio_num_threshold": 10,
"idle_time_reduce_to": 0,
"trigger_type": ["comment"],
"copywriting": {
"enable": false,
Expand Down
6 changes: 4 additions & 2 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,10 @@
],
"idle_time_task": {
"enable": false,
"idle_time_min": 60,
"idle_time_max": 120,
"idle_time_min": 30,
"idle_time_max": 60,
"wait_play_audio_num_threshold": 10,
"idle_time_reduce_to": 0,
"trigger_type": ["comment"],
"copywriting": {
"enable": false,
Expand Down
10 changes: 10 additions & 0 deletions docs/投资人/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_240309040204_QQ%E5%9B%BE%E7%89%8720240309114614.jpg",
amount: "¥50"
},
{
name: "老年人",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_240427041208_QQ%E5%9B%BE%E7%89%8720240427121056.jpg",
amount: "¥50"
},
{
name: "Yuno",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230719132530_QQ%E5%9B%BE%E7%89%8720230719212024.jpg",
Expand Down Expand Up @@ -546,6 +551,11 @@
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_230815120427_QQ%E5%9B%BE%E7%89%8720230815200306.jpg",
amount: "¥8.8"
},
{
name: "半闲",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_240427120314_QQ%E5%9B%BE%E7%89%8720240427200305.jpg",
amount: "¥8.8"
},
{
name: "77",
avatar: "https://images.cnblogs.com/cnblogs_com/ikaros-521/2328032/o_231009133208_QQ%E5%9B%BE%E7%89%8720231009213136.png",
Expand Down
Binary file modified docs/投资人/invest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
def start_server():
global config, common, my_handle, last_username_list, config_path, last_liveroom_data
global do_listen_and_comment_thread, stop_do_listen_and_comment_thread_event, faster_whisper_model



# 按键监听相关
Expand Down Expand Up @@ -145,7 +146,34 @@ def llm():

except Exception as e:
return jsonify({"code": -1, "message": f"发送数据失败!{e}"})


@app.route('/callback', methods=['POST'])
def callback():
global my_handle, config, global_idle_time

try:
try:
data_json = request.get_json()
logging.info(f"API收到数据:{data_json}")

# 音频播放完成
if data_json["type"] in ["audio_playback_completed"]:
# 如果等待播放的音频数量大于10
if data_json["data"]["wait_play_audio_num"] > int(config.get("idle_time_task", "wait_play_audio_num_threshold")):
logging.info(f'等待播放的音频数量大于限定值,闲时任务的闲时计时由 {global_idle_time} -> {int(config.get("idle_time_task", "idle_time_reduce_to"))}秒')
# 闲时任务的闲时计时 清零
global_idle_time = int(config.get("idle_time_task", "idle_time_reduce_to"))


return jsonify({"code": 200, "message": "callback处理成功!"})
except Exception as e:
logging.error(f"callback处理失败!{e}")
return jsonify({"code": -1, "message": f"callback处理失败!{e}"})

except Exception as e:
return jsonify({"code": -1, "message": f"callback处理失败!{e}"})


app.run(host="0.0.0.0", port=config.get("api_port"), debug=False)

# HTTP API线程并启动
Expand Down
30 changes: 30 additions & 0 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Audio:
# # 文案单独一个线程排队播放
# only_play_copywriting_thread = None

# 异常报警数据
abnormal_alarm_data = {
"platform": {
"error_count": 0
Expand Down Expand Up @@ -925,6 +926,31 @@ async def tts_handle(self, message):

return message

# 发送音频播放信息给main内部的http服务端
def send_audio_play_info_to_callback(self, data: dict=None):
"""发送音频播放信息给main内部的http服务端
Args:
data (dict): 音频播放信息
"""
if data is None:
data = {
"type": "audio_playback_completed",
"data": {
# 待播放音频数量
"wait_play_audio_num": Audio.voice_tmp_path_queue.qsize(),
# 待合成音频的消息数量
"wait_synthesis_msg_num": len(Audio.message_queue),
}
}

logging.debug(f"data={data}")

resp = self.common.send_request(f'http://{self.config.get("api_ip")}:{self.config.get("api_port")}/callback', "POST", data)

return resp


# 播放音频
async def my_play_voice(self, message):
"""合成音频并插入待播放队列
Expand Down Expand Up @@ -1170,6 +1196,8 @@ async def only_play_audio(self):
while Audio.mixer_normal.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_normal.music.stop()

self.send_audio_play_info_to_callback()
except pygame.error as e:
logging.error(traceback.format_exc())
# 如果发生 pygame.error 异常,则捕获并处理它
Expand Down Expand Up @@ -1278,6 +1306,8 @@ async def random_speed_and_play(audio_path):
while Audio.mixer_copywriting.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_copywriting.music.stop()

self.send_audio_play_info_to_callback()
except pygame.error as e:
logging.error(traceback.format_exc())
# 如果发生 pygame.error 异常,则捕获并处理它
Expand Down
9 changes: 6 additions & 3 deletions utils/audio_handle/my_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,12 @@ def get_file_path(data):
# 获取索引为1的元素
file_path = get_value_by_index(result, int(data_analysis))

if file_path:
logging.debug(f"文件路径:{file_path}")
return file_path
if file_path:
logging.debug(f"文件路径:{file_path}")
return file_path
elif isinstance(result, str):
logging.debug(f"文件路径:{result}")
return result
else:
logging.error("数据解析失败!Invalid index or response format.")
return None
Expand Down
6 changes: 5 additions & 1 deletion webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,8 @@ def common_textarea_handle(content):
config_data["idle_time_task"]["enable"] = switch_idle_time_task_enable.value
config_data["idle_time_task"]["idle_time_min"] = int(input_idle_time_task_idle_time_min.value)
config_data["idle_time_task"]["idle_time_max"] = int(input_idle_time_task_idle_time_max.value)
config_data["idle_time_task"]["wait_play_audio_num_threshold"] = int(input_idle_time_task_wait_play_audio_num_threshold.value)
config_data["idle_time_task"]["idle_time_reduce_to"] = int(input_idle_time_task_idle_time_reduce_to.value)

tmp_arr = []
for index in range(len(idle_time_task_trigger_type_var)):
Expand Down Expand Up @@ -2927,7 +2929,9 @@ def save_config():
switch_idle_time_task_enable = ui.switch('启用', value=config.get("idle_time_task", "enable")).style(switch_internal_css)
input_idle_time_task_idle_time_min = ui.input(label='最小闲时时间', value=config.get("idle_time_task", "idle_time_min"), placeholder='最小闲时间隔时间(正整数,单位:秒),就是在没有弹幕情况下经过的时间').style("width:150px;")
input_idle_time_task_idle_time_max = ui.input(label='最大闲时时间', value=config.get("idle_time_task", "idle_time_max"), placeholder='最大闲时间隔时间(正整数,单位:秒),就是在没有弹幕情况下经过的时间').style("width:150px;")

input_idle_time_task_wait_play_audio_num_threshold = ui.input(label='等待播放音频数量阈值', value=config.get("idle_time_task", "wait_play_audio_num_threshold"), placeholder='当等待播放音频数量超过这个阈值,将会在音频播放完毕后触发闲时时间减少到设定的缩减值,旨在控制闲时任务触发总量').style("width:150px;")
input_idle_time_task_idle_time_reduce_to = ui.input(label='闲时计时减小到', value=config.get("idle_time_task", "idle_time_reduce_to"), placeholder='达到阈值情况下,闲时计时缩减到的数值').style("width:150px;")

with ui.row():
ui.label('刷新闲时计时的消息类型')
# 类型列表
Expand Down

0 comments on commit 28a7e2b

Please sign in to comment.