voice-changer/server/voice_changer/RVC/embedder/EmbedderManager.py

60 lines
2.4 KiB
Python
Raw Normal View History

2023-05-02 06:11:00 +03:00
from torch import device
from const import EnumEmbedderTypes
from voice_changer.RVC.embedder.Embedder import Embedder
from voice_changer.RVC.embedder.FairseqContentvec import FairseqContentvec
from voice_changer.RVC.embedder.FairseqHubert import FairseqHubert
from voice_changer.RVC.embedder.FairseqHubertJp import FairseqHubertJp
2023-05-04 16:46:42 +03:00
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
2023-05-02 06:11:00 +03:00
class EmbedderManager:
currentEmbedder: Embedder | None = None
2023-05-04 16:46:42 +03:00
params: VoiceChangerParams
@classmethod
def initialize(cls, params: VoiceChangerParams):
cls.params = params
2023-05-02 06:11:00 +03:00
@classmethod
def getEmbedder(
2023-05-04 16:46:42 +03:00
cls, embederType: EnumEmbedderTypes, isHalf: bool, dev: device
2023-05-02 06:11:00 +03:00
) -> Embedder:
if cls.currentEmbedder is None:
print("[Voice Changer] generate new embedder. (no embedder)")
2023-05-04 16:46:42 +03:00
cls.currentEmbedder = cls.loadEmbedder(embederType, isHalf, dev)
elif cls.currentEmbedder.matchCondition(embederType) is False:
2023-05-02 06:11:00 +03:00
print("[Voice Changer] generate new embedder. (not match)")
2023-05-04 16:46:42 +03:00
cls.currentEmbedder = cls.loadEmbedder(embederType, isHalf, dev)
2023-05-02 06:11:00 +03:00
else:
cls.currentEmbedder.setDevice(dev)
cls.currentEmbedder.setHalf(isHalf)
2023-05-03 07:14:00 +03:00
# print("[Voice Changer] generate new embedder. (ANYWAY)", isHalf)
# cls.currentEmbedder = cls.loadEmbedder(embederType, file, isHalf, dev)
2023-05-02 06:11:00 +03:00
return cls.currentEmbedder
@classmethod
def loadEmbedder(
2023-05-04 16:46:42 +03:00
cls, embederType: EnumEmbedderTypes, isHalf: bool, dev: device
2023-05-02 06:11:00 +03:00
) -> Embedder:
2023-05-02 16:29:28 +03:00
if (
embederType == EnumEmbedderTypes.hubert
or embederType == EnumEmbedderTypes.hubert.value
):
2023-05-04 16:46:42 +03:00
file = cls.params.hubert_base
2023-05-02 06:11:00 +03:00
return FairseqHubert().loadModel(file, dev, isHalf)
2023-05-02 16:29:28 +03:00
elif (
embederType == EnumEmbedderTypes.hubert_jp
or embederType == EnumEmbedderTypes.hubert_jp.value
):
2023-05-04 16:46:42 +03:00
file = cls.params.hubert_base_jp
2023-05-02 06:11:00 +03:00
return FairseqHubertJp().loadModel(file, dev, isHalf)
2023-05-02 16:29:28 +03:00
elif (
embederType == EnumEmbedderTypes.contentvec
or embederType == EnumEmbedderTypes.contentvec.value
):
2023-05-04 16:46:42 +03:00
file = cls.params.hubert_base
2023-05-02 06:11:00 +03:00
return FairseqContentvec().loadModel(file, dev, isHalf)
else:
return FairseqHubert().loadModel(file, dev, isHalf)