11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using Newtonsoft . Json ;
56using Newtonsoft . Json . Linq ;
@@ -39,15 +40,97 @@ public class VoicevoxQueryNotes {
3940 public class VoicevoxQueryMain {
4041 public List < VoicevoxQueryNotes > notes = new List < VoicevoxQueryNotes > ( ) ;
4142 }
43+ public class Phoneme_list {
44+ public string [ ] vowels = "a i u e o A I U E O N pau cl" . Split ( ) ;
45+ public string [ ] consonants = "b by ch d dy f g gw gy h hy j k kw ky m my n ny p py r ry s sh t ts ty v w y z" . Split ( ) ;
46+ public Dictionary < string , string > kanas = new Dictionary < string , string > ( ) ;
47+ public Dictionary < string , string > paus = new Dictionary < string , string > ( ) ;
48+ public Phoneme_list ( ) {
49+ var kanaGroups = new List < string [ ] > {
50+ "あ ば びゃ ちゃ だ でゃ ふぁ が ぐゎ ぎゃ は ひゃ じゃ か くゎ きゃ ま みゃ な にゃ ぱ ぴゃ ら りゃ さ しゃ た つぁ てゃ ゔぁ わ や ざ" . Split ( ) ,
51+ "い び ち ぢ でぃ ふぃ ぎ ひ じ き み に ぴ り すぃ し てぃ つぃ ゔぃ うぃ ずぃ" . Split ( ) ,
52+ "う ぶ びゅ ちゅ どぅ でゅ ふ ぐ ぎゅ ひゅ じゅ く きゅ む みゅ ぬ にゅ ぷ ぴゅ る りゅ す しゅ つ つ てゅ ゔ ゆ ず" . Split ( ) ,
53+ "え べ びぇ ちぇ で でぇ ふぇ げ ぎぇ へ ひぇ じぇ け きぇ め みぇ ね にぇ ぺ ぴぇ れ りぇ せ しぇ て つぇ ゔぇ うぇ いぇ ぜ" . Split ( ) ,
54+ "お ぼ びょ ちょ ど でょ ふぉ ご ぎょ ほ ひょ じょ こ きょ も みょ の にょ ぽ ぴょ ろ りょ そ しょ と つぉ てょ ゔぉ を よ ぞ" . Split ( ) ,
55+ "ん ン" . Split ( ) ,
56+ "っ ッ" . Split ( )
57+ } ;
58+
59+ foreach ( var group in kanaGroups ) {
60+ foreach ( var kana in group ) {
61+ if ( ! kanas . ContainsKey ( kana ) ) {
62+ kanas . Add ( kana . Normalize ( ) , group [ 0 ] . Normalize ( ) ) ;
63+ }
64+ }
65+ }
66+ string [ ] pauseGroups = "R pau AP SP" . Split ( ) ;
67+
68+ foreach ( string group in pauseGroups ) {
69+ if ( ! paus . ContainsKey ( group ) ) {
70+ paus . Add ( group . Normalize ( ) , pauseGroups [ 0 ] . Normalize ( ) ) ;
71+ }
72+ }
73+ }
74+ }
75+
76+ public class Dictionary_list {
77+ public Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
78+
79+ public void Loaddic ( string location ) {
80+ try {
81+ var parentDirectory = Directory . GetParent ( location ) . ToString ( ) ;
82+ var yamlPath = Path . Join ( parentDirectory , "dictionary.yaml" ) ;
83+ if ( File . Exists ( yamlPath ) ) {
84+ var yamlTxt = File . ReadAllText ( yamlPath ) ;
85+ var yamlObj = Yaml . DefaultDeserializer . Deserialize < Dictionary < string , List < Dictionary < string , string > > > > ( yamlTxt ) ;
86+ var list = yamlObj [ "list" ] ;
87+ dict = new Dictionary < string , string > ( ) ;
88+
89+ foreach ( var item in list ) {
90+ foreach ( var pair in item ) {
91+ dict [ pair . Key ] = pair . Value ;
92+ }
93+ }
94+
95+ }
96+ } catch ( Exception e ) {
97+ Log . Error ( $ "Failed to read dictionary file. : { e } ") ;
98+ }
99+ }
100+ public string Notetodic ( Note [ ] [ ] notes , int index ) {
101+ if ( dict . TryGetValue ( notes [ index ] [ 0 ] . lyric , out var lyric_ ) ) {
102+ if ( string . IsNullOrEmpty ( lyric_ ) ) {
103+ return "" ;
104+ }
105+ return lyric_ ;
106+ }
107+ return notes [ index ] [ 0 ] . lyric ;
108+ }
109+
110+ public string Lyrictodic ( string lyric ) {
111+ if ( dict . TryGetValue ( lyric , out var lyric_ ) ) {
112+ if ( string . IsNullOrEmpty ( lyric_ ) ) {
113+ return "" ;
114+ }
115+ return lyric_ ;
116+ }
117+ return lyric ;
118+ }
119+
120+ public bool IsDic ( string lyric ) {
121+ return dict . ContainsKey ( lyric ) ;
122+ }
123+ }
42124
43125
44- internal static class VoicevoxUtils {
126+ public static class VoicevoxUtils {
45127 public const string VOLC = "volc" ;
46128 public const int headS = 1 ;
47129 public const int tailS = 1 ;
48130 public const double fps = 93.75 ;
49131 public const string defaultID = "6000" ;
50132 public static Dictionary_list dic = new Dictionary_list ( ) ;
133+ public static Phoneme_list phoneme_List = new Phoneme_list ( ) ;
51134
52135 public static VoicevoxNote VoicevoxVoiceBase ( VoicevoxQueryMain qNotes , string id ) {
53136 var queryurl = new VoicevoxURL ( ) { method = "POST" , path = "/sing_frame_audio_query" , query = new Dictionary < string , string > { { "speaker" , id } } , body = JsonConvert . SerializeObject ( qNotes ) } ;
@@ -83,11 +166,11 @@ public static VoicevoxQueryMain NoteGroupsToVoicevox(Note[][] notes, TimeAxis ti
83166 string lyric = dic . Notetodic ( notes , index ) ;
84167 int length = ( int ) Math . Round ( ( ( timeAxis . TickPosToMsPos ( notes [ index ] . Sum ( n => n . duration ) ) / 1000f ) * VoicevoxUtils . fps ) , MidpointRounding . AwayFromZero ) ;
85168 //Avoid synthesis without at least two frames.
86- if ( length < 2 ) {
169+ if ( length < 2 ) {
87170 length = 2 ;
88171 }
89172 int ? tone = null ;
90- if ( ! string . IsNullOrEmpty ( lyric ) || VoicevoxUtils . IsPau ( lyric ) ) {
173+ if ( ! string . IsNullOrEmpty ( lyric ) ) {
91174 if ( notes [ index ] [ 0 ] . phonemeAttributes != null ) {
92175 if ( notes [ index ] [ 0 ] . phonemeAttributes . Length > 0 ) {
93176 tone = notes [ index ] [ 0 ] . tone + notes [ index ] [ 0 ] . phonemeAttributes [ 0 ] . toneShift ;
@@ -97,6 +180,8 @@ public static VoicevoxQueryMain NoteGroupsToVoicevox(Note[][] notes, TimeAxis ti
97180 } else {
98181 tone = notes [ index ] [ 0 ] . tone ;
99182 }
183+ } else {
184+ lyric = "" ;
100185 }
101186 qnotes . notes . Add ( new VoicevoxQueryNotes {
102187 lyric = lyric ,
@@ -146,21 +231,13 @@ public static double[] SampleCurve(RenderPhrase phrase, float[] curve, double de
146231 return result ;
147232 }
148233
149-
150- public static bool IsHiraKana ( string s ) {
151- foreach ( char c in s . ToCharArray ( ) ) {
152- if ( ! ( '\u3041 ' <= c && c <= '\u309F ' ) || ( '\u30A0 ' <= c && c <= '\u30FF ' ) || c == '\u30FC ' || c == '\u30A0 ' ) {
153- return false ;
154- }
155- }
156- return true ;
234+ public static bool IsPau ( string s ) {
235+ return phoneme_List . paus . ContainsKey ( s ) ;
157236 }
158237
159- public static bool IsPau ( string s ) {
160- if ( s . EndsWith ( "R" ) || s . ToLower ( ) . EndsWith ( "pau" ) || s . EndsWith ( "AP" ) || s . EndsWith ( "SP" ) ) {
161- return true ;
162- }
163- return false ;
238+ public static bool TryGetPau ( string s , out string str ) {
239+ phoneme_List . paus . TryGetValue ( s , out str ) ;
240+ return phoneme_List . paus . ContainsKey ( s ) ;
164241 }
165242
166243 public static string getBaseSingerID ( VoicevoxSinger singer ) {
0 commit comments