This commit is contained in:
恍兮惚兮 2024-08-06 12:52:01 +08:00
parent acce466c1b
commit f1445937fc
7 changed files with 106 additions and 101 deletions

View File

@ -67,7 +67,6 @@ class MAINUI:
self.lasttranslatorindex = 0
self.translators = {}
self.cishus = {}
self.reader = None
self.specialreaders = {}
self.textsource_p = None
self.currentmd5 = "0"
@ -90,6 +89,24 @@ class MAINUI:
self.sqlsavegameinfo = None
self.notifyonce = set()
self.audioplayer = series_audioplayer()
self._internal_reader = None
self.reader_uid = None
@property
def reader(self):
return self._internal_reader
@reader.setter
def reader(self, _):
if _ is None:
self._internal_reader = None
self.reader_uid = None
self.settin_ui.voicelistsignal.emit(None)
else:
if self.reader_uid != _.uid:
return
self._internal_reader = _
self.settin_ui.voicelistsignal.emit(_)
@property
def textsource(self):
@ -584,31 +601,42 @@ class MAINUI:
else:
self.__readcurrent(self.currentread, None, force)
def loadreader(self, use, voicelistsignal=None, privateconfig=None, init=True):
if voicelistsignal is None:
voicelistsignal = self.settin_ui.voicelistsignal
def loadreader(self, use, privateconfig=None, init=True, uid=None):
aclass = importlib.import_module("tts." + use).TTS
obj = aclass(use, voicelistsignal, self.audioplayer.play, privateconfig, init)
if uid is None:
uid = uuid.uuid4()
obj = aclass(use, self.audioplayer.play, privateconfig, init, uid)
return obj
@threader
def startreader(self, use=None, checked=True):
def __reader_usewhich(self):
self.reader = None
self.settin_ui.voicelistsignal.emit(None)
for key in globalconfig["reader"]:
if globalconfig["reader"][key]["use"] and os.path.exists(
("./LunaTranslator/tts/" + key + ".py")
):
return key
return None
@threader
def startreader(self, use=None, checked=True, setting=False):
if setting:
if use != self.__reader_usewhich():
return
self.reader = None
self.reader_uid = uuid.uuid4()
self.reader = self.loadreader(use, uid=self.reader_uid)
return
if not checked:
self.reader = None
return
if use is None:
for key in globalconfig["reader"]:
if globalconfig["reader"][key]["use"] and os.path.exists(
("./LunaTranslator/tts/" + key + ".py")
):
use = key
break
use = self.__reader_usewhich()
if not use:
self.reader = None
return
self.reader = self.loadreader(use)
self.reader_usevoice = use
self.reader = None
self.reader_uid = uuid.uuid4()
self.reader = self.loadreader(use, uid=self.reader_uid)
def selectprocess(self, selectedp, title):
self.textsource = None

View File

