mirror of
https://github.com/xfgryujk/blivechat.git
synced 2024-12-26 21:00:15 +08:00
TTS插件添加配置文件
This commit is contained in:
parent
7a158bf185
commit
c9b713fa85
14
.gitignore
vendored
14
.gitignore
vendored
@ -105,15 +105,5 @@ venv.bak/
|
|||||||
|
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
data/*
|
data/
|
||||||
!data/config.example.ini
|
log/
|
||||||
!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/*
|
|
||||||
|
0
plugins/msg-logging/log/.gitkeep
Normal file
0
plugins/msg-logging/log/.gitkeep
Normal file
@ -1,17 +1,47 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import configparser
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
|
logger = logging.getLogger('text-to-speech.' + __name__)
|
||||||
|
|
||||||
BASE_PATH = os.path.realpath(os.getcwd())
|
BASE_PATH = os.path.realpath(os.getcwd())
|
||||||
LOG_PATH = os.path.join(BASE_PATH, 'log')
|
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
|
_config: Optional['AppConfig'] = None
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
|
if reload():
|
||||||
|
return
|
||||||
|
logger.warning('Using default config')
|
||||||
global _config
|
global _config
|
||||||
_config = AppConfig()
|
_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():
|
def get_config():
|
||||||
@ -20,14 +50,40 @@ def get_config():
|
|||||||
|
|
||||||
class AppConfig:
|
class AppConfig:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tts_voice_id: Optional[str] = None
|
self.tts_voice_id = ''
|
||||||
self.tts_rate = 250
|
self.tts_rate = 250
|
||||||
self.tts_volume = 1.0
|
self.tts_volume = 1.0
|
||||||
|
|
||||||
self.max_tts_queue_size = 5
|
self.max_tts_queue_size = 5
|
||||||
|
|
||||||
self.template_text = '{author_name} 说:{content}'
|
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_paid_gift = '{author_name} 赠送了{num}个{gift_name},总价{price}元'
|
||||||
self.template_member = '{author_name} 购买了{num}{unit} {guard_name}'
|
self.template_member = '{author_name} 购买了{num}{unit} {guard_name}'
|
||||||
self.template_super_chat = '{author_name} 发送了{price}元的醒目留言:{content}'
|
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)
|
||||||
|
25
plugins/text-to-speech/data/config.example.ini
Normal file
25
plugins/text-to-speech/data/config.example.ini
Normal 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}
|
@ -2,6 +2,7 @@
|
|||||||
import __main__
|
import __main__
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
@ -33,11 +34,19 @@ class MsgHandler(blcsdk.BaseHandler):
|
|||||||
def _on_open_plugin_admin_ui(
|
def _on_open_plugin_admin_ui(
|
||||||
self, client: blcsdk.BlcPluginClient, message: sdk_models.OpenPluginAdminUiMsg, extra: sdk_models.ExtraData
|
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':
|
if sys.platform == 'win32':
|
||||||
# TODO 浏览配置文件
|
subprocess.run(['explorer', '/select,' + config_path])
|
||||||
os.startfile(config.LOG_PATH)
|
|
||||||
else:
|
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):
|
def _on_add_text(self, client: blcsdk.BlcPluginClient, message: sdk_models.AddTextMsg, extra: sdk_models.ExtraData):
|
||||||
if extra.is_from_plugin:
|
if extra.is_from_plugin:
|
||||||
|
0
plugins/text-to-speech/log/.gitkeep
Normal file
0
plugins/text-to-speech/log/.gitkeep
Normal file
@ -121,7 +121,7 @@ class Tts:
|
|||||||
logger.info('Available voices:\n%s', '\n'.join(map(str, voices)))
|
logger.info('Available voices:\n%s', '\n'.join(map(str, voices)))
|
||||||
|
|
||||||
cfg = config.get_config()
|
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('voice', cfg.tts_voice_id)
|
||||||
self._engine.setProperty('rate', cfg.tts_rate)
|
self._engine.setProperty('rate', cfg.tts_rate)
|
||||||
self._engine.setProperty('volume', cfg.tts_volume)
|
self._engine.setProperty('volume', cfg.tts_volume)
|
||||||
|
Loading…
Reference in New Issue
Block a user