Skip to content

Commit a25ac24

Browse files
authored
Merge pull request #1255 from yqzhishen/fix-slow-phonemizer
Avoid repeated dictionary and model loading in DiffSingerBasePhonemizer
2 parents 9d574ef + cc6b440 commit a25ac24

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

OpenUtau.Core/DiffSinger/DiffSingerBasePhonemizer.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ public abstract class DiffSingerBasePhonemizer : MachineLearningPhonemizer
3232
protected virtual string GetDictionaryName()=>"dsdict.yaml";
3333
protected virtual string GetLangCode()=>String.Empty;//The language code of the language the phonemizer is made for
3434

35+
private bool _singerLoaded;
36+
3537
public override void SetSinger(USinger singer) {
38+
if (_singerLoaded && singer == this.singer) return;
39+
try {
40+
_singerLoaded = _executeSetSinger(singer);
41+
} catch {
42+
_singerLoaded = false;
43+
throw;
44+
}
45+
}
46+
47+
private bool _executeSetSinger(USinger singer) {
3648
this.singer = singer;
37-
if(singer==null){
38-
return;
49+
if (singer == null) {
50+
return false;
3951
}
4052
if(singer.Location == null){
4153
Log.Error("Singer location is null");
42-
return;
54+
return false;
4355
}
4456
if (File.Exists(Path.Join(singer.Location, "dsdur", "dsconfig.yaml"))) {
4557
rootPath = Path.Combine(singer.Location, "dsdur");
@@ -53,7 +65,7 @@ public override void SetSinger(USinger singer) {
5365
dsConfig = Yaml.DefaultDeserializer.Deserialize<DsConfig>(configTxt);
5466
} catch(Exception e) {
5567
Log.Error(e, $"failed to load dsconfig from {configPath}");
56-
return;
68+
return false;
5769
}
5870
//Load language id if needed
5971
if(dsConfig.use_lang_id){
@@ -83,7 +95,7 @@ public override void SetSinger(USinger singer) {
8395
linguisticModel = new InferenceSession(linguisticModelBytes);
8496
} catch (Exception e) {
8597
Log.Error(e, $"failed to load linguistic model from {linguisticModelPath}");
86-
return;
98+
return false;
8799
}
88100
var durationModelPath = Path.Join(rootPath, dsConfig.dur);
89101
try {
@@ -92,8 +104,9 @@ public override void SetSinger(USinger singer) {
92104
durationModel = new InferenceSession(durationModelBytes);
93105
} catch (Exception e) {
94106
Log.Error(e, $"failed to load duration model from {durationModelPath}");
95-
return;
107+
return false;
96108
}
109+
return true;
97110
}
98111

99112
protected virtual IG2p LoadG2p(string rootPath) {

0 commit comments

Comments
 (0)