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):
self.sid = sid
await self.asynctest("on req")
timestamp = int(msg[0])
data = msg[1]
if isinstance(data, str):

View File

@ -8,47 +8,51 @@ import os
def generateModelSlot(slotDir: str):
modelSlot = ModelSlot()
if os.path.exists(slotDir) is False:
try:
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
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
except Exception as e:
print(f"[Voice Changer] faild to generate slot: {e}")
return ModelSlot()
def _setInfoByPytorch(slot: ModelSlot):

View File

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