From c9b713fa8555102615ceaa5c9fb47dff4f887e30 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 1 Mar 2024 21:58:18 +0800 Subject: [PATCH] =?UTF-8?q?TTS=E6=8F=92=E4=BB=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 14 +---- plugins/msg-logging/log/.gitkeep | 0 plugins/text-to-speech/config.py | 62 ++++++++++++++++++- .../text-to-speech/data/config.example.ini | 25 ++++++++ plugins/text-to-speech/listener.py | 15 ++++- plugins/text-to-speech/log/.gitkeep | 0 plugins/text-to-speech/tts.py | 2 +- 7 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 plugins/msg-logging/log/.gitkeep create mode 100644 plugins/text-to-speech/data/config.example.ini create mode 100644 plugins/text-to-speech/log/.gitkeep diff --git a/.gitignore b/.gitignore index 5592709..6b306bd 100644 --- a/.gitignore +++ b/.gitignore @@ -105,15 +105,5 @@ venv.bak/ .idea/ -data/* -!data/config.example.ini -!data/emoticons/ -data/emoticons/* -!data/emoticons/.gitkeep -!data/custom_public/ -data/custom_public/* -!data/custom_public/README.txt -!data/plugins/ -data/plugins/* -!data/plugins/.gitkeep -log/* +data/ +log/ diff --git a/plugins/msg-logging/log/.gitkeep b/plugins/msg-logging/log/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/plugins/text-to-speech/config.py b/plugins/text-to-speech/config.py index f7f2aeb..287f498 100644 --- a/plugins/text-to-speech/config.py +++ b/plugins/text-to-speech/config.py @@ -1,17 +1,47 @@ # -*- coding: utf-8 -*- +import configparser +import logging import os from typing import * +logger = logging.getLogger('text-to-speech.' + __name__) + BASE_PATH = os.path.realpath(os.getcwd()) LOG_PATH = os.path.join(BASE_PATH, 'log') +DATA_PATH = os.path.join(BASE_PATH, 'data') + +CONFIG_PATH_LIST = [ + os.path.join(DATA_PATH, 'config.ini'), + os.path.join(DATA_PATH, 'config.example.ini') +] _config: Optional['AppConfig'] = None def init(): + if reload(): + return + logger.warning('Using default config') global _config _config = AppConfig() - # TODO 读配置文件 + + +def reload(): + config_path = '' + for path in CONFIG_PATH_LIST: + if os.path.exists(path): + config_path = path + break + if config_path == '': + return False + + config = AppConfig() + if not config.load(config_path): + return False + + global _config + _config = config + return True def get_config(): @@ -20,14 +50,40 @@ def get_config(): class AppConfig: def __init__(self): - self.tts_voice_id: Optional[str] = None + self.tts_voice_id = '' self.tts_rate = 250 self.tts_volume = 1.0 self.max_tts_queue_size = 5 self.template_text = '{author_name} 说:{content}' - self.template_free_gift = '{author_name} 赠送了{num}个{gift_name},总价{total_coin}银瓜子' + # self.template_free_gift = '{author_name} 赠送了{num}个{gift_name},总价{total_coin}银瓜子' + self.template_free_gift = '{author_name} 赠送了{num}个{gift_name}' self.template_paid_gift = '{author_name} 赠送了{num}个{gift_name},总价{price}元' self.template_member = '{author_name} 购买了{num}{unit} {guard_name}' self.template_super_chat = '{author_name} 发送了{price}元的醒目留言:{content}' + + def load(self, path): + try: + config = configparser.ConfigParser() + config.read(path, 'utf-8-sig') + + self._load_app_config(config) + except Exception: # noqa + logger.exception('Failed to load config:') + return False + return True + + def _load_app_config(self, config: configparser.ConfigParser): + app_section = config['app'] + self.tts_voice_id = app_section.get('tts_voice_id', self.tts_voice_id) + self.tts_rate = app_section.getint('tts_rate', self.tts_rate) + self.tts_volume = app_section.getfloat('tts_volume', self.tts_volume) + + self.max_tts_queue_size = app_section.getint('max_tts_queue_size', self.max_tts_queue_size) + + self.template_text = app_section.get('template_text', self.template_text) + self.template_free_gift = app_section.get('template_free_gift', self.template_free_gift) + self.template_paid_gift = app_section.get('template_paid_gift', self.template_paid_gift) + self.template_member = app_section.get('template_member', self.template_member) + self.template_super_chat = app_section.get('template_super_chat', self.template_super_chat) diff --git a/plugins/text-to-speech/data/config.example.ini b/plugins/text-to-speech/data/config.example.ini new file mode 100644 index 0000000..dc66f22 --- /dev/null +++ b/plugins/text-to-speech/data/config.example.ini @@ -0,0 +1,25 @@ +# 如果要修改配置,可以复制此文件并重命名为“config.ini”再修改 + +[app] +# 语音ID,如果为空则使用默认的。取值看启动时“Available voices:”这条日志里的ID +tts_voice_id = +# 语速 +tts_rate = 250 +# 音量 +tts_volume = 1.0 + +# 最大队列长度,未读的消息数超过这个长度则不会读新的消息 +max_tts_queue_size = 5 + +# 消息模板,如果为空则不读 +# 弹幕 +template_text = {author_name} 说:{content} +# 免费礼物 +# template_free_gift = {author_name} 赠送了{num}个{gift_name},总价{total_coin}银瓜子 +template_free_gift = {author_name} 赠送了{num}个{gift_name} +# 付费礼物 +template_paid_gift = {author_name} 赠送了{num}个{gift_name},总价{price}元 +# 上舰 +template_member = {author_name} 购买了{num}{unit} {guard_name} +# 醒目留言 +template_super_chat = {author_name} 发送了{price}元的醒目留言:{content} diff --git a/plugins/text-to-speech/listener.py b/plugins/text-to-speech/listener.py index bf8f4c1..2fc9ef9 100644 --- a/plugins/text-to-speech/listener.py +++ b/plugins/text-to-speech/listener.py @@ -2,6 +2,7 @@ import __main__ import logging import os +import subprocess import sys from typing import * @@ -33,11 +34,19 @@ class MsgHandler(blcsdk.BaseHandler): def _on_open_plugin_admin_ui( self, client: blcsdk.BlcPluginClient, message: sdk_models.OpenPluginAdminUiMsg, extra: sdk_models.ExtraData ): + config_path = '' + for path in config.CONFIG_PATH_LIST: + if os.path.exists(path): + config_path = path + break + if config_path == '': + logger.warning('Config file not found, candidates: %s', config.CONFIG_PATH_LIST) + return + if sys.platform == 'win32': - # TODO 浏览配置文件 - os.startfile(config.LOG_PATH) + subprocess.run(['explorer', '/select,' + config_path]) else: - logger.info('Log path is "%s"', config.LOG_PATH) + logger.info('Config path is "%s"', config_path) def _on_add_text(self, client: blcsdk.BlcPluginClient, message: sdk_models.AddTextMsg, extra: sdk_models.ExtraData): if extra.is_from_plugin: diff --git a/plugins/text-to-speech/log/.gitkeep b/plugins/text-to-speech/log/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/plugins/text-to-speech/tts.py b/plugins/text-to-speech/tts.py index 93d6562..be2278c 100644 --- a/plugins/text-to-speech/tts.py +++ b/plugins/text-to-speech/tts.py @@ -121,7 +121,7 @@ class Tts: logger.info('Available voices:\n%s', '\n'.join(map(str, voices))) cfg = config.get_config() - if cfg.tts_voice_id is not None: + if cfg.tts_voice_id != '': self._engine.setProperty('voice', cfg.tts_voice_id) self._engine.setProperty('rate', cfg.tts_rate) self._engine.setProperty('volume', cfg.tts_volume)