22using System ;
33using System . Collections . Generic ;
44using System . IO ;
5+ using System . Linq ;
56
67using OpenUtau . Api ;
78
89namespace OpenUtau . Core . DiffSinger
910{
10- public class G2pReplacementsData {
11+ class DiffSingerG2pDictionaryData : G2pDictionaryData {
1112 public struct Replacement {
1213 public string from ;
1314 public string to ;
1415 }
1516 public Replacement [ ] ? replacements ;
16-
17- public static G2pReplacementsData Load ( string text ) {
18- return OpenUtau . Core . Yaml . DefaultDeserializer . Deserialize < G2pReplacementsData > ( text ) ;
19- }
2017
21- public Dictionary < string , string > toDict ( ) {
18+ public Dictionary < string , string > replacementsDict ( ) {
2219 var dict = new Dictionary < string , string > ( ) ;
2320 if ( replacements != null ) {
2421 foreach ( var r in replacements ) {
@@ -39,7 +36,7 @@ public abstract class DiffSingerG2pPhonemizer : DiffSingerBasePhonemizer
3936 protected virtual string [ ] GetBaseG2pVowels ( ) => new string [ ] { } ;
4037 protected virtual string [ ] GetBaseG2pConsonants ( ) => new string [ ] { } ;
4138
42- protected override IG2p LoadG2p ( string rootPath ) {
39+ protected override IG2p LoadG2p ( string rootPath , bool useLangId = false ) {
4340 //Each phonemizer has a delicated dictionary name, such as dsdict-en.yaml, dsdict-ru.yaml.
4441 //If this dictionary exists, load it.
4542 //If not, load dsdict.yaml.
@@ -54,8 +51,9 @@ protected override IG2p LoadG2p(string rootPath) {
5451 if ( File . Exists ( dictionaryPath ) ) {
5552 try {
5653 string dictText = File . ReadAllText ( dictionaryPath ) ;
57- replacements = G2pReplacementsData . Load ( dictText ) . toDict ( ) ;
58- g2pBuilder . Load ( dictText ) ;
54+ var dictData = Yaml . DefaultDeserializer . Deserialize < DiffSingerG2pDictionaryData > ( dictText ) ;
55+ g2pBuilder . Load ( dictData ) ;
56+ replacements = dictData . replacementsDict ( ) ;
5957 } catch ( Exception e ) {
6058 Log . Error ( e , $ "Failed to load { dictionaryPath } ") ;
6159 }
@@ -79,6 +77,15 @@ protected override IG2p LoadG2p(string rootPath) {
7977 foreach ( var c in GetBaseG2pConsonants ( ) ) {
8078 phonemeSymbols [ c ] = false ;
8179 }
80+ if ( useLangId ) {
81+ //For diffsinger multi dict voicebanks, the replacements of g2p phonemes default to the <langcode>/<phoneme>
82+ var langCode = GetLangCode ( ) ;
83+ foreach ( var ph in GetBaseG2pVowels ( ) . Concat ( GetBaseG2pConsonants ( ) ) ) {
84+ if ( ! replacements . ContainsKey ( ph ) ) {
85+ replacements [ ph ] = langCode + "/" + ph ;
86+ }
87+ }
88+ }
8289 foreach ( var from in replacements . Keys ) {
8390 var to = replacements [ from ] ;
8491 if ( baseG2p . IsValidSymbol ( to ) ) {
0 commit comments