improve error handling

This commit is contained in:
wataru 2023-05-26 16:52:05 +09:00
parent 3d8dc7a9bd
commit ca989775dd
3 changed files with 66 additions and 50 deletions

View File

@ -43,7 +43,6 @@ class MMVC_Namespace(socketio.AsyncNamespace):
async def on_request_message(self, sid, msg): async def on_request_message(self, sid, msg):
self.sid = sid self.sid = sid
await self.asynctest("on req")
timestamp = int(msg[0]) timestamp = int(msg[0])
data = msg[1] data = msg[1]
if isinstance(data, str): if isinstance(data, str):

View File

@ -8,47 +8,51 @@ import os
def generateModelSlot(slotDir: str): def generateModelSlot(slotDir: str):
modelSlot = ModelSlot() try:
if os.path.exists(slotDir) is False: modelSlot = ModelSlot()
if os.path.exists(slotDir) is False:
return modelSlot
paramFile = os.path.join(slotDir, "params.json")
with open(paramFile, "r") as f:
params = json.load(f)
modelSlot.modelFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcModel"])
)
if "rvcFeature" in params["files"]:
modelSlot.featureFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcFeature"])
)
else:
modelSlot.featureFile = None
if "rvcIndex" in params["files"]:
modelSlot.indexFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcIndex"])
)
else:
modelSlot.indexFile = None
modelSlot.defaultTune = params["defaultTune"] if "defaultTune" in params else 0
modelSlot.defaultIndexRatio = (
params["defaultIndexRatio"] if "defaultIndexRatio" in params else 0
)
modelSlot.name = params["name"] if "name" in params else None
modelSlot.description = params["description"] if "description" in params else None
modelSlot.credit = params["credit"] if "credit" in params else None
modelSlot.termsOfUseUrl = (
params["termsOfUseUrl"] if "termsOfUseUrl" in params else None
)
modelSlot.isONNX = modelSlot.modelFile.endswith(".onnx")
if modelSlot.isONNX:
_setInfoByONNX(modelSlot)
else:
_setInfoByPytorch(modelSlot)
return modelSlot return modelSlot
paramFile = os.path.join(slotDir, "params.json") except Exception as e:
with open(paramFile, "r") as f: print(f"[Voice Changer] faild to generate slot: {e}")
params = json.load(f) return ModelSlot()
modelSlot.modelFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcModel"])
)
if "rvcFeature" in params["files"]:
modelSlot.featureFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcFeature"])
)
else:
modelSlot.featureFile = None
if "rvcIndex" in params["files"]:
modelSlot.indexFile = os.path.join(
slotDir, os.path.basename(params["files"]["rvcIndex"])
)
else:
modelSlot.indexFile = None
modelSlot.defaultTune = params["defaultTune"] if "defaultTune" in params else 0
modelSlot.defaultIndexRatio = (
params["defaultIndexRatio"] if "defaultIndexRatio" in params else 0
)
modelSlot.name = params["name"] if "name" in params else None
modelSlot.description = params["description"] if "description" in params else None
modelSlot.credit = params["credit"] if "credit" in params else None
modelSlot.termsOfUseUrl = (
params["termsOfUseUrl"] if "termsOfUseUrl" in params else None
)
modelSlot.isONNX = modelSlot.modelFile.endswith(".onnx")
if modelSlot.isONNX:
_setInfoByONNX(modelSlot)
else:
_setInfoByPytorch(modelSlot)
return modelSlot
def _setInfoByPytorch(slot: ModelSlot): def _setInfoByPytorch(slot: ModelSlot):

View File

