@@ -36,6 +36,9 @@ public DsPitch(string rootPath)
3636 dsConfig = Core . Yaml . DefaultDeserializer . Deserialize < DsConfig > (
3737 File . ReadAllText ( Path . Combine ( rootPath , "dsconfig.yaml" ) ,
3838 System . Text . Encoding . UTF8 ) ) ;
39+ if ( dsConfig . pitch == null ) {
40+ throw new Exception ( "This voicebank doesn't contain a pitch model" ) ;
41+ }
3942 //Load phonemes list
4043 string phonemesPath = Path . Combine ( rootPath , dsConfig . phonemes ) ;
4144 phonemes = File . ReadLines ( phonemesPath , Encoding . UTF8 ) . ToList ( ) ;
@@ -52,17 +55,16 @@ public DsPitch(string rootPath)
5255 }
5356
5457 protected IG2p LoadG2p ( string rootPath ) {
55- var g2ps = new List < IG2p > ( ) ;
5658 // Load dictionary from singer folder.
5759 string file = Path . Combine ( rootPath , "dsdict.yaml" ) ;
58- if ( File . Exists ( file ) ) {
59- try {
60- g2ps . Add ( G2pDictionary . NewBuilder ( ) . Load ( File . ReadAllText ( file ) ) . Build ( ) ) ;
61- } catch ( Exception e ) {
62- Log . Error ( e , $ "Failed to load { file } ") ;
63- }
60+ if ( ! File . Exists ( file ) ) {
61+ throw new Exception ( $ "File not found: { file } ") ;
6462 }
65- return new G2pFallbacks ( g2ps . ToArray ( ) ) ;
63+ var g2pBuilder = G2pDictionary . NewBuilder ( ) . Load ( File . ReadAllText ( file ) ) ;
64+ //SP and AP should always be vowel
65+ g2pBuilder . AddSymbol ( "SP" , true ) ;
66+ g2pBuilder . AddSymbol ( "AP" , true ) ;
67+ return g2pBuilder . Build ( ) ;
6668 }
6769
6870 public DiffSingerSpeakerEmbedManager getSpeakerEmbedManager ( ) {
@@ -77,6 +79,14 @@ void SetRange<T>(T[] list, T value, int startIndex, int endIndex){
7779 list [ i ] = value ;
7880 }
7981 }
82+
83+ int PhonemeTokenize ( string phoneme ) {
84+ int result = phonemes . IndexOf ( phoneme ) ;
85+ if ( result < 0 ) {
86+ throw new Exception ( $ "Phoneme \" { phoneme } \" isn't supported by pitch model. Please check { Path . Combine ( rootPath , dsConfig . phonemes ) } ") ;
87+ }
88+ return result ;
89+ }
8090
8191 public RenderPitchResult Process ( RenderPhrase phrase ) {
8292 var startMs = Math . Min ( phrase . notes [ 0 ] . positionMs , phrase . phones [ 0 ] . positionMs ) - headMs ;
@@ -86,9 +96,10 @@ public RenderPitchResult Process(RenderPhrase phrase){
8696 //Linguistic Encoder
8797 var linguisticInputs = new List < NamedOnnxValue > ( ) ;
8898 var tokens = phrase . phones
89- . Select ( p => ( Int64 ) phonemes . IndexOf ( p . phoneme ) )
90- . Prepend ( ( Int64 ) phonemes . IndexOf ( "SP" ) )
91- . Append ( ( Int64 ) phonemes . IndexOf ( "SP" ) )
99+ . Select ( p => p . phoneme )
100+ . Prepend ( "SP" )
101+ . Append ( "SP" )
102+ . Select ( x => ( Int64 ) PhonemeTokenize ( x ) )
92103 . ToArray ( ) ;
93104 var ph_dur = phrase . phones
94105 . Select ( p=> ( int ) Math . Round ( p . endMs / frameMs ) - ( int ) Math . Round ( p . positionMs / frameMs ) )
@@ -104,6 +115,9 @@ public RenderPitchResult Process(RenderPhrase phrase){
104115 var vowelIds = Enumerable . Range ( 0 , phrase . phones . Length )
105116 . Where ( i=> g2p . IsVowel ( phrase . phones [ i ] . phoneme ) )
106117 . ToArray ( ) ;
118+ if ( vowelIds . Length == 0 ) {
119+ vowelIds = new int [ ] { phrase . phones . Length - 1 } ;
120+ }
107121 var word_div = vowelIds . Zip ( vowelIds . Skip ( 1 ) , ( a , b ) => ( Int64 ) ( b - a ) )
108122 . Prepend ( vowelIds [ 0 ] + 1 )
109123 . Append ( phrase . phones . Length - vowelIds [ ^ 1 ] + 1 )
0 commit comments