-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathSyllableBasedPhonemizer.cs
More file actions
1361 lines (1191 loc) · 60.4 KB
/
Copy pathSyllableBasedPhonemizer.cs
File metadata and controls
1361 lines (1191 loc) · 60.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using OpenUtau.Api;
using OpenUtau.Core.Ustx;
using System.Linq;
using System.IO;
using Serilog;
using System.Threading.Tasks;
using static OpenUtau.Api.Phonemizer;
using System.Collections;
using OpenUtau.Core;
namespace OpenUtau.Plugin.Builtin {
/// <summary>
/// Use this class as a base for easier phonemizer configuration. Works for vb styles like VCV, VCCV, CVC etc;
///
/// - Supports dictionary;
/// - Automatically align phonemes to notes;
/// - Supports syllable extension;
/// - Automatically calculates transition phonemes length, with constants by default,
/// but there is a pre-created function to use Oto value;
/// - The transition length is scaled based on Tempo and note length.
///
/// Note that here "Vowel" means "stretchable phoneme" and "Consonant" means "non-stretchable phoneme".
///
/// So if a diphthong is represented with several phonemes, like English "byke" -> [b a y k],
/// then [a] as a stretchable phoneme would be a "Vowel", and [y] would be a "Consonant".
///
/// Some reclists have consonants that also may behave as vowels, like long "M" and "N". They are "Vowels".
///
/// If your oto hase same symbols for them, like "n" for stretchable "n" from a long note and "n" from CV,
/// then you can use a vitrual symbol [N], and then replace it with [n] in ValidateAlias().
/// </summary>
public abstract class SyllableBasedPhonemizer : Phonemizer {
/// <summary>
/// Syllable is [V] [C..] [V]
/// </summary>
protected struct Syllable {
/// <summary>
/// vowel from previous syllable for VC
/// </summary>
public string prevV;
/// <summary>
/// CCs, may be empty
/// </summary>
public string[] cc;
/// <summary>
/// "base" note. May not actually be vowel, if only consonants way provided
/// </summary>
public string v;
/// <summary>
/// Start position for vowel. All VC CC goes before this position
/// </summary>
public int position;
/// <summary>
/// previous note duration, i.e. this is container for VC and CC notes
/// </summary>
public int duration;
/// <summary>
/// Tone for VC and CC
/// </summary>
public int tone;
/// <summary>
/// Other phoneme attributes for VC and CC
/// </summary>
public PhonemeAttributes[] attr;
/// <summary>
/// tone for base "vowel" phoneme
/// </summary>
public int vowelTone;
/// <summary>
/// Other phoneme attributes for base "vowel" phoneme
/// </summary>
public PhonemeAttributes[] vowelAttr;
/// <summary>
/// 0 if no consonants are taken from previous word;
/// 1 means first one is taken from previous word, etc.
/// </summary>
public int prevWordConsonantsCount;
/// <summary>
/// If true, you may use alias extension instead of VV, by putting the phoneme as null if vowels match.
/// If you do this when canAliasBeExtended == false, the note will produce no phoneme and there will be a break.
/// Use CanMakeAliasExtension() to pass all checks if alias extension is possible
/// </summary>
public bool canAliasBeExtended;
// helpers
public bool IsStartingV => prevV == "" && cc.Length == 0;
public bool IsVV => prevV != "" && cc.Length == 0;
public bool IsStartingCV => prevV == "" && cc.Length > 0;
public bool IsVCV => prevV != "" && cc.Length > 0;
public bool IsStartingCVWithOneConsonant => prevV == "" && cc.Length == 1;
public bool IsVCVWithOneConsonant => prevV != "" && cc.Length == 1;
public bool IsStartingCVWithMoreThanOneConsonant => prevV == "" && cc.Length > 1;
public bool IsVCVWithMoreThanOneConsonant => prevV != "" && cc.Length > 1;
public string[] PreviousWordCc => cc.Take(prevWordConsonantsCount).ToArray();
public string[] CurrentWordCc => cc.Skip(prevWordConsonantsCount).ToArray();
public override string ToString() {
return $"({prevV}) {(cc != null ? string.Join(" ", cc) : "")} {v}";
}
}
protected struct Ending {
/// <summary>
/// vowel from the last syllable to make VC
/// </summary>
public string prevV;
/// <summary>
/// actuall CC at the ending
/// </summary>
public string[] cc;
/// <summary>
/// The exact lyric/symbol of the tail (e.g., "R", "br", "-", etc.)
/// </summary>
public string tail;
public bool HasTail => !string.IsNullOrEmpty(tail);
/// <summary>
/// last note position + duration, all phonemes must be less than this
/// </summary>
public int position;
/// <summary>
/// last syllable length, max container for all VC CC C-
/// </summary>
public int duration;
/// <summary>
/// the tone from last syllable, for all ending phonemes
/// </summary>
public int tone;
/// <summary>
/// Other phoneme attributes from last syllable
/// </summary>
public PhonemeAttributes[] attr;
// helpers
public bool IsEndingV => cc.Length == 0;
public bool IsEndingVC => cc.Length > 0;
public bool IsEndingVCWithOneConsonant => cc.Length == 1;
public bool IsEndingVCWithMoreThanOneConsonant => cc.Length > 1;
public override string ToString() {
return $"({prevV}) {(cc != null ? string.Join(" ", cc) : "")}";
}
}
public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, Note[] prevNeighbours) {
error = "";
var mainNote = notes[0];
if (mainNote.lyric.StartsWith(FORCED_ALIAS_SYMBOL)) {
return MakeForcedAliasResult(mainNote);
}
if (hasDictionary && isDictionaryLoading) {
return MakeSimpleResult("");
}
var syllables = MakeSyllables(notes, MakeEnding(prevNeighbours));
if (syllables == null) {
return HandleError();
}
var phonemes = new List<Phoneme>();
foreach (var syllable in syllables) {
var modifiedSyllable = ApplyBoundaryReplacements(syllable);
if (tails.Contains(modifiedSyllable.v)) {
var ending = new Ending {
prevV = modifiedSyllable.prevV,
cc = modifiedSyllable.cc,
tail = modifiedSyllable.v,
position = modifiedSyllable.position,
duration = modifiedSyllable.duration,
tone = modifiedSyllable.tone,
attr = modifiedSyllable.attr
};
var endingPhonemes = ProcessEnding(ending);
if (endingPhonemes != null) {
phonemes.AddRange(MakePhonemes(endingPhonemes, modifiedSyllable.duration, modifiedSyllable.position, false));
}
continue;
}
phonemes.AddRange(MakePhonemes(ProcessSyllable(modifiedSyllable), modifiedSyllable.duration, modifiedSyllable.position, false));
}
if (!nextNeighbour.HasValue) {
var tryEnding = MakeEnding(notes);
if (tryEnding.HasValue) {
var ending = tryEnding.Value;
if (nextNeighbour.HasValue && tails.Contains(nextNeighbour.Value.lyric)) {
ending.tail = nextNeighbour.Value.lyric;
}
var modifiedEnding = ApplyBoundaryReplacements(ending);
var endingPhonemes = ProcessEnding(modifiedEnding);
if (endingPhonemes != null) {
phonemes.AddRange(MakePhonemes(endingPhonemes, modifiedEnding.duration, modifiedEnding.position, true));
}
}
}
return new Result() {
phonemes = AssignAllAffixes(phonemes, notes, prevNeighbours)
};
}
protected virtual Phoneme[] AssignAllAffixes(List<Phoneme> phonemes, Note[] notes, Note[] prevs) {
int noteIndex = 0;
for (int i = 0; i < phonemes.Count; i++) {
var attr = notes[0].phonemeAttributes?.FirstOrDefault(attr => attr.index == i) ?? default;
string alt = attr.alternate?.ToString() ?? string.Empty;
string color = attr.voiceColor;
int toneShift = attr.toneShift;
var phoneme = phonemes[i];
while (noteIndex < notes.Length - 1 && notes[noteIndex].position - notes[0].position < phoneme.position) {
noteIndex++;
}
var noteStartPosition = notes[noteIndex].position - notes[0].position;
int tone = (prevs != null && prevs.Length > 0 && phoneme.position < noteStartPosition) ?
prevs.Last().tone : (noteIndex > 0 && phoneme.position < noteStartPosition) ?
notes[noteIndex - 1].tone : notes[noteIndex].tone;
var validatedAlias = phoneme.phoneme;
if (validatedAlias != null) {
validatedAlias = ValidateAliasIfNeeded(validatedAlias, tone + toneShift);
validatedAlias = MapPhoneme(validatedAlias, tone + toneShift, color, alt, singer);
phoneme.phoneme = validatedAlias;
} else {
phoneme.phoneme = null;
phoneme.position = 0;
}
phonemes[i] = phoneme;
}
return phonemes.ToArray();
}
private Result HandleError() {
return new Result {
phonemes = new Phoneme[] {
new Phoneme() {
phoneme = error
}
}
};
}
public override void SetSinger(USinger singer) {
if (this.singer != singer || this.localYamlGeneration != globalYamlGeneration) {
this.singer = singer;
this.localYamlGeneration = globalYamlGeneration;
if (this.singer == null || !this.singer.Loaded) {
return;
}
if (string.IsNullOrEmpty(YamlFileName)) {
if (backupVowels != null) this.vowels = backupVowels;
else this.vowels = GetVowels();
if (backupConsonants != null) this.consonants = backupConsonants;
else this.consonants = GetConsonants();
if (backupDictionaryReplacements != null) {
dictionaryReplacements.Clear();
foreach (var kvp in backupDictionaryReplacements) {
dictionaryReplacements[kvp.Key] = kvp.Value;
}
}
if (!hasDictionary) {
ReadDictionaryAndInit();
} else {
Init();
}
return;
}
string file = null;
if (singer != null && singer.Found && singer.Loaded && !string.IsNullOrEmpty(singer.Location)) {
file = Path.Combine(singer.Location, YamlFileName);
} else if (!string.IsNullOrEmpty(PluginDir)) {
file = Path.Combine(PluginDir, YamlFileName);
}
SetupYamlWatcher(file);
if (!string.IsNullOrEmpty(file)) {
bool shouldWriteTemplate = false;
bool shouldBackupOldFile = false;
if (File.Exists(file)) {
if (YamlTemplate != null && !string.IsNullOrEmpty(YamlVersion)) {
try {
var checkData = Core.Yaml.DefaultDeserializer.Deserialize<YAMLData>(File.ReadAllText(file));
string currentVersion = checkData?.version?.Trim() ?? "";
if (string.IsNullOrEmpty(currentVersion) || currentVersion != YamlVersion) {
shouldWriteTemplate = true;
shouldBackupOldFile = true;
}
} catch (Exception ex) {
Log.Error(ex, $"Failed to read version from '{file}'. Backing up and resetting to template...");
shouldWriteTemplate = true;
shouldBackupOldFile = true;
}
}
} else if (YamlTemplate != null) {
shouldWriteTemplate = true;
}
if (shouldBackupOldFile && File.Exists(file)) {
try {
string backupFile = Path.Combine(Path.GetDirectoryName(file), $"{Path.GetFileNameWithoutExtension(YamlFileName)}_backup{Path.GetExtension(YamlFileName)}");
if (File.Exists(backupFile)) File.Delete(backupFile);
File.Move(file, backupFile);
Log.Information($"Old {YamlFileName} backed up to {backupFile}");
} catch (Exception e) {
Log.Error(e, $"Failed to back up {YamlFileName}");
}
}
if (shouldWriteTemplate) {
try {
File.WriteAllBytes(file, YamlTemplate);
Log.Information($"'{file}' created or updated to version {YamlVersion ?? "default"}");
} catch (Exception e) {
Log.Error(e, $"Failed to write template to {file}");
}
}
if (File.Exists(file)) {
try {
var data = Core.Yaml.DefaultDeserializer.Deserialize<YAMLData>(File.ReadAllText(file)) ?? new YAMLData();
if (backupVowels == null) backupVowels = GetVowels() ?? Array.Empty<string>();
if (backupConsonants == null) backupConsonants = GetConsonants() ?? Array.Empty<string>();
var yamlVowels = data.symbols?.Where(s => s.type == "vowel" || s.type == "diphthong").Select(s => s.symbol).ToArray() ?? Array.Empty<string>();
vowels = backupVowels.Concat(yamlVowels).Distinct().ToArray();
tails = new string[] { "-", "R" }.Concat(data.symbols?.Where(s => s.type == "tail").Select(s => s.symbol) ?? Array.Empty<string>()).Distinct().ToArray();
fricative = data.symbols?.Where(s => s.type == "fricative").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
aspirate = data.symbols?.Where(s => s.type == "aspirate").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
semivowel = data.symbols?.Where(s => s.type == "semivowel").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
liquid = data.symbols?.Where(s => s.type == "liquid").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
nasal = data.symbols?.Where(s => s.type == "nasal").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
stop = data.symbols?.Where(s => s.type == "stop").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
tap = data.symbols?.Where(s => s.type == "tap").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
affricate = data.symbols?.Where(s => s.type == "affricate").Select(s => s.symbol).Distinct().ToArray() ?? Array.Empty<string>();
var yamlConsonants = fricative.Concat(aspirate).Concat(semivowel).Concat(liquid).Concat(nasal).Concat(stop).Concat(tap).Concat(affricate).ToArray();
consonants = backupConsonants.Concat(yamlConsonants).Distinct().ToArray();
PhonemeOverrides = data.timings?.ToDictionary(t => t.symbol, t => t.value) ?? new Dictionary<string, double>();
if (backupDictionaryReplacements == null) {
backupDictionaryReplacements = new Dictionary<string, string>(dictionaryReplacements);
}
dictionaryReplacements.Clear();
foreach (var kvp in backupDictionaryReplacements) {
dictionaryReplacements[kvp.Key] = kvp.Value;
}
mergingReplacements.Clear();
splittingReplacements.Clear();
if (data?.replacements != null && data.replacements.Any()) {
foreach (var replacement in data.replacements) {
string ruleScope = string.IsNullOrEmpty(replacement.where) ? "inside" : replacement.where.ToLowerInvariant();
if (replacement.from is IEnumerable<object> fromList) {
string[] fromArray = fromList.Select(item => item.ToString()).ToArray();
if (replacement.to is string toString) mergingReplacements.Add(new Replacement { from = fromArray, to = toString, where = ruleScope });
else if (replacement.to is IEnumerable<object> toList) splittingReplacements.Add(new Replacement { from = fromArray, to = toList.Select(item => item.ToString()).ToArray(), where = ruleScope });
} else if (replacement.from is string fromString) {
if (replacement.to is string toString) dictionaryReplacements[fromString] = toString;
else if (replacement.to is IEnumerable<object> toList) splittingReplacements.Add(new Replacement { from = fromString, to = toList.Select(item => item.ToString()).ToArray(), where = ruleScope });
}
}
}
yamlFallbacks.Clear();
if (data?.fallbacks != null) {
yamlFallbacks.Clear();
foreach (var df in data.fallbacks) {
if (!string.IsNullOrEmpty(df.from) && !string.IsNullOrEmpty(df.to)) {
yamlFallbacks[df.from] = df.to;
}
}
}
} catch (Exception ex) {
Log.Error($"Failed to parse {YamlFileName}: {ex.Message}");
}
}
}
if (!hasDictionary) {
ReadDictionaryAndInit();
} else {
Init();
}
}
}
protected USinger singer;
protected bool hasDictionary => dictionaries.ContainsKey(GetType());
protected IG2p dictionary => dictionaries[GetType()];
protected bool isDictionaryLoading => dictionaries[GetType()] == null;
protected double TransitionBasicLengthMs => 100;
public static YamlWatcher yamlWatcher;
public static string currentlyWatchedYaml;
public static int globalYamlGeneration = 0;
public int localYamlGeneration = 0;
private Dictionary<Type, IG2p> dictionaries = new Dictionary<Type, IG2p>();
private const string FORCED_ALIAS_SYMBOL = "?";
private string error = "";
private readonly string[] wordSeparators = new[] { " ", "_" };
private readonly string[] wordSeparator = new[] { " " };
/// <summary>
/// Returns list of vowels
/// </summary>
/// <returns></returns>
protected abstract string[] GetVowels();
/// <summary>
/// Returns list of consonants. Only needed if there is a dictionary
/// </summary>
/// <returns></returns>
protected virtual string[] GetConsonants() {
throw new NotImplementedException();
}
/// <summary>
/// returns phoneme symbols, like, VCV, or VC + CV, or -CV, etc
/// </summary>
/// <returns>List of phonemes</returns>
protected abstract List<string> ProcessSyllable(Syllable syllable);
/// <summary>
/// phoneme symbols for ending, like, V-, or VC-, or VC+C
/// </summary>
protected abstract List<string> ProcessEnding(Ending ending);
/// <summary>
/// simple alias to alias fallback
/// </summary>
/// <returns></returns>
protected virtual Dictionary<string, string> GetAliasesFallback() { return null; }
/// <summary>
/// Use to some custom init, if needed
/// </summary>
protected virtual void Init() { }
/// <summary>
/// Dictionary name. Must be stored in Dictionaries folder.
/// If missing or can't be read, phonetic input is used
/// </summary>
/// <returns></returns>
protected virtual string GetDictionaryName() { return null; }
/// <summary>
/// extracts array of phoneme symbols from note. Override for procedural dictionary or something
/// reads from dictionary if provided
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
protected virtual string[] GetSymbols(Note note) {
string[] getSymbolsRaw(string lyrics) {
if (lyrics == null) {
return new string[0];
} else return lyrics.Split(" ");
}
if (tails.Contains(note.lyric)) {
return new string[] { note.lyric };
}
if (hasDictionary) {
if (!string.IsNullOrEmpty(note.phoneticHint)) {
return getSymbolsRaw(note.phoneticHint);
}
var result = new List<string>();
foreach (var subword in note.lyric.Trim().ToLowerInvariant().Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries)) {
var subResult = dictionary.Query(subword);
if (subResult == null) {
Log.Warning($"Subword '{subword}' from word '{note.lyric}' can't be found in the dictionary");
subResult = HandleWordNotFound(note);
if (subResult == null) {
return null;
}
} else {
for (int i = 0; i < subResult.Length; i++) {
string phoneme = subResult[i];
if (dictionaryReplacements.TryGetValue(phoneme, out string replaced)) {
subResult[i] = replaced;
} else if (dictionaryReplacements.TryGetValue(subResult[i], out string replacedExact)) {
subResult[i] = replacedExact;
}
}
}
result.AddRange(subResult);
}
return result.ToArray();
} else {
return getSymbolsRaw(note.lyric);
}
}
/// <summary>
/// Instead of changing symbols in cmudict itself for each reclist,
/// you may leave it be and provide symbol replacements with this method.
/// </summary>
/// <returns></returns>
protected virtual Dictionary<string, string> GetDictionaryPhonemesReplacement() {
return dictionaryReplacements ?? new Dictionary<string, string>();
}
private string[] backupVowels = null;
private string[] backupConsonants = null;
private Dictionary<string, string> backupDictionaryReplacements = null;
/// <summary>
/// separates symbols to syllables, without an ending.
/// </summary>
/// <param name="inputNotes"></param>
/// <param name="prevWord"></param>
/// <returns></returns>
protected virtual Syllable[] MakeSyllables(Note[] inputNotes, Ending? prevEnding) {
(var symbols, var vowelIds, var notes) = GetSymbolsAndVowels(inputNotes);
if (symbols == null || vowelIds == null || notes == null) {
return null;
}
var firstVowelId = vowelIds[0];
if (notes.Length < vowelIds.Length) {
error = $"Not enough extension notes, {vowelIds.Length - notes.Length} more expected";
return null;
}
var syllables = new Syllable[vowelIds.Length];
// Making the first syllable
if (prevEnding.HasValue) {
var prevEndingValue = prevEnding.Value;
var beginningCc = prevEndingValue.cc.ToList();
beginningCc.AddRange(symbols.Take(firstVowelId));
// If we had a prev neighbour ending, let's take info from it
syllables[0] = new Syllable() {
prevV = prevEndingValue.prevV,
cc = beginningCc.ToArray(),
v = symbols[firstVowelId],
tone = prevEndingValue.tone,
attr = prevEndingValue.attr,
duration = prevEndingValue.duration,
position = 0,
vowelTone = notes[0].tone,
vowelAttr = notes[0].phonemeAttributes,
prevWordConsonantsCount = prevEndingValue.cc.Count()
};
} else {
// there is only empty space before us
syllables[0] = new Syllable() {
prevV = "",
cc = symbols.Take(firstVowelId).ToArray(),
v = symbols[firstVowelId],
tone = notes[0].tone,
attr = notes[0].phonemeAttributes,
duration = -1,
position = 0,
vowelTone = notes[0].tone,
vowelAttr = notes[0].phonemeAttributes
};
}
// normal syllables after the first one
var noteI = 1;
var ccs = new List<string>();
var position = 0;
var lastSymbolI = firstVowelId + 1;
for (; lastSymbolI < symbols.Length & noteI < notes.Length; lastSymbolI++) {
if (!vowelIds.Contains(lastSymbolI)) {
ccs.Add(symbols[lastSymbolI]);
} else {
position += notes[noteI - 1].duration;
syllables[noteI] = new Syllable() {
prevV = syllables[noteI - 1].v,
cc = ccs.ToArray(),
v = symbols[lastSymbolI],
tone = notes[noteI - 1].tone,
attr = notes[noteI - 1].phonemeAttributes,
duration = notes[noteI - 1].duration,
position = position,
vowelTone = notes[noteI].tone,
vowelAttr = notes[noteI].phonemeAttributes,
canAliasBeExtended = true // for all not-first notes is allowed
};
ccs = new List<string>();
noteI++;
}
}
return syllables;
}
/// <summary>
/// extracts word ending
/// </summary>
/// <param inputNotes="notes"></param>
/// <returns></returns>
protected Ending? MakeEnding(Note[] inputNotes) {
if (inputNotes == null || inputNotes.Length == 0 || inputNotes[0].lyric.StartsWith(FORCED_ALIAS_SYMBOL)) {
return null;
}
(var symbols, var vowelIds, var notes) = GetSymbolsAndVowels(inputNotes);
if (symbols == null || vowelIds == null || notes == null) {
return null;
}
return new Ending() {
prevV = symbols[vowelIds.Last()],
cc = symbols.Skip(vowelIds.Last() + 1).ToArray(),
tone = notes.Last().tone,
attr = notes.Last().phonemeAttributes,
duration = notes.Skip(vowelIds.Length - 1).Sum(n => n.duration),
position = notes.Sum(n => n.duration)
};
}
/// <summary>
/// extracts and validates symbols and vowels
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
private (string[], int[], Note[]) GetSymbolsAndVowels(Note[] notes) {
var mainNote = notes[0];
var symbols = GetSymbols(mainNote);
if (symbols == null) {
return (null, null, null);
}
if (symbols.Length == 0) {
symbols = new string[] { "" };
}
symbols = ApplyReplacements(symbols.ToList(), false).ToArray();
symbols = ApplyExtensions(symbols, notes);
List<int> vowelIds = ExtractVowels(symbols);
if (vowelIds.Count == 0) {
vowelIds.Add(symbols.Length - 1);
}
if (notes.Length < vowelIds.Count) {
notes = HandleNotEnoughNotes(notes, vowelIds);
}
return (symbols, vowelIds.ToArray(), notes);
}
/// <summary>
/// When there are more syllables than notes, recombines notes to match syllables count
/// </summary>
/// <param name="notes"></param>
/// <param name="vowelIds"></param>
/// <returns></returns>
protected virtual Note[] HandleNotEnoughNotes(Note[] notes, List<int> vowelIds) {
var newNotes = new List<Note>();
newNotes.AddRange(notes.SkipLast(1));
var lastNote = notes.Last();
var position = lastNote.position;
var notesToSplit = vowelIds.Count - newNotes.Count;
var duration = lastNote.duration / notesToSplit / 15 * 15;
for (var i = 0; i < notesToSplit; i++) {
var durationFinal = i != notesToSplit - 1 ? duration : lastNote.duration - duration * (notesToSplit - 1);
newNotes.Add(new Note() {
position = position,
duration = durationFinal,
tone = lastNote.tone,
phonemeAttributes = lastNote.phonemeAttributes
});
position += durationFinal;
}
return newNotes.ToArray();
}
/// <summary>
/// Override this method, if you want to implement some machine converting from a word to phonemes
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
protected virtual string[] HandleWordNotFound(Note note) {
var attr = note.phonemeAttributes?.FirstOrDefault(attr => attr.index == 0) ?? default;
string alt = attr.alternate?.ToString() ?? string.Empty;
string color = attr.voiceColor;
int toneShift = attr.toneShift;
var mpdlyric = MapPhoneme(note.lyric, note.tone + toneShift, color, alt, singer);
if(HasOto(mpdlyric, note.tone)){
error = mpdlyric;
}else{
error = "word not found";
}
return null;
}
/// <summary>
/// Does this note extend the previous syllable?
/// </summary>
/// <param name="note"></param>
/// <returns></returns>
protected bool IsSyllableVowelExtensionNote(Note note) {
return note.lyric.StartsWith("+~") || note.lyric.StartsWith("+*");
}
/// <summary>
/// Used to extract phonemes from CMU Dict word. Override if you need some extra logic
/// </summary>
/// <param name="phonemesString"></param>
/// <returns></returns>
protected virtual string[] GetDictionaryWordPhonemes(string phonemesString) {
return phonemesString.Split(' ');
}
/// <summary>
/// use to validate alias
/// </summary>
/// <param name="alias"></param>
/// <returns></returns>
protected virtual string ValidateAlias(string alias) {
return alias;
}
/// <summary>
/// Defines basic transition length before scaling it according to tempo and note length
/// Use GetTransitionBasicLengthMsByConstant, GetTransitionBasicLengthMsByOto or your own implementation
/// </summary>
/// <param name="alias">Mapped alias</param>
/// <returns></returns>
protected virtual double GetTransitionBasicLengthMs(string alias = "") {
return GetTransitionBasicLengthMsByConstant();
}
protected double GetTransitionBasicLengthMsByConstant() {
return TransitionBasicLengthMs * GetTempoNoteLengthFactor();
}
private void SetupYamlWatcher(string yamlFilePath) {
if (string.IsNullOrEmpty(yamlFilePath) || currentlyWatchedYaml == yamlFilePath) {
return;
}
if (yamlWatcher != null) {
yamlWatcher.Dispose();
yamlWatcher = null;
}
currentlyWatchedYaml = yamlFilePath;
string directory = Path.GetDirectoryName(yamlFilePath);
if (Directory.Exists(directory)) {
yamlWatcher = new YamlWatcher(directory, () => {
Log.Information($"[SyllableBasedPhonemizer] Detected change in {YamlFileName}, incrementing generation...");
System.Threading.Thread.Sleep(200);
lock (dictionaries) {
if (dictionaries.ContainsKey(this.GetType())) {
dictionaries.Remove(this.GetType());
}
}
backupVowels = null;
backupConsonants = null;
backupDictionaryReplacements = null;
globalYamlGeneration++;
if (this.singer != null) {
OpenUtau.Core.SingerManager.Inst.ScheduleReload(this.singer);
}
});
}
}
/// <summary>
/// a note length modifier, from 1 to 0.3. Used to make transition notes shorter on high tempo
/// </summary>
/// <returns></returns>
protected double GetTempoNoteLengthFactor() {
return (300 - Math.Clamp(bpm, 90, 300)) / (300 - 90) / 3 + 0.33;
}
protected virtual IG2p LoadBaseDictionary() {
var dictionaryName = GetDictionaryName();
var filename = Path.Combine(DictionariesPath, dictionaryName);
var dictionaryText = File.ReadAllText(filename);
var builder = G2pDictionary.NewBuilder();
var vowels = GetVowels();
foreach (var vowel in vowels) {
builder.AddSymbol(vowel, true);
}
var consonants = GetConsonants();
foreach (var consonant in consonants) {
builder.AddSymbol(consonant, false);
}
builder.AddEntry("a", new string[] { "a" });
ParseDictionary(dictionaryText, builder);
return builder.Build();
}
/// <summary>
/// Parses CMU dictionary, when phonemes are separated by spaces, and word vs phonemes are separated with two spaces,
/// and replaces phonemes with replacement table
/// Is Running Async!
/// </summary>
/// <param name="dictionaryText"></param>
/// <param name="builder"></param>
protected virtual void ParseDictionary(string dictionaryText, G2pDictionary.Builder builder) {
var replacements = GetDictionaryPhonemesReplacement();
foreach (var line in dictionaryText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)) {
if (line.StartsWith(";;;")) {
continue;
}
var parts = line.Trim().Split(wordSeparator, StringSplitOptions.None);
if (parts.Length != 2) {
continue;
}
string key = parts[0].ToLowerInvariant();
var values = GetDictionaryWordPhonemes(parts[1]).Select(
n => replacements != null && replacements.ContainsKey(n) ? replacements[n] : n);
lock (builder) {
builder.AddEntry(key, values);
};
};
}
#region helpers
/// <summary>
/// May be used if you have different logic for short and long notes
/// </summary>
/// <param name="syllable"></param>
/// <returns></returns>
protected bool IsShort(Syllable syllable) {
return syllable.duration != -1 && TickToMs(syllable.duration) < GetTransitionBasicLengthMs() * 2;
}
protected bool IsShort(Ending ending) {
return TickToMs(ending.duration) < GetTransitionBasicLengthMs() * 2;
}
/// <summary>
/// Checks if mapped and validated alias exists in oto
/// </summary>
/// <param name="alias"></param>
/// <param name="tone"></param>
/// <returns></returns>
protected bool HasOto(string alias, int tone) {
return singer.TryGetMappedOto(alias, tone, out _);
}
/// <summary>
/// Can be used for different variants, like exhales [v R], [v -] etc
/// </summary>
/// <param name="sourcePhonemes">phonemes container to add to</param>
/// <param name="tone">to map alias</param>
/// <param name="targetPhonemes">target phoneme variants</param>
/// <returns>returns true if added any</returns>
protected bool TryAddPhoneme(List<string> sourcePhonemes, int tone, params string[] targetPhonemes) {
foreach (var phoneme in targetPhonemes) {
if (HasOto(phoneme, tone)) {
sourcePhonemes.Add(phoneme);
return true;
}
}
return false;
}
/// <summary>
/// if true, you can put phoneme as null so the previous alias will be extended
/// </summary>
/// <param name="syllable"></param>
/// <returns></returns>
protected bool CanMakeAliasExtension(Syllable syllable) {
return syllable.canAliasBeExtended && syllable.prevV == syllable.v && syllable.cc.Length == 0;
}
/// <summary>
/// if current syllable is VV and previous one is from the same pitch,
/// you may wan't to just extend the previous alias. Put the phoneme as null fot that
/// </summary>
/// <param name="tone1"></param>
/// <param name="tone2"></param>
/// <returns></returns>
protected bool AreTonesFromTheSameSubbank(int tone1, int tone2) {
if (singer.Subbanks.Count == 1) {
return true;
}
if (tone1 == tone2) {
return true;
}
var toneSets = singer.Subbanks.Select(n => n.toneSet);
foreach (var toneSet in toneSets) {
if (toneSet.Contains(tone1) && toneSet.Contains(tone2)) {
return true;
}
if (toneSet.Contains(tone1) != toneSet.Contains(tone2)) {
return false;
}
}
return true;
}
protected virtual string YamlFileName => null;
protected virtual byte[] YamlTemplate => null;
protected virtual string YamlVersion => null;
protected string[] vowels = Array.Empty<string>();
protected string[] consonants = Array.Empty<string>();
protected string[] tails = "-,R".Split(',');
protected string[] affricate = Array.Empty<string>();
protected string[] fricative = Array.Empty<string>();
protected string[] aspirate = Array.Empty<string>();
protected string[] semivowel = Array.Empty<string>();
protected string[] liquid = Array.Empty<string>();
protected string[] nasal = Array.Empty<string>();
protected string[] stop = Array.Empty<string>();
protected string[] tap = Array.Empty<string>();
protected Dictionary<string, string> dictionaryReplacements = new Dictionary<string, string>();
protected Dictionary<string, double> PhonemeOverrides = new Dictionary<string, double>();
protected Dictionary<string, string> yamlFallbacks = new Dictionary<string, string>();
protected List<string> consExceptions = new List<string>();
public class YAMLData {
public string version { get; set; }
public SymbolData[] symbols { get; set; } = Array.Empty<SymbolData>();
public Replacement[] replacements { get; set; } = Array.Empty<Replacement>();
public Fallbacks[] fallbacks { get; set; } = Array.Empty<Fallbacks>();
public Timings[] timings { get; set; } = Array.Empty<Timings>();
public struct SymbolData { public string symbol { get; set; } public string type { get; set; } }
public struct Fallbacks { public string from { get; set; } public string to { get; set; } }
public struct Timings { public string symbol { get; set; } public double value { get; set; } }
}
public class Replacement {
public object from { get; set; }
public object to { get; set; }
public string where { get; set; } = "inside";
public List<string> FromList {
get {
if (from is string s) return new List<string> { s };
if (from is IEnumerable<object> list) return list.Select(x => x.ToString()).ToList();
return new List<string>();
}
}
public List<string> ToList {
get {
if (to is string s) return new List<string> { s };
if (to is IEnumerable<object> list) return list.Select(x => x.ToString()).ToList();
return new List<string>();
}
}
}
protected List<Replacement> mergingReplacements = new List<Replacement>();
protected List<Replacement> splittingReplacements = new List<Replacement>();
protected virtual bool IsGroupKeyword(string rulePhoneme) {
string baseGroup = rulePhoneme.Split(new[] { '!', '=', '+' })[0];
return new[] { "vowel", "vowels", "consonant", "consonants",
"affricate", "fricative", "aspirate", "semivowel",
"liquid", "nasal", "stop", "tap" }.Contains(baseGroup);
}
protected virtual bool IsGroupMatch(string rulePhoneme, string actualPhoneme) {
string baseGroup = rulePhoneme.Split(new[] { '!', '=', '+' })[0];
if (rulePhoneme.Contains("+")) {
string added = rulePhoneme.Substring(rulePhoneme.IndexOf('+') + 1).Split(new[] { '!', '=' })[0];
// If it matches another group name, or a literal letter, it passes
foreach (string inc in added.Split(',')) {
if (IsGroupKeyword(inc) ? IsGroupMatch(inc, actualPhoneme) : inc == actualPhoneme) {
return true;
}
}
}
// BASE GROUP: If it wasn't an addition, it must belong to the base group.
bool inBaseGroup = false;
switch (baseGroup) {