@ -5,7 +5,6 @@ import qtawesome, os, gobject
from myutils.config import globalconfig, _TR
from myutils.utils import makehtml
from myutils.wrapper import Singleton_close
from tts.basettsclass import getvisidx
from gui.usefulwidget import (
MySwitch,
selectcolor,
@ -212,7 +211,7 @@ class voiceselect(LDialog):
self.engine_internal.append(name)
self.datas = {
"engine": self.engine_internal[0],
"voice": "",
"voice": None,
"vis": "",
"visx": "",
}
@ -232,12 +231,10 @@ class voiceselect(LDialog):
self.lastwidget = None
def loadedvoice(self, obj):
vl, idx = getvisidx(obj)
self.datas["voice"] = obj.voice
vl = obj.voiceshowlist
if self._layout.rowCount() == 3:
self._layout.removeRow(1)
if len(vl) == 0:
return
self.datas["voice"] = obj.voice
voices = getsimplecombobox(
vl,
self.datas,
@ -255,8 +252,10 @@ class voiceselect(LDialog):
def __engine_cb(self, internal):
self.datas["visx"] = self.engine_vis[self.engine_internal.index(internal)]
self.datas["vis"] = self.datas["visx"]
self.datas["voice"] = None
try:
self.object = gobject.baseobject.loadreader(internal, self.voicelistsignal, init=False)
self.object = gobject.baseobject.loadreader(internal, init=False)
self.voicelistsignal.emit(self.object)
except:
if self._layout.rowCount() == 3:
@ -357,6 +356,9 @@ class yuyinzhidingsetting(LDialog):
elif idx == 2:
voice = voiceselect(self)
if voice.exec():
if voice.datas["voice"] is None:
com.setCurrentIndex(1)
return
config["target"] = (
voice.datas["engine"],
voice.datas["voice"],

View File

@ -1,7 +1,6 @@
from qtsymbols import *
import os, functools
import gobject
from tts.basettsclass import getvisidx
from myutils.config import globalconfig, static_data
from gui.inputdialog import (
autoinitdialog_items,
@ -21,23 +20,30 @@ from gui.usefulwidget import (
def showvoicelist(self, obj):
vl, idx = getvisidx(obj)
if obj is None:
try:
self.voicecombo.clear()
except:
pass
return
vl = obj.voiceshowlist
idx = obj.voicelist.index(obj.voice)
try:
self.voicecombo.blockSignals(True)
self.voicecombo.clear()
self.voicecombo.addItems(vl)
if idx >= 0:
self.voicecombo.setCurrentIndex(idx)
self.voicecombo.blockSignals(False)
self.voicecombo.setCurrentIndex(idx)
except:
self.voicecombo_cache = vl, idx
def changevoice(self, text):
globalconfig["reader"][gobject.baseobject.reader_usevoice]["voice"] = (
gobject.baseobject.reader.voicelist[self.voicecombo.currentIndex()]
)
if gobject.baseobject.reader is None:
return
gobject.baseobject.reader.voice = gobject.baseobject.reader.voicelist[
self.voicecombo.currentIndex()
]
def createvoicecombo(self):
@ -71,7 +77,9 @@ def getttsgrid(self):
continue
if "args" in globalconfig["reader"][name]:
items = autoinitdialog_items(globalconfig["reader"][name])
items[-1]["callback"] = gobject.baseobject.startreader
items[-1]["callback"] = functools.partial(
gobject.baseobject.startreader, name, True, True
)
_3 = D_getIconButton(
callback=functools.partial(
autoinitdialog,

View File

@ -37,32 +37,31 @@ class TTSbase:
@property
def voice(self):
_v = self.privateconfig["voice"]
if len(self.voicelist) == 0:
return None
if _v not in self.voicelist:
_v = self.voicelist[0]
return _v
@voice.setter
def voice(self, v):
self.privateconfig["voice"] = v
########################
def __init__(
self,
typename,
voicelistsignal,
playaudiofunction,
privateconfig=None,
init=True,
self, typename, playaudiofunction, privateconfig=None, init=True, uid=None
) -> None:
self.typename = typename
self.voicelistsignal = voicelistsignal
self.playaudiofunction = playaudiofunction
self.uid = uid
if privateconfig is None:
self.privateconfig = globalconfig["reader"][self.typename]
else:
self.privateconfig = privateconfig
self.voicelist, self.voiceshowlist = self.getvoicelist()
voicelistsignal.emit(self)
if len(self.voicelist) != len(self.voiceshowlist):
raise
if len(self.voicelist) == 0:
raise
if init:
self.init()
@ -87,16 +86,3 @@ class TTSbase:
except:
print_exc()
return
def getvisidx(obj: TTSbase):
if obj is None:
vl = []
idx = -1
else:
vl = obj.voiceshowlist
if obj.voice:
idx = obj.voicelist.index(obj.voice)
else:
idx = -1
return vl, idx

View File

@ -1,31 +1,33 @@
import requests
import time, os
from urllib.parse import urljoin
from tts.basettsclass import TTSbase
from urllib.parse import quote
class TTS(TTSbase):
def getvoicelist(self):
if self.config["voices"] == "":
return [(0, 0, 0)], []
responseVits = requests.get(
f"http://127.0.0.1:{self.config['Port']}/voice/speakers"
urljoin(self.config["URL"], self.config["voices"])
).json()
voicelist = []
# 获取所有模型类型,对于每个模型类型下的模型信息,将其 modelType、id、name 合成一个字符串
internal = []
modelTypes = responseVits.keys()
for modelType in modelTypes:
vits_data = responseVits[modelType]
for item in vits_data:
model_info = f'{modelType}_{item["id"]}_{item["name"]}'
voicelist.append(model_info)
return voicelist, voicelist
internal.append((modelType, item["id"], item["name"]))
return internal, voicelist
def speak(self, content, rate, voice):
encoded_content = quote(content)
idx = int(voice.split("_")[1])
model = str.lower(voice.split("_")[0])
response = requests.get(
f"http://127.0.0.1:{self.config['Port']}/voice/{model}?text={encoded_content}&id={idx}&lang=auto&prompt_lang=auto&format=wav&preset={self.config['preset']}"
).content
model, idx, _ = voice
speak = self.config["speak"].format(
model_lower=model.lower(), model=model, id=idx, text=encoded_content
)
response = requests.get(urljoin(self.config["URL"], speak)).content
return response

View File

@ -1037,6 +1037,12 @@
}
},
"reader": {
"windowstts": {
"use": false,
"voice": "",
"name": "WindowsTTS",
"type": "offline"
},
"huoshantts": {
"use": false,
"voice": "",
@ -1054,12 +1060,6 @@
"name": "有道TTS",
"useproxy": false
},
"windowstts": {
"use": false,
"voice": "",
"name": "WindowsTTS",
"type": "offline"
},
"NeoSpeech": {
"use": false,
"voice": "",
@ -1106,35 +1106,14 @@
"name": "vits-simple-api",
"type": "offline",
"args": {
"Port": 23456,
"preset": "",
"整合包_CPU": "{main_server}/Resource/IntegrationPack/vits-simple-api/cpu",
"整合包_GPU": "{main_server}/Resource/IntegrationPack/vits-simple-api/gpu"
},
"argstype": {
"Port": {
"type": "intspin",
"min": 1,
"max": 65535,
"step": 1,
"name": "端口号"
},
"整合包_CPU": {
"type": "label",
"islink": true
},
"整合包_GPU": {
"type": "label",
"islink": true
},
"preset": {
"name": "GPT-SOVITS preset"
}
"URL": "http://127.0.0.1:23456",
"voices": "/voice/speakers",
"speak": "/voice/{model_lower}?text={text}&id={id}&lang=auto&prompt_lang=auto&format=wav&preset=default"
}
},
"gtts": {
"use": false,
"name": "谷歌",
"name": "GoogleTTS",
"voice": ""
}
},

View File

@ -29,7 +29,7 @@ include(generate_product_version)
set(VERSION_MAJOR 5)
set(VERSION_MINOR 22)
set(VERSION_PATCH 1)
set(VERSION_PATCH 2)
add_library(pch pch.cpp)
target_precompile_headers(pch PUBLIC pch.h)