TTS插件添加配置文件

This commit is contained in:
John Smith 2024-03-01 21:58:18 +08:00
parent 7a158bf185
commit c9b713fa85
7 changed files with 99 additions and 19 deletions

14
.gitignore vendored
View File

@ -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/

View File

View File

@ -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)

View File

@ -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}

View File

@ -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:

View File

View File

@ -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)