@ -92,7 +92,7 @@ class VoiceChangerSettings:
class VoiceChanger: class VoiceChanger:
settings: VoiceChangerSettings settings: VoiceChangerSettings
voiceChanger: VoiceChangerModel voiceChanger: VoiceChangerModel | None = None
ioRecorder: IORecorder ioRecorder: IORecorder
sola_buffer: AudioInOut sola_buffer: AudioInOut
namespace: socketio.AsyncNamespace | None = None namespace: socketio.AsyncNamespace | None = None
@ -142,6 +142,7 @@ class VoiceChanger:
if ( if (
vc.settings.serverAudioStated == 0 vc.settings.serverAudioStated == 0
or vc.settings.serverInputDeviceId == -1 or vc.settings.serverInputDeviceId == -1
or vc.voiceChanger is None
): ):
vc.settings.inputSampleRate = 48000 vc.settings.inputSampleRate = 48000
time.sleep(2) time.sleep(2)
@ -270,7 +271,7 @@ class VoiceChanger:
def switchModelType(self, modelType: ModelType): def switchModelType(self, modelType: ModelType):
try: try:
if hasattr(self, "voiceChanger") and self.voiceChanger is not None: if self.voiceChanger is not None:
# return {"status": "ERROR", "msg": "vc is already selected. currently re-select is not implemented"} # return {"status": "ERROR", "msg": "vc is already selected. currently re-select is not implemented"}
del self.voiceChanger del self.voiceChanger
self.voiceChanger = None self.voiceChanger = None
@ -320,6 +321,8 @@ class VoiceChanger:
def loadModel(self, props: LoadModelParams): def loadModel(self, props: LoadModelParams):
try: try:
if self.voiceChanger is None:
raise RuntimeError("Voice Changer is not selected.")
return self.voiceChanger.loadModel(props) return self.voiceChanger.loadModel(props)
except Exception as e: except Exception as e:
print(traceback.format_exc()) print(traceback.format_exc())
@ -328,7 +331,7 @@ class VoiceChanger:
def get_info(self): def get_info(self):
data = asdict(self.settings) data = asdict(self.settings)
if hasattr(self, "voiceChanger"): if self.voiceChanger is not None:
data.update(self.voiceChanger.get_info()) data.update(self.voiceChanger.get_info())
return data return data
@ -336,6 +339,10 @@ class VoiceChanger:
return self.settings.performance return self.settings.performance
def update_settings(self, key: str, val: Any): def update_settings(self, key: str, val: Any):
if self.voiceChanger is None:
print("[Voice Changer] Voice Changer is not selected.")
return
if key in self.settings.intData: if key in self.settings.intData:
setattr(self.settings, key, int(val)) setattr(self.settings, key, int(val))
if key == "crossFadeOffsetRate" or key == "crossFadeEndRate": if key == "crossFadeOffsetRate" or key == "crossFadeEndRate":
@ -359,12 +366,9 @@ class VoiceChanger:
elif key in self.settings.strData: elif key in self.settings.strData:
setattr(self.settings, key, str(val)) setattr(self.settings, key, str(val))
else: else:
if hasattr(self, "voiceChanger"): ret = self.voiceChanger.update_settings(key, val)
ret = self.voiceChanger.update_settings(key, val) if ret is False:
if ret is False: print(f"{key} is not mutable variable or unknown variable!")
print(f"{key} is not mutable variable or unknown variable!")
else:
print("voice changer is not initialized!")
return self.get_info() return self.get_info()
def _generate_strength(self, crossfadeSize: int): def _generate_strength(self, crossfadeSize: int):
@ -422,6 +426,9 @@ class VoiceChanger:
self, receivedData: AudioInOut self, receivedData: AudioInOut
) -> tuple[AudioInOut, list[Union[int, float]]]: ) -> tuple[AudioInOut, list[Union[int, float]]]:
try: try:
if self.voiceChanger is None:
raise RuntimeError("Voice Changer is not selected.")
processing_sampling_rate = self.voiceChanger.get_processing_sampling_rate() processing_sampling_rate = self.voiceChanger.get_processing_sampling_rate()
# 前処理 # 前処理
@ -571,10 +578,16 @@ class VoiceChanger:
############## ##############
def merge_models(self, request: str): def merge_models(self, request: str):
if self.voiceChanger is None:
print("[Voice Changer] Voice Changer is not selected.")
return
self.voiceChanger.merge_models(request) self.voiceChanger.merge_models(request)
return self.get_info() return self.get_info()
def update_model_default(self): def update_model_default(self):
if self.voiceChanger is None:
print("[Voice Changer] Voice Changer is not selected.")
return
self.voiceChanger.update_model_default() self.voiceChanger.update_model_default()
return self.get_info() return self.get_